StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StEmcMixerMaker.cxx
1 #include "StEmcMixerMaker.h"
2 #include <Stiostream.h>
3 #include <math.h>
4 #include "SystemOfUnits.h"
5 #include <stdlib.h>
6 #include <string.h>
7 #include "TMath.h"
8 #include "TFile.h"
9 #include "Stypes.h"
10 #include "math_constants.h"
11 #include "StEmcSimulatorMaker/StEmcSimulatorMaker.h"
12 #include "StEventTypes.h"
13 #include "StEvent.h"
14 #include "StEmcUtil/geometry/StEmcGeom.h"
15 #include "tables/St_emcStatus_Table.h"
16 #include "tables/St_smdStatus_Table.h"
17 
18 ClassImp(StEmcMixerMaker)
19 
20 //_____________________________________________________________________________
21 StEmcMixerMaker::StEmcMixerMaker(const char *name):StMaker(name)
22 {
23  mAddHits = kTRUE;
24  mClear = kTRUE;
25  mUseDB = kTRUE;
26  mEmbedAll = kTRUE;
27  mFakeTrackEmbed = kFALSE;
28  for(Int_t i=0;i<NDETECTORS;i++) mGeom[i]=StEmcGeom::instance(i+1);
29 }
30 //_____________________________________________________________________________
31 StEmcMixerMaker::~StEmcMixerMaker()
32 {
33 }
34 //_____________________________________________________________________________
35 Int_t StEmcMixerMaker::Init()
36 {
37  m_hit_1 = new TH1F("old_hit","old_hit",100,0.,30.);
38  m_hit_2 = new TH1F("new_hit","new_hit",100,0.,30.);
39  m_edep_1 = new TH1F("EDEP1","edep1",100,0.,30.);
40  m_edep_2 = new TH1F("EDEP2","edep2",100,0.,30.);
41  return StMaker::Init();
42 }
43 //_____________________________________________________________________________
45 {
46  if(!getEvents()) return kStWarn;
47 
48  if(mUseDB) getDB(); // get status tables from DB. If not, set all status to 1 (valid channel)
49  else
50  for(Int_t i=0;i<NDETECTORS;i++)
51  for(Int_t j=0;j<(EMCCHANNELS+(MAXCHANNELS-EMCCHANNELS)*(i>1));j++)
52  mStatus[i][j] = 1;
53 
54  clearPoints(); // clear EMC points
55  clearClusters(); // clear EMC clusters
56 
57  if(mAddHits){ if(addHits()!=kStOk) { LOG_WARN <<" error in addhits***"<<endm; return kStWarn; } }
58  if(mFakeTrackEmbed) addTracks();
59  return kStOK;
60 }
61 
62 //_____________________________________________________________________________
64 {
65  return StMaker::Finish();
66 }
67 //--------------------------------------------------------------------
72 Int_t StEmcMixerMaker::addHits()
73 {
74  StEmcCollection* emccol1=(StEmcCollection*)mEvent1->emcCollection();
75  StEmcCollection* emccol2=(StEmcCollection*)mEvent2->emcCollection();
76  Int_t stat_h2_all[MAXCHANNELS];
77  Int_t stat_h2[MAXCHANNELS];
78 
79 // Loop over all four sub detectors
80  Float_t old_edep_tot=0.0;
81  Float_t new_edep_tot=0.0;
82  for(Int_t i=0; i<NDETECTORS; i++)
83  {
84  StDetectorId id = static_cast<StDetectorId>(i+kBarrelEmcTowerId);
85  StEmcDetector* detector1=emccol1->detector(id);
86  StEmcDetector* detector2=emccol2->detector(id);
87  if(!detector1){ LOG_WARN <<"detector1 not loaded"<<endm; }
88  if(!detector2){ LOG_WARN <<"detector2 not loaded"<<endm; }
89 
90  Float_t edep1_tot=0;
91  Float_t edep2_tot=0;
92 
93  if(detector1 && detector2) for(UInt_t j=1;j<=NMODULES;j++) // loop over all modules
94  {
95  StEmcModule* module1 = detector1->module(j);
96  StEmcModule* module2 = detector2->module(j);
97  StSPtrVecEmcRawHit& rawHit1=module1->hits();
98  StSPtrVecEmcRawHit& rawHit2=module2->hits();
99 
100  for(UInt_t k1=0;k1<rawHit1.size();k1++) {edep1_tot+=rawHit1[k1]->energy();}
101  for(UInt_t k1=0;k1<rawHit2.size();k1++) {edep2_tot+=rawHit2[k1]->energy();}
102 
103  for(UInt_t is=0;is<rawHit2.size();is++) {stat_h2_all[is]=0;stat_h2[is]=0;}
104 
105  for(UInt_t k1=0;k1<rawHit1.size();k1++)
106  {
107  Int_t m1, e1, s1;
108  m1=(Int_t)rawHit1[k1]->module();
109  e1=(Int_t)rawHit1[k1]->eta();
110  s1=abs(rawHit1[k1]->sub());
111  for(UInt_t is=0;is<rawHit2.size();is++) {stat_h2[is]=0;}
112 
113  Float_t edep_add=0.0;
114  UInt_t adc_add=0;
115  for(UInt_t k2=0;k2<rawHit2.size();k2++) if(checkHit(i,rawHit2[k2]))
116  {
117  if(stat_h2[k2]==1)continue;
118  Int_t m2, e2, s2;
119  m2=(Int_t)rawHit2[k2]->module();
120  e2=(Int_t)rawHit2[k2]->eta();
121  s2=abs(rawHit2[k2]->sub());
122  if(m1==m2 && e1==e2 && s1==s2)
123  {
124  stat_h2[k2]=1;
125  stat_h2_all[k2]=1;
126  edep_add=rawHit2[k2]->energy();
127  adc_add=rawHit2[k2]->adc();
128  }
129  if(stat_h2[k2]==1)break;
130  }
131  Float_t new_edep=rawHit1[k1]->energy()+edep_add;
132  UInt_t new_adc=rawHit1[k1]->adc()+adc_add;
133  Float_t oldE=rawHit1[k1]->energy();
134  old_edep_tot+=rawHit1[k1]->energy();
135  rawHit1[k1]->setAdc(new_adc);
136  rawHit1[k1]->setEnergy(new_edep);
137  new_edep_tot+=new_edep;
138  UInt_t calib = rawHit1[k1]->calibrationType();
139  while(calib>127) calib-=128;
140  if(edep_add>0) {
141  LOG_DEBUG <<"EMBEDDED HIT -> det = "<<i+1<<" m = "<<m1<<" e = "<<e1<<" s = "<<s1
142  <<" calib = "<<rawHit1[k1]->calibrationType()
143  <<" new calib = "<<calib
144  <<" oldE = "<<oldE<<" EADD = "<<edep_add
145  <<" newE = "<<rawHit1[k1]->energy()<<endm;
146  }
147  rawHit1[k1]->setCalibrationType(calib);
148  }
149 
150  //Add remainig hits
151  if(mEmbedAll) for(UInt_t k2=0;k2<rawHit2.size();k2++) if(checkHit(i,rawHit2[k2]))
152  {
153  if(stat_h2_all[k2]==0)
154  {
155  StEmcRawHit* hit = new StEmcRawHit(id, rawHit2[k2]->module(),
156  rawHit2[k2]->eta(), rawHit2[k2]->sub(),
157  rawHit2[k2]->adc(), rawHit2[k2]->energy());
158  detector1->addHit(hit);
159  new_edep_tot+=rawHit2[k2]->energy();
160  }
161  }
162  }
163  m_hit_1->Fill(old_edep_tot);
164  m_hit_2->Fill(new_edep_tot);
165  m_edep_1->Fill(edep1_tot);
166  m_edep_2->Fill(edep2_tot);
167  }
168  return kStOK;
169 }
170 //-------------------------------------------------------------------
174 void StEmcMixerMaker::clearPoints()
175 {
176  StEmcCollection* emccol=(StEmcCollection*)mEvent1->emcCollection();
177  if(emccol)
178  {
179  StSPtrVecEmcPoint& pvec = emccol->barrelPoints();
180  if(mClear)pvec.clear();
181  }
182 }
183 //-------------------------------------------------------------------
187 void StEmcMixerMaker::clearClusters()
188 {
189  StEmcCollection* emccol=(StEmcCollection*)mEvent1->emcCollection();
190  if(emccol) for(Int_t i=0; i<NDETECTORS; i++)
191  {
192  StDetectorId id = static_cast<StDetectorId>(i+kBarrelEmcTowerId);
193  StEmcDetector* detector=emccol->detector(id);
194  if(detector)
195  {
196  if(detector->cluster())
197  {
198  StSPtrVecEmcCluster& cluster=detector->cluster()->clusters();
199  if(mClear)cluster.clear();
200  }
201  }
202  }
203 }
204 //-------------------------------------------------------------------
212 Bool_t StEmcMixerMaker::getEvents()
213 {
214  // the default event should be the one where the data will be embedded
215  // for this, it should be created before the second event
216  // if there is no EMC collection in the event there is no reason for
217  // embedding.
218  mEvent1 = (StEvent*)GetInputDS("StEvent");
219  if(!mEvent1) return kFALSE;
220  if(!mEvent1->emcCollection()) return kFALSE; // should have EMC data
221 
222  // getting second event. First try StEmcSimulatorMaker
223  // if there is no EMC simulator, try to get from a second StEvent in the memory
224  // if the event source is the EMC simulator, StEmcMixerMaker owns the StEvent
225  // that is created to hold the StEmcCollection from simulator
226  // This maker adds the StEvent to .data so it will be properly deleted
227  // during Clear() action
228  StEmcSimulatorMaker *sim = (StEmcSimulatorMaker*)GetMaker("EmcSimulator");
229  if(sim)
230  {
231  StEmcCollection *ecol = sim->getEmcCollection();
232  if(ecol)
233  {
234  mEvent2 = new StEvent();
235  mEvent2->setEmcCollection(ecol);
236  AddData(mEvent2);
237  } else { LOG_WARN <<"No second event to embed"<<endm; return kFALSE; }
238  }
239  else // no EMC simulator. Events come from another source
240  {
241  StMaker *m = GetMaker("embedIO");
242  if(!m) { LOG_WARN <<"No embedIO maker"<<endm; return kFALSE; }
243  mEvent2 = (StEvent*)m->GetInputDS("StEvent");
244  if(!mEvent2) { LOG_WARN <<"No second event to embed"<<endm; return kFALSE; }
245  if(!mEvent2->emcCollection()) { LOG_WARN <<"No second event to embed"<<endm; return kFALSE; }
246  }
247 
248  LOG_DEBUG <<"Event 1 = "<<mEvent1<<" Event 2 ="<<mEvent2<<endm;
249 
250  // StEvent pointers should be different.
251  if(mEvent1==mEvent2) { LOG_DEBUG <<"Identical events"<<endm; return kFALSE; }
252  return kTRUE;
253 }
254 //-------------------------------------------------------------------
256 {
257  const TString detname[] = {"bemc", "bprs", "bsmde", "bsmdp","eemc", "eprs", "esmde", "esmdp"};
258  StEmcCollection* emccol=(StEmcCollection*)event->emcCollection();
259 
260  for(Int_t i=0; i<NDETECTORS; i++)
261  {
262  StDetectorId id = static_cast<StDetectorId>(i+kBarrelEmcTowerId);
263  StEmcDetector* detector=emccol->detector(id);
264  LOG_INFO <<"****************** hits in detector "<< detname[i].Data()<<endm;
265  if(detector) for(UInt_t j=1;j<=NMODULES;j++)
266  {
267  StEmcModule* module = detector->module(j);
268  StSPtrVecEmcRawHit& rawHit=module->hits();
269  if(rawHit.size()>0) { LOG_INFO <<"Number of hits for module "<<j<<" = "<<rawHit.size()<<endm; }
270  for(UInt_t k=0;k<rawHit.size();k++) {
271  LOG_INFO <<"Hit number = "<<k<<" module = " << rawHit[k]->module()<<" eta = "<<rawHit[k]->eta() << " sub = "<< rawHit[k]->sub()<< " adc = "<< rawHit[k]->adc() <<" energy = "<<rawHit[k]->energy()<<endm;
272  }
273  }
274  }
275 
276 }
277 //-------------------------------------------------------------------
282 void StEmcMixerMaker::getDB()
283 {
284  TDataSet *Db=GetDataBase("Calibrations/emc/y3bemc");
285  Int_t valid[NDETECTORS]; for(Int_t i=0;i<NDETECTORS;i++) valid[i]=0;
286  St_emcStatus* s0 = (St_emcStatus*)Db->Find("bemcStatus");
287  if(s0)
288  {
289  emcStatus_st* st=s0->GetTable();
290  for(Int_t i=0;i<EMCCHANNELS;i++) mStatus[0][i] = st[0].Status[i];
291  } else for(Int_t i=0;i<EMCCHANNELS;i++) mStatus[0][i] = 0;
292 
293  Db=GetDataBase("Calibrations/emc/y3bprs");
294  St_emcStatus* s1 = (St_emcStatus*)Db->Find("bprsStatus");
295  if(s1)
296  {
297  emcStatus_st* st=s1->GetTable();
298  for(Int_t i=0;i<EMCCHANNELS;i++) mStatus[1][i] = st[0].Status[i];
299  } else for(Int_t i=0;i<EMCCHANNELS;i++) mStatus[1][i] = 0;
300 
301  Db=GetDataBase("Calibrations/emc/y3bsmde");
302  St_smdStatus* s2 = (St_smdStatus*)Db->Find("bsmdeStatus");
303  if(s2)
304  {
305  smdStatus_st* st=s2->GetTable();
306  for(Int_t i=0;i<SMDCHANNELS;i++) mStatus[2][i] = st[0].Status[i];
307  } else for(Int_t i=0;i<SMDCHANNELS;i++) mStatus[2][i] = 0;
308 
309  Db=GetDataBase("Calibrations/emc/y3bsmdp");
310  St_smdStatus* s3 = (St_smdStatus*)Db->Find("bsmdpStatus");
311  if(s3)
312  {
313  smdStatus_st* st=s3->GetTable();
314  for(Int_t i=0;i<SMDCHANNELS;i++) mStatus[3][i] = st[0].Status[i];
315  } else for(Int_t i=0;i<SMDCHANNELS;i++) mStatus[3][i] = 0;
316 
317  for(Int_t i=0;i<NDETECTORS;i++)
318  for(Int_t j=0;j<(EMCCHANNELS*(i<2)+SMDCHANNELS*(i>1));j++)
319  if(mStatus[i][j]==1) valid[i]++;
320 
321  LOG_DEBUG <<"Date = "<<GetDate()<<" time = "<<GetTime()<<endm;
322  for(Int_t i=0;i<NDETECTORS;i++) { LOG_DEBUG <<"Number of valid channels for detector "<<i<<" = "<<valid[i]<<endm; }
323 }
324 //-------------------------------------------------------------------
331 Bool_t StEmcMixerMaker::checkHit(Int_t d,StEmcRawHit* h)
332 {
333  if(!h) return kFALSE;
334  Int_t m = (Int_t)h->module();
335  Int_t e = (Int_t)h->eta();
336  Int_t s = abs(h->sub());
337  Int_t id;
338  mGeom[d]->getId(m,e,s,id);
339  if(id>0) if(mStatus[d][id-1]==1) return kTRUE;
340  return kFALSE;
341 }
342 //--------------------------------------------------------------------
348 Int_t StEmcMixerMaker::addTracks()
349 {
350  // this just adds the tracks from one StEvent into the tracks of another StEvent.
351  StSPtrVecTrackNode& Node1 = mEvent1->trackNodes();
352  StSPtrVecTrackNode& Node2 = mEvent2->trackNodes();
353  for (unsigned int j=0; j < Node2.size(); j++)
354  {
355  StGlobalTrack* gTrack = (StGlobalTrack*)(Node2[j]->track(global));
356  StPrimaryTrack* pTrack = (StPrimaryTrack*)(Node2[j]->track(primary));
357  StTrackNode *node = new StTrackNode();
358  if(gTrack) node->addTrack(new StGlobalTrack(*gTrack));
359  if(pTrack) node->addTrack(new StPrimaryTrack(*pTrack));
360  Node1.push_back(node);
361  }
362  return 0;
363 }
void printHits(StEvent *)
Prints all EMC hits in the StEvent object.
virtual void AddData(TDataSet *data, const char *dir=".data")
User methods.
Definition: StMaker.cxx:332
StEmcCollection * getEmcCollection()
virtual Int_t Finish()
virtual Int_t Make()
Definition: Stypes.h:42
Definition: Stypes.h:40
virtual Int_t Finish()
Definition: StMaker.cxx:776
void addTracks(bool cuts=false)
Add tracks to the list of the rendered objects from current MuDst event.
Definition: EdMu.C:161
Definition: Stypes.h:41
virtual TDataSet * Find(const char *path) const
Definition: TDataSet.cxx:362