StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StTofSimMaker.cxx
1 /***************************************************************************
2  *
3  * $Id: StTofSimMaker.cxx,v 1.13 2018/02/26 23:26:51 smirnovd Exp $
4  *
5  * Author: Frank Geurts
6  ***************************************************************************
7  *
8  * Description: StTofSimMaker class for TOFp Simulations
9  *
10  **************************************************************************/
12 
19 #include <Stiostream.h>
20 #include "StTofSimMaker.h"
21 
22 // SCL
23 #include "StThreeVectorD.hh"
24 #include "Random.h"
25 #include "RanluxEngine.h"
26 #include "RandGauss.h"
27 #include "TH1.h"
28 #include "TFile.h"
29 
30 #include "StTofUtil/StTofCalibration.h"
31 #include "StTofUtil/StTofSimParam.h"
32 #include "StTofUtil/StTofGeometry.h"
33 #include "StEventTypes.h"
34 
35 // g2t tables and collections
36 #include "tables/St_g2t_ctf_hit_Table.h"
37 #include "tables/St_g2t_vpd_hit_Table.h"
38 //#include "tables/St_g2t_track_Table.h"
39 //#include "tables/St_g2t_tpc_hit_Table.h"
40 #include "StTofUtil/StTofDataCollection.h"
41 #include "StTofUtil/StTofSlatCollection.h"
42 #include "StTofMCSlat.h"
43 
44 typedef vector<StTofMCSlat> tofMCSlatVector;
45 typedef tofMCSlatVector::iterator tofMCSlatVecIter;
46 
47 
48 static RanluxEngine engine;
49 
50 
52 StTofSimMaker::StTofSimMaker(const char *name):StMaker(name){
53  mGeomDb = 0;
54  mCalibDb = 0;
55  mSimDb = 0;
56 }
57 
60 
61 
64  //mGeomDb = new StTofGeometry();
65  //mGeomDb->init(this);
66  mCalibDb = new StTofCalibration();
67  mCalibDb->init();
68  mSimDb = new StTofSimParam();
69  mSimDb->init();
70 
71  if (m_Mode){
72  // book histograms
73  mdE = new TH1F("energy deposition","dE",100,0.,0.011);
74  mdS = new TH1F("distance","ds",100,0.,10);
75  mNumberOfPhotoelectrons = new TH1F("number of photoelectrons","nphe",1000,0,5000);
76  mT = new TH1F("delay corrected tof","time",100,0.,12e-7);
77  mTime = new TH1F("only hit-pos resolution added","tt",100,0.,12e-7);
78  mTime1 = new TH1F("fully corrected tof","tt1",100,0.,120e-7);
79  mPMlength = new TH1F("distance in slat","length",100,0,22);
80  mAdc = new TH1F("adc","adc",1025,-0.5,1024.5);
81  mTdc = new TH1F("tdc","tdc",2049,-0.5,2048.5);
82  }
83 
84  return StMaker::Init();
85 }
86 
87 
88 
90 Int_t StTofSimMaker::InitRun(int runnumber){
91  LOG_INFO << "StTofSimMaker::InitRun -- initializing TofGeometry --" << endm;
92  mGeomDb = new StTofGeometry();
93  mGeomDb->init(this);
94  return kStOK;
95 }
96 
97 
98 
100 Int_t StTofSimMaker::FinishRun(int runnumber){
101  LOG_INFO << "StTofSimMaker::FinishRun -- cleaning up TofGeometry --" << endm;
102  if (mGeomDb) delete mGeomDb;
103  mGeomDb=0;
104  return 0;
105 }
106 
107 
108 
111  LOG_INFO << "StTofSimMaker Make() starts" << endm;
112 
113  StTofSlatCollection *mSlatCollection = new StTofSlatCollection;
114  tofMCSlatVector tofMC;
115 
116  // Check for GEANT data
117  St_DataSet *geantData = GetInputDS("geant");
118  if (geantData) {
119  St_DataSetIter geantIter(geantData);
120 
121  // TOFp hits
122  St_g2t_ctf_hit *g2t_tof_hit = (St_g2t_ctf_hit*) geantIter("g2t_tof_hit");
123  if (g2t_tof_hit){
124  g2t_ctf_hit_st* tof_hit = g2t_tof_hit->GetTable();
125  int numberOfTofHits = g2t_tof_hit->GetNRows();
126  LOG_INFO << "TOF #hits: " << numberOfTofHits << endm;
127 
128  tofMCSlatVector MCSlatVec;
129  MCSlatVec.clear();
130  for (int i=0;i<numberOfTofHits;i++,tof_hit++){
131  MCSlatVec.push_back(detectorResponse(tof_hit));
132  }
133 
134  // build ... from MCSlatVec which may have entries with same slatId
135  tofMCSlatVector slatTempVec = MCSlatVec;
136  tofMCSlatVector slatErasedVec = slatTempVec;
137  tofMCSlatVecIter slatTempIter, slatErasedIter;
138 
139  while (slatTempVec.size()!=0){
140  unsigned short fastTdc;
141  int nFired=0, accumNPhe=0;
142  float accumDe=0., accumDs=0., fastTof=0.;
143  slatTempIter=slatTempVec.begin();
144  slatErasedIter=slatErasedVec.begin();
145  fastTof = slatTempIter->mcInfo().mTof;
146  fastTdc = slatTempIter->tdc();
147 
148  while(slatErasedIter!=slatErasedVec.end()){
149  if(slatTempIter->slatIndex() == slatErasedIter->slatIndex()){
150  nFired++;
151  accumDe += slatErasedIter->mcInfo().mDe;
152  accumDs += slatErasedIter->mcInfo().mDs;
153  accumNPhe += slatErasedIter->mcInfo().mNPhe;
154  fastTof = min(fastTof, slatErasedIter->mcInfo().mTof);
155  fastTdc = min(fastTdc, slatErasedIter->tdc());
156 
157  slatErasedVec.erase(slatErasedIter);
158  slatErasedIter--;
159  }
160  slatErasedIter++;
161  }
162  StTofMCSlat MCSlat = *slatTempIter;
163  MCSlat.setNHits(nFired);
164  MCSlat.setNPhe(accumNPhe);
165  MCSlat.setDe(accumDe);
166  MCSlat.setDs(accumDs);
167  MCSlat.setTof(fastTof);
168  MCSlat.setTdc(fastTdc);
169  tofMC.push_back(MCSlat);
170 
171  slatTempVec = slatErasedVec;
172  }
173  LOG_INFO << "StTofSimMaker::make() vector size from " << MCSlatVec.size()
174  << " to " << tofMC.size() << endm;
175 
177 
178  fillRaw();
179  if (mSimDb->elec_noise() < 0) electronicNoise();
180  fillEvent();
181  for (unsigned int i=0;i<tofMC.size(); i++){
182  StTofMCSlat *MCSlatPtr = new StTofMCSlat();
183  *MCSlatPtr = tofMC[i];
184  //LOG_INFO << *MCSlatPtr ;
185  mSlatCollection->push_back(MCSlatPtr);
186  }
187  }
188  else
189  LOG_INFO << "StTofSimMaker Make() no TOF hits found" << endm;
190 
191  // pVPD section hits
192  St_g2t_vpd_hit *g2t_vpd_hit = (St_g2t_vpd_hit*) geantIter("g2t_vpd_hit");
193  if (g2t_vpd_hit){
194  // g2t_vpd_hit_st* vpd_hit = g2t_vpd_hit->GetTable();
195  int numberOfVpdHits = g2t_vpd_hit->GetNRows();
196  LOG_INFO << "VPD #hits: " << numberOfVpdHits << endm;
197  }
198  else
199  LOG_INFO << "StTofSimMaker Make() no VPD hits found" << endm;
200  }
201 
202 
203  // send off to StEvent
204  StTofCollection *mTheTofCollection = new StTofCollection();
205  for (size_t j=0;j<mSlatCollection->size();j++){
206  mTheTofCollection->addSlat(dynamic_cast<StTofMCSlat*>(mSlatCollection->getSlat(j)));
207  // mTheTofCollection->addSlat(mSlatCollection->getSlat(j));
208 }
209 
210  // create tofData collection
211  StTofDataCollection *mDataCollection = new StTofDataCollection;
212  for (int i=0;i<48;i++){
213  bool slatFound = false;
214  int j=0;
215  for (j=0;j<(int)mSlatCollection->size();j++){
216  StTofSlat *tempSlat = mSlatCollection->getSlat(j);
217  unsigned short indexSlat = tempSlat->slatIndex();
218  if (indexSlat == mGeomDb->daqToSlatId(i)){
219  slatFound = true;
220  // StTofData *rawTofData = new StTofData(indexSlat,tempSlat->adc(),tempSlat->tdc(),0,0);
221  // update for year 5 new format
222  StTofData *rawTofData = new StTofData(indexSlat,tempSlat->adc(),tempSlat->tdc(),0,0,0,0);
223  if (Debug()) LOG_INFO << indexSlat << ": A" << tempSlat->adc() << " T" << tempSlat->tdc() << endm;
224  mDataCollection->push_back(rawTofData);
225  }
226  }
227  if (!slatFound){
228  // StTofData *rawTofData = new StTofData(mGeomDb->daqToSlatId(i),0,0,0,0);
229  // update for year 5 new format
230  StTofData *rawTofData = new StTofData(mGeomDb->daqToSlatId(i),0,0,0,0,0,0);
231  mDataCollection->push_back(rawTofData);
232  }
233  }
234  for(size_t jj = 0; jj < mDataCollection->size(); jj++)
235  mTheTofCollection->addData(mDataCollection->getData(jj));
236 
237  mEvent = (StEvent*) GetInputDS("StEvent");
238  if (mEvent) mEvent->setTofCollection(mTheTofCollection);
239  else{
240  LOG_INFO << "StTofSimMaker: Where is StEvent !?! Unable to store data" << endm;
241  return kStWarn;
242  }
243 
244 
245  // verify existence of tofCollection in StEvent (mEvent)
246  LOG_INFO << "StTofSimMaker: verifying TOF StEvent data ..." << endm;
247  StTofCollection *mmTheTofCollection = mEvent->tofCollection();
248  if(mmTheTofCollection) {
249  LOG_INFO << " + StEvent tofCollection Exists" << endm;
250  if(mmTheTofCollection->slatsPresent())
251  LOG_INFO << " + StEvent TofSlatCollection Exists" << endm;
252  else
253  LOG_INFO << " - StEvent TofSlatCollection DOES NOT Exist" << endm;
254  if(mmTheTofCollection->hitsPresent())
255  LOG_INFO << " + StEvent TofHitCollection Exists" << endm;
256  else
257  LOG_INFO << " - StEvent TofHitCollection DOES NOT Exist" << endm;
258  }
259  else {
260  LOG_INFO << " - StEvent tofCollection DOES NOT Exist" << endm;
261  LOG_INFO << " - StEvent TofSlatCollection DOES NOT Exist" << endm;
262  LOG_INFO << " - StEvent TofHitCollection DOES NOT Exist" << endm;
263  }
264 
265  LOG_INFO << "StTofSimMaker Make() finished" << endm;
266  return kStOK;
267 }
268 
269 
272 {
273  if(Debug()){
274  // dump the g2t structure ...
275  LOG_INFO << " " <<setw( 3) << tof_hit->id << " " <<setw( 4) << tof_hit->next_tr_hit_p
276  << " " <<setw( 4) << tof_hit->track_p << " " <<setw( 8) << tof_hit->volume_id
277  << " " <<setw(13) << tof_hit->de << " " <<setw(11) << tof_hit->ds
278  << " " <<setw(12) << tof_hit->p[0] << " " <<setw(12) << tof_hit->p[1]
279  << " " <<setw(12) << tof_hit->p[2] << " " <<setw( 7) << tof_hit->s_track
280  << " " <<setw(13) << tof_hit->tof << " " <<setw(10) << tof_hit->x[0]
281  << " " <<setw(10) << tof_hit->x[1] << " " <<setw(10) << tof_hit->x[2]
282  << endm;
283  }
284  // skip the consistency checks for now,
285 
286  // determine eta and phi indices from hitpoint and slatId
287  StThreeVectorD hitPoint = StThreeVectorD(tof_hit->x[0],
288  tof_hit->x[1],
289  tof_hit->x[2]);
290  //fg StTofCross* mTofCross = new StTofCross;
291  int slatId= (int) mGeomDb->tofSlatCrossId(hitPoint);
292  int volId = (int) mGeomDb->tofSlatCrossId(tof_hit->volume_id);
293 
294  if (slatId != volId){
295  LOG_INFO << "StTofSimMaker::Make Warning: volume_id ("<< volId
296  << ") and hit ("<<slatId<<") inconsistent. Switching to volumeid."<< endm;
297  slatId=volId;
298  }
299 
300  // retrieve dbase parameters
301  float zmin = mGeomDb->tofSlat(slatId).z_min;
302  float zmax = mGeomDb->tofSlat(slatId).z_max;
303  float cosang = mGeomDb->tofSlat(slatId).cosang;
304 
305  float length = (zmax-tof_hit->x[2])/cosang ;
306  float max_distance = (zmax-zmin)/cosang ;
307 
308  if (length>max_distance || length<0){
309  LOG_INFO << "StTofSimMaker: length="<<length<<" max="<<max_distance
310  << " zmin="<<zmin<<" zmax="<<zmax<<" coasng="<<cosang<<endm;
311  mGeomDb->printSlat(slatId);
312  }
313 
314 
315  // do the slat response modelling similar to cts
316  long numberOfPhotoelectrons;
317  if (mSimDb->slat_para()){
318  LOG_INFO << "StTofSimMaker Slat Response Table not implemented yet. "
319  << " Switching to exponential model instead" <<endm;
320  }
321  //numberOfPhotoelectrons = long (tof_hit->de * slatResponseExp(length));
322  numberOfPhotoelectrons = long (tof_hit->de * mSimDb->GeV_2_n_photons()
323  * mSimDb->cath_eff() * mSimDb->cath_surf() * mSimDb->surf_loss());
324 
325  // prepare some random generator stuff
326  RandGauss random(engine);
327 
328  // calculate TOFs with all kinds of resolutions
329  float time= tof_hit->tof + length*mSimDb->delay();
330  float resl= mSimDb->time_res() * ::sqrt(length);
331  if (resl<50e-12) resl=50e-12;
332  float tt = tof_hit->tof + (float) random.shoot()* resl;
333  float tt1 = time + (float) random.shoot()* mSimDb->start_res();
334 
335  // fill the histograms
336  if(m_Mode){
337  mPMlength->Fill(length);
338  mdE->Fill(tof_hit->de);
339  mdS->Fill(tof_hit->ds);
340  mNumberOfPhotoelectrons->Fill(numberOfPhotoelectrons);
341  mT->Fill(time);
342  mTime->Fill(tt);
343  mTime1->Fill(tt1);
344  }
345 
346  // fill or update the mcInfo structure of StTofMCslat
347  StTofMCSlat slat;
348  slat.setSlatIndex(slatId);
349  StTofMCInfo slatData;
350  slatData.mNHits = 1; // slats will be reorderd and #hits/slat recounted
351  slatData.mNPhe = numberOfPhotoelectrons;
352  slatData.mDe = tof_hit->de;
353  slatData.mDs = tof_hit->ds;
354  slatData.mTof = tof_hit->tof;// warning: this is *NOT* correct ... prop-delay is not included !!!
355  slatData.mTime = time;
356  slatData.mMTime = tt;
357  slatData.mMTimeL = tt1;
358  slatData.mPmLength = length;
359  slatData.mSLength = tof_hit->s_track;
360  StThreeVectorD hitMomentum = StThreeVectorD(tof_hit->p[0],
361  tof_hit->p[1],
362  tof_hit->p[2]);
363  slatData.mPTot = hitMomentum.mag();
364  slatData.mGId = tof_hit->id;
365  slatData.mTrkId = tof_hit->track_p;
366 
367  slat.setMCInfo(slatData);
368 
369  // do a rough cts-style calibration
370  float tdcOffset = mCalibDb->slat(slatId).offset_tdc
371  + random.shoot() * mCalibDb->slat(slatId).ods_tdc;
372  float cctdc = mCalibDb->slat(slatId).cc_tdc;
373  if(cctdc==0) cctdc=0.0001;
374  unsigned short tdc = (unsigned short)((float)tt/cctdc+ tdcOffset);
375  float adcOffset = mCalibDb->slat(slatId).offset_adc
376  + random.shoot() * mCalibDb->slat(slatId).ods_adc;
377  unsigned short adc = (unsigned short)((float)numberOfPhotoelectrons
378  * mSimDb->nphe_to_adc() + adcOffset);
379  if (tdc>2048) tdc=2048;
380  if (adc>1024) adc=1024;
381  slat.setAdc(adc);
382  slat.setTdc(tdc);
383 
384  if(m_Mode){
385  mAdc->Fill(adc);
386  mTdc->Fill(tdc);
387  }
388 
389  if (Debug()){
390  LOG_INFO << "StTofmcInfo slatId " << slatId << " " << slatData;
391  LOG_INFO << " a:" << adc << " t:" << tdc << " dE:"<< slatData.mDe << " dS:"<< slatData.mDs
392  << " mTof:" << slatData.mTof << " mTime:"<< slatData.mTime << " mMTime:"<<slatData.mMTime
393  << " mMTimeL:"<< slatData.mMTimeL << " mSLength:" << slatData.mSLength
394  << " mPTot:" << slatData.mPTot << endm;
395  }
396 
397  // this part considers X-talk between slats (based on parameter below) ...
398  // LOG_INFO << "PHYSNOISE PARAMETER: " << mSimDb->phys_noise() << endm;
399 
400  return slat;
401 }
402 
403 
404 
407 {
408  // Exponential model for slat response
409  return mSimDb->GeV_2_n_photons() * mSimDb->cath_eff()
410  * mSimDb->cath_surf() * mSimDb->surf_loss()
411  * exp(-dz/mSimDb->attlen());
412 }
413 
414 
417 
418  if(m_Mode){
419  LOG_INFO << "StTofSimMaker::Finish writing tofsim.root ...";
420  TFile theFile("tofsim.root","RECREATE","tofsim");
421  theFile.cd();
422  mdE->Write();
423  mdS->Write();
424  mNumberOfPhotoelectrons->Write();
425  mT->Write();
426  mTime->Write();
427  mTime1->Write();
428  mPMlength->Write();
429  mAdc->Write();
430  mTdc->Write();
431  LOG_INFO << "done"<<endm;
432  }
433  return kStOK;
434 }
435 
436 
439  //fill the adc and tdc entries.
440 }
441 
442 
445 }
446 
447 
450 }
451 
452 /***************************************************************************
453  *
454  * $Log: StTofSimMaker.cxx,v $
455  * Revision 1.13 2018/02/26 23:26:51 smirnovd
456  * StTof: Remove outdated ClassImp macro
457  *
458  * Revision 1.12 2018/02/26 23:13:21 smirnovd
459  * Move embedded CVS log messages to the end of file
460  *
461  * Revision 1.11 2007/04/17 23:02:36 dongx
462  * replaced with standard STAR Loggers
463  *
464  * Revision 1.10 2006/12/08 18:55:26 dongx
465  * Update to avoid zero tdc value in denominator - modified by Jing Liu
466  *
467  * Revision 1.9 2005/04/13 16:03:29 dongx
468  * corresponding changes because of the update of StTofData structure
469  *
470  * Revision 1.8 2004/04/01 21:33:46 jeromel
471  * More than one place where m_Mode should be used
472  *
473  * Revision 1.6 2003/09/17 19:49:10 geurts
474  * zeroed pointers in constructor
475  *
476  * Revision 1.5 2003/09/02 17:59:10 perev
477  * gcc 3.2 updates + WarnOff
478  *
479  * Revision 1.4 2003/08/08 00:22:11 geurts
480  * changed location of header files for the local collections
481  *
482  * Revision 1.3 2003/07/25 04:34:44 geurts
483  * - upper adc and tdc limits
484  * - geometry initialization moved to InitRun()
485  *
486  * Revision 1.2 2002/12/12 01:43:46 geurts
487  * Introduced InitRun() and FinishRun() members.
488  * TofData in TofCollection is filled with adc and tdc data.
489  * Extra checks for StEvent object to prevent null pointers.
490  * Primitive ADC response function, disabled slatResponseExp().
491  *
492  * Revision 1.1 2001/09/28 19:11:11 llope
493  * first version
494  */
Int_t m_Mode
counters
Definition: StMaker.h:81
tofSlatGeom_st tofSlat(const Int_t slatId) const
return slat geometry structure for slatId
void fillRaw(void)
digitize to ADC and TDC entries (empty)
virtual Int_t Finish()
write histograms to file
void fillEvent()
fill event (empty)
virtual Int_t Make()
read in GSTAR table and create TOF SlatCollection
Int_t FinishRun(int)
FinishRun method, clean up TOFp dBase entries.
Time-of-Flight Calibration Utilities.
virtual ~StTofSimMaker()
default empty destructor
void init()
initializes calibration from XDF or dBase (not functioning yet)
StTofMCSlat detectorResponse(g2t_ctf_hit_st *)
calculate detector response for a single hit
Time-of-Flight Geometry Utilities.
virtual Int_t Init()
Initialize dBase interfaces and book histograms.
Definition: Stypes.h:42
Int_t InitRun(int)
InitRun method, (re)initialize TOFp data from STAR dBase.
Definition: Stypes.h:40
StTofSimMaker(const char *name="TofSim")
default constructor
void electronicNoise(void)
simulate electronic noise (empty)
float slatResponseExp(float &)
exponential slat response model
void printSlat(const Int_t slatId, ostream &os=cout) const
print slat-specific geometry parameters
int tofSlatCrossId(const StThreeVectorD &point) const
return the index of a slat if the point is in the slat
Time-of-Flight Simulation Utilities.
Definition: StTofSimParam.h:24
void init()
initializes calibration from XDF or dBase (not functioning yet)
void init()
initialize geometry class from XDF file and set-up DAQ/Slat mappings