HYDRA_development_version
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
hemcdigitizer.cc
Go to the documentation of this file.
1 using namespace std;
2 #include "TRandom.h"
3 #include "hemcdigitizer.h"
4 #include "hdebug.h"
5 #include "hades.h"
6 #include "hiterator.h"
7 #include "hruntimedb.h"
8 #include "hspectrometer.h"
9 #include "hemcdetector.h"
10 #include "hgeantemc.h"
11 #include "hgeantkine.h"
12 #include "hevent.h"
13 #include "hlinearcategory.h"
14 #include "hemccalsim.h"
15 #include "hemcgeompar.h"
16 #include "hemcdigipar.h"
17 #include "hemccellstatuspar.h"
18 #include "hemcsimulpar.h"
19 #include "hstartdef.h"
20 #include "hstart2hit.h"
21 
22 #include <iostream>
23 
24 //*-- Author : V. Pechenov
25 //*-- Modified :
26 
27 /////////////////////////////////////////////////////////////////////
28 //
29 // HEmcDigitizer digitizes data, puts output values into cal data category
30 //
31 // Input data for digitization:
32 // Two types of HGeantEmc objects:
33 // 1. Hit which has track number = -777 keeps integrated number of photo electrons
34 // 2. Hit which has track number > 0 keeps a time of flight, momentum, track length and number of photo electrons
35 //
36 // Parameters for recalculating of the number of photo electrons to the energy deposit
37 // Parameters for the time and energy deposit smearings
38 // Parameter for the start signal smearing
39 //
40 // Two modes of reconstruction: simulation and embedding
41 //
42 // HEmcDigitizer
43 // collects all tracks in the cell,
44 // corrects GEANT time of flight to the time in the input EMC plane,
45 // sorts tracks by corrected time and assigns the cell time to the fastest track which has energy deposit above threshold,
46 // sorts tracks by energy and assigns the cell to the track which has maximal energy deposit.
47 //
48 // Digitizer retrieves a particle from track history which crosses parallel plane to EMC plane and distance to front of EMC is equal to zVertBorder.
49 // zVertBorder >= 0 EMC front plane is used
50 // zVertBorder = -130. RPC front plane is used (default value)
51 //
52 /////////////////////////////////////////////////////////////////////
53 
55 
57  initVariables();
58 }
59 
60 HEmcDigitizer::HEmcDigitizer(const Text_t *name, const Text_t *title) : HReconstructor(name,title) {
61  initVariables();
62 }
63 
65  if (iterGeantEmc) delete iterGeantEmc;
67  cellobjects.clear();
68 }
69 
71  fGeantEmcCat = NULL;
72  fGeantKineCat = NULL;
73  fCalCat = NULL;
74  iterGeantEmc = NULL;
75  fGeomPar = NULL;
76  fDigiPar = NULL;
77  fStartHitCat = NULL;
78  zVertBorder = -130.; // [mm]
79  energyDepositCut = 15.; // [MeV]
80  signalVelocity = 299.792; // [mm/ns]
81  halfOfCellLength = 210.; // [mm]
82  embeddingmode = 0;
83  fLoc.setNIndex(2);
84  fLoc.set(2,0,0);
85 }
86 
87 Bool_t HEmcDigitizer::init(void) {
89  if(!fEmcDet){
90  Error("init","No Emc Detector found");
91  return kFALSE;
92  }
93 
94  // working array
95  cellobjects.resize(cellObjectsSize(), 0 ); // size is constant over run time
96 
97  // GEANT input data
99  if (!fGeantEmcCat) {
100  Error("HEmcDigitizer::init()","HGeant EMC input missing");
101  return kFALSE;
102  }
103  iterGeantEmc = (HIterator *)fGeantEmcCat->MakeIterator("native");
104 
106  if(!fGeantKineCat){
107  Error("HEmcDigitizer::init()","No catGeantKine in input!");
108  return kFALSE;
109  }
110 
111  // Build the Calibration category
113  if (fCalCat == NULL) {
114  fCalCat = fEmcDet->buildMatrixCategory("HEmcCalSim",0.5);
116  }
117  if(fCalCat == NULL) return kFALSE;
119 
120 
122  if (!fStartHitCat) Warning("init","Start hit level not defined; setting start time to 0");
123 
124  return setParameterContainers();
125 }
126 
129  if (!fGeomPar){
130  Error("initParContainer","No EmcGeomPar parameter container");
131  return kFALSE;
132  }
133  fDigiPar =(HEmcDigiPar *)gHades->getRuntimeDb()->getContainer("EmcDigiPar");
134  if (!fDigiPar){
135  Error("initParContainer","No EmcDigiPar parameter container");
136  return kFALSE;
137  }
138  fSimulPar =(HEmcSimulPar *)gHades->getRuntimeDb()->getContainer("EmcSimulPar");
139  if (!fSimulPar){
140  Error("initParContainer","No EmcSimulPar parameter container");
141  return kFALSE;
142  }
143  pStatuspar = (HEmcCellStatusPar*)gHades->getRuntimeDb()->getContainer("EmcCellStatusPar");
144  if (!pStatuspar) return kFALSE;
145  return kTRUE;
146 }
147 
148 Bool_t HEmcDigitizer::reinit(void) {
149  // sets some local variables read from initialized parameter container
151  phot2Energy[0] = fDigiPar->getPhot2E(); // for PMT type 1 (1.5inch)
152  phot2Energy[1] = fDigiPar->getPhot2E2(); // for PMT type 2 (3.0inch)
153  Float_t sigmaEIntern = fDigiPar->getSigmaEIntern();
154  Float_t sigmaEReal = fDigiPar->getSigmaEReal(); // for PMT type 1 (1.5inch)
155  Float_t sigmaEReal2 = fDigiPar->getSigmaEReal2(); // for PMT type 2 (3.0inch)
156  facEnergSmear[0] = 1000.*TMath::Sqrt(sigmaEReal*sigmaEReal - sigmaEIntern*sigmaEIntern);
157  facEnergSmear[1] = 1000.*TMath::Sqrt(sigmaEReal2*sigmaEReal2 - sigmaEIntern*sigmaEIntern);
158  for(Int_t s=0;s<6;s++) labTrans[s] = fGeomPar->getModule(s)->getLabTransform();
159  return kTRUE;
160 }
161 
163  // Digitization of GEANT hits and storage in HEmcCalSim
164 
165  // Getting start time smearing
166  Float_t startTimeSmearing = 0; //[ns]
167  if (fStartHitCat && fStartHitCat->getEntries()>0) {
168  HStart2Hit *pStartH = (HStart2Hit *) fStartHitCat->getObject(0);
169  if(pStartH != NULL && pStartH->getResolution()!=-1000) {
170  startTimeSmearing = pStartH->getResolution();
171  }
172  }
173 
174  // Case of embedding real tracks in the sim.data
175  if(embeddingmode > 0) {
176  if(gHades->getEmbeddingDebug() == 1) {
177  fCalCat->Clear();
178  } else {
179  Int_t nentr = fCalCat->getEntries();
180  for(Int_t n=0;n<nentr;n++) {
181  HEmcCalSim* pCal = (HEmcCalSim*)fCalCat->getObject(n);
182  Int_t sec = pCal->getSector();
183  Int_t cell = pCal->getCell();
184  if (sec<0 || sec>5 || cell<0 || cell>=emcMaxComponents) {
185  Warning("HEmcDigitizer:execute","EmcCal cell address invalid: sec=%i cell=%i",sec,cell);
186  continue;
187  }
188  Int_t ind = cellObjectIndex(sec,cell);
189  if(cellobjects[ind] == NULL) {
190  cellobjects[ind] = new celldata;
191  cellobjects[ind]->reset();
192  }
193  cellobjects[ind]->isEmbeddedReal = kTRUE;
194  celltrack* celltr = new celltrack;
195  celltr->reset();
196  celltr->trackEn = pCal->getEnergy();
197  celltr->gtime = pCal->getTime();
199  cellobjects[ind]->ctracks.push_back(celltr);
200  }
201  }
202  }
203  // loop over the HGeantEmc objects and fill temporary working objects
204  iterGeantEmc->Reset();
205  HGeantEmc* geantemc = 0;
206  while ((geantemc=(HGeantEmc *)iterGeantEmc->Next())!=0) {
207  Int_t trackNumber = geantemc->getTrack();
208  Int_t sec = geantemc->getSector();
209  Int_t cell = geantemc->getCell();
210  if (sec<0 || sec>5 || cell<0 || cell>=emcMaxComponents) {
211  Warning("HEmcDigitizer:execute","Emc Geant cell address invalid: sec=%i cell=%i",sec,cell);
212  continue;
213  }
214  if(trackNumber <= 0 && trackNumber != -777) continue; // nothing to do for real data
215 
216  Int_t pos = HEmcDetector::getPositionFromCell(cell);
217  if (pStatuspar->getCellStatus(sec, pos) == 0) continue;
218 
219  Float_t peHit, xHit, yHit, zHit, tofHit, momHit, trackLength;
220  geantemc->getHit(peHit, xHit, yHit, zHit, tofHit, momHit, trackLength);
221  if(peHit == 0.) continue; // Number of photo electrons must be > 0
222 
223  Int_t pmtType = fSimulPar->getPmtType(sec,cell);
224  if(pmtType < 1 || pmtType > 2) continue; // never should happens
225  Float_t energyHit = peHit * phot2Energy[pmtType-1]; // Convert to MeV
226 
227  Int_t ind = cellObjectIndex(sec,cell);
228  if(cellobjects[ind] == NULL) {
229  cellobjects[ind] = new celldata;
230  cellobjects[ind]->reset();
231  cellobjects[ind]->energy = 0.F;
232  }
233  celldata* cdata = cellobjects[ind];
234 
235  if(trackNumber == -777) { // -777: hits with integrated number of photo electrons
236  cdata->energy += energyHit;
237  } else {
238  HGeantEmc* pFirstEmc = getInputHit(geantemc,trackNumber);
239  if(trackNumber <= 0 || pFirstEmc == NULL) continue;
240 
241  // Correction for the signal speed:
242  tofHit -= (zHit+halfOfCellLength)/signalVelocity;
243  // "zHit+halfOfCellLength" is distance from beginning of cell to the HGeandEmc hit
244 
245  Bool_t isInTheList = kFALSE;
246  Int_t numInpTrack = pFirstEmc->getTrack();
247 
248  if(std::find((cdata->inputTracks).begin(),(cdata->inputTracks).end(),numInpTrack) != cdata->inputTracks.end()) continue;
249  cdata->inputTracks.push_back(numInpTrack);
250  Float_t tofHitD;
251  pFirstEmc->getHit(peHit, xHit, yHit, zHit, tofHitD, momHit, trackLength);
252  Int_t pmtType = fSimulPar->getPmtType(sec,cell);
253  energyHit = peHit * phot2Energy[pmtType-1]; // Convert to MeV
254 
255  // Is this track in the track list already:
256  for(UInt_t i=0;i<cdata->ctracks.size();i++) {
257  celltrack* celltr = cdata->ctracks[i];
258  if (celltr->gtrack == trackNumber) {
259  // The current track found in list
260  isInTheList = kTRUE;
261  celltr->trackEn += energyHit;
262  if (tofHit < celltr->gtime) celltr->gtime = tofHit;
263  break;
264  }
265  }
266  if( !isInTheList ) {
267  // If track is not in the list yet
268  // create new object for this cell and add them to the list of tracks for this cell
269  celltrack* celltr = new celltrack;
270  celltr->reset();
271  celltr->trackEn = energyHit;
272  celltr->gtime = tofHit;
273  celltr->gtrack = trackNumber;
274  cdata->ctracks.push_back(celltr);
275  }
276  }
277  } // end of HGeantEmc loop
278 
279  for(UInt_t i = 0; i < cellobjects.size(); i ++) {
280  celldata* cdata = cellobjects[i];
281  if (cdata!=NULL && cdata->energy>0.) {
282  if(cdata->ctracks.size() == 0) continue;
283  Int_t cell = cellFromIndex(i);
284  fLoc[0] = sectorFromIndex(i);
285  fLoc[1] = cell;
286  HEmcCalSim* cal = (HEmcCalSim*) fCalCat->getSlot(fLoc);
287  if (cal == NULL) {
288  Warning("HEmcDigitizer:execute","HEmcCalSim:getSlot(loc) failed!");
289  continue;
290  }
291  Float_t sigmaE = 0.;
292  Float_t energy = 0.;
293  if(cdata->energy > 0.) {
294  Int_t pmtType = fSimulPar->getPmtType(fLoc[0],cell);
295  sigmaE = TMath::Sqrt(cdata->energy/1000.) * facEnergSmear[pmtType-1];
296  energy = gRandom->Gaus(cdata->energy,sigmaE);
297  }
298  if(!cdata->isEmbeddedReal) {
299  cal = new(cal) HEmcCalSim;
300  cal->setSector(fLoc[0]);
301  cal->setCell(cell);
302  Char_t row,col;
303  fEmcDet->getRowCol(cell,row,col);
304  cal->setRow(row);
305  cal->setColumn(col);
306  } else { // Embedded real hit
307  energy += cal->getEnergy();
308  sigmaE = TMath::Sqrt(sigmaE*sigmaE + cal->getSigmaEnergy()*cal->getSigmaEnergy());
309  }
310  // ------ energy -------------
311  cal->setEnergy(energy);
312  cal->setSigmaEnergy(sigmaE);
313  if(cal->getEnergy() < energyDepositCut) cal->setStatus(-1); // Energy deposit < threshold in this cell
314 
315  // ------ time and track numbers ------
316  // Take track number and time from track when sum. of energy deposit exceed threshold
317  cdata->sortTime();
318  Float_t energySum = 0.;
319  celltrack* celltr = NULL;
320  for(UInt_t k = 0; k < cdata->ctracks.size(); k++ ) {
321  celltr = cdata->ctracks[k];
322  energySum += celltr->trackEn;
323  if (energySum > energyDepositCut) break;
324  }
325  if(!cdata->isEmbeddedReal || gHades->getEmbeddingRealTrackId() != celltr->gtrack) {
326  cal->setTime(gRandom->Gaus(celltr->gtime,sigmaT) - startTimeSmearing);
327  cal->setTimeTrack(celltr->gtrack);
328  cal->setSigmaTime(sigmaT);
329  } // else keep time and sigma from real hit
330 
331  // ------ sort by enegry ---------------
332  cdata->sortEnergy();
333  for(UInt_t k = 0; k < cdata->ctracks.size() && k<5; k++ ) {
334  celltrack* celltr = cdata->ctracks[k];
335  cal->setTrack(celltr->gtrack, celltr->trackEn);
336  }
337 
338  cal->setNHits(1);
339  cal->setTotMult(cdata->ctracks.size());
340  }
341  }
342 
343  clearCellobjects(); // clear temporary data
344  return 0;
345 }
346 
348  // deletes objects in working array and sets pointer to 0
349  // the vector is still not cleared
350  for(UInt_t i = 0; i < cellobjects.size(); i++) {
351  if(cellobjects[i]) cellobjects[i]->reset();
352  }
353 }
354 
355 HGeantEmc* HEmcDigitizer::getInputHit(HGeantEmc* pGeantEmc,Int_t &inputTrack) const {
356  // Return pointer to the first HGeantEmc hit for track pGeantEmc->getTrack() or
357  // parent track if it has HGeantEmc hit in this cell
358  // Return inputTrack: geant track number of the parent track of pGeantEmc->getTrack()
359  // which first reach EMC(or RPC) in this sector first
360  Int_t track = pGeantEmc->getTrack();
361  inputTrack = track;
362  Int_t sec = pGeantEmc->getSector();
363  Int_t cell = pGeantEmc->getCell();
364  HGeantKine* kine = (HGeantKine*)fGeantKineCat->getObject(track-1);
365 
366  HGeantEmc* firstHitInCell = pGeantEmc;
367 
368  do {
369  track = kine->getTrack();
370  Int_t first = kine->getFirstEmcHit();
371  if(first != -1) {
372  // track is in EMC
373  kine->resetEmcIter();
374  HGeantEmc* gemc = NULL;
375  while( (gemc = (HGeantEmc*)kine->nextEmcHit()) != NULL) {
376  if(gemc->getSector() != sec) continue;
377  inputTrack = kine->getTrack();
378  if(gemc->getCell() == cell) {
379  Float_t peHit,xHit, yHit, zHit, tofHit, momHit, trackLength;
380  gemc->getHit(peHit, xHit, yHit, zHit, tofHit, momHit, trackLength);
381  if(peHit > 0.) {
382  firstHitInCell = gemc;
383  break; // Take the first hit
384  }
385  }
386  }
387  }
388  } while((kine = kine->getParent(track,fGeantKineCat)) != NULL);
389 
390  if(zVertBorder < 0.) {
391  // Test vertex of inputTrack and if it was borned in RPC region take parent track as inputTrack
392  HGeomVector ver;
393  kine = (HGeantKine*)fGeantKineCat->getObject(inputTrack-1);
394  while(kTRUE) {
395  kine->getVertex(ver);
396  ver = labTrans[sec].transTo(ver);
397  if(ver.getZ() < zVertBorder || ver.getZ()>0.) break;
398  kine = kine->getParent(inputTrack,fGeantKineCat);
399  if(kine == NULL) break;
400  inputTrack = kine->getTrack();
401  }
402  }
403 
404  return firstHitInCell;
405 }
virtual Bool_t addCategory(Cat_t aCat, HCategory *cat, Option_t opt[])=0
struct HEmcDigitizer::celltrack celltrack
static Int_t getPositionFromCell(Int_t cell)
static Int_t getEmbeddingRealTrackId()
Definition: hades.h:102
Double_t zVertBorder
Definition: hemcdigitizer.h:98
Int_t getEmbeddingMode()
Definition: hades.h:98
HCategory * fCalCat
Definition: hemcdigitizer.h:84
Int_t cellObjectsSize(void) const
HEmcSimulPar * fSimulPar
pointer to calibration parameters
Definition: hemcdigitizer.h:90
Float_t getSigmaEnergy(void) const
Definition: hemccalsim.h:44
Float_t getEnergy(void) const
Definition: hemccal.h:61
void getVertex(Float_t &ax, Float_t &ay, Float_t &az)
Definition: hgeantkine.cc:210
Double_t getZ() const
Definition: hgeomvector.h:24
vector< celltrack * > ctracks
Definition: hemcdigitizer.h:52
void setSigmaEnergy(Float_t e)
Definition: hemccalsim.h:35
void setTime(Float_t t)
Definition: hemccal.h:46
HModGeomPar * getModule(const Int_t, const Int_t)
Definition: hdetgeompar.cc:162
HEmcCellStatusPar * pStatuspar
Definition: hemcdigitizer.h:89
Int_t getEmbeddingDebug()
Definition: hades.h:100
const Cat_t catStart2Hit
Definition: hstartdef.h:8
Int_t embeddingmode
Definition: hemcdigitizer.h:79
void setNHits(UChar_t n)
Definition: hemccal.h:43
HEmcDetector * fEmcDet
Definition: hemcdigitizer.h:86
vector< Int_t > inputTracks
Definition: hemcdigitizer.h:53
void setEnergy(Float_t e)
Definition: hemccal.h:47
Float_t getSigmaEReal() const
Definition: hemcdigipar.h:24
Float_t phot2Energy[3]
Definition: hemcdigitizer.h:96
HRuntimeDb * getRuntimeDb(void)
Definition: hades.h:111
HEvent *& getCurrentEvent(void)
Definition: hades.cc:422
const Cat_t catGeantKine
Definition: hgeantdef.h:8
Float_t energyDepositCut
void setCell(UChar_t c)
Definition: hemccal.h:49
Float_t halfOfCellLength
Int_t getCellStatus(Int_t sec, Int_t cell)
Int_t n
Int_t cellObjectIndex(Int_t s, Int_t c) const
Bool_t reinit(void)
Int_t getSector(void)
Definition: hgeantemc.h:35
void clearCellobjects()
Float_t getSigmaT() const
Definition: hemcdigipar.h:20
void setSector(Char_t s)
Definition: hemccal.h:48
HCategory * fGeantEmcCat
Definition: hemcdigitizer.h:82
void setTimeTrack(Int_t tr)
Definition: hemccalsim.h:33
void setTotMult(Int_t n)
Definition: hemccalsim.h:32
HEmcDigiPar * fDigiPar
Definition: hemcdigitizer.h:88
void resetEmcIter(void)
Definition: hgeantkine.h:450
HCategory * fStartHitCat
Definition: hemcdigitizer.h:85
void setRow(Char_t r)
Definition: hemccal.h:50
Int_t getPmtType(Int_t sec, Int_t cell)
Definition: hemcsimulpar.h:28
HSpectrometer * getSetup(void)
Definition: hades.h:112
HDetector * getDetector(const Char_t *name)
Int_t sectorFromIndex(Int_t ind) const
HGeomTransform labTrans[6]
Definition: hemcdigitizer.h:92
Float_t getResolution(void) const
Definition: hstart2hit.h:53
Hades * gHades
Definition: hades.cc:1213
Float_t getSigmaEReal2() const
Definition: hemcdigipar.h:25
const Cat_t catEmcGeantRaw
Definition: hgeantdef.h:16
HIterator * iterGeantEmc
Definition: hemcdigitizer.h:91
Float_t getTime(void) const
Definition: hemccal.h:60
Float_t facEnergSmear[3]
Definition: hemcdigitizer.h:97
HParSet * getContainer(const Text_t *)
Definition: hruntimedb.cc:124
HLocation fLoc
Definition: hemcdigitizer.h:81
Int_t getFirstEmcHit()
Definition: hgeantkine.h:191
void getHit(Float_t &ae, Float_t &ax, Float_t &ay, Float_t &az, Float_t &atof, Float_t &amom, Float_t &alen)
Definition: hgeantemc.cc:69
Bool_t init(void)
void setStatus(Int_t f)
Definition: hemccalsim.h:34
void setColumn(Char_t c)
Definition: hemccal.h:51
Float_t getPhot2E() const
Definition: hemcdigipar.h:21
Float_t getSigmaEIntern() const
Definition: hemcdigipar.h:23
Bool_t setParameterContainers(void)
Int_t execute(void)
UChar_t getCell(void) const
Definition: hemccal.h:63
vector< celldata * > cellobjects
Definition: hemcdigitizer.h:74
HEmcGeomPar * fGeomPar
Definition: hemcdigitizer.h:87
Int_t cellFromIndex(Int_t ind) const
HLinearCategory * fGeantKineCat
Definition: hemcdigitizer.h:83
HCategory * buildMatrixCategory(const Text_t *, Float_t)
Definition: hemcdetector.cc:80
const Int_t emcMaxComponents
Definition: emcdef.h:15
HLinkedDataObject * nextEmcHit()
Definition: hgeantkine.h:405
void setTrack(Int_t trackNumber, Float_t energy)
Float_t getPhot2E2() const
Definition: hemcdigipar.h:22
const Cat_t catEmcCal
Definition: emcdef.h:8
virtual HCategory * getCategory(Cat_t aCat)=0
Int_t getTrack(void)
Definition: hgeantemc.h:33
Int_t getCell(void)
Definition: hgeantemc.h:36
HGeomTransform & getLabTransform()
Definition: hdetgeompar.h:21
void initVariables(void)
Int_t getTrack(void) const
Definition: hgeantkine.h:104
Float_t signalVelocity
ClassImp(HEmcDigitizer) HEmcDigitizer
~HEmcDigitizer(void)
Char_t getSector(void) const
Definition: hemccal.h:62
Float_t sigmaT
Definition: hemcdigitizer.h:95
static HGeantKine * getParent(Int_t track, HLinearCategory *cat=NULL)
Definition: hgeantkine.cc:1857
HGeomVector transTo(const HGeomVector &p) const
HEmcDigitizer(void)
void setSigmaTime(Float_t t)
Definition: hemccalsim.h:36
static void getRowCol(const Int_t cell, Char_t &row, Char_t &col)
HGeantEmc * getInputHit(HGeantEmc *pGeantEmc, Int_t &inputTrack) const