FairRoot/PandaRoot
PndEmcHitProducer.cxx
Go to the documentation of this file.
1 //
3 // PndEmcHitProducer
4 //
5 // Filler of PndEmcHit
6 //
7 // Created 14/08/06 by S.Spataro
8 //
10 
11 #include "PndEmcHitProducer.h"
12 
13 #include "PndEmcStructure.h"
14 #include "PndEmcHit.h"
15 #include "PndEmcPoint.h"
16 #include "PndEmcGeoPar.h"
17 #include "PndEmcDigiPar.h"
19 #include "PndMCTrack.h"
20 
21 #include "PndEmcXtal.h"
22 
23 #include "FairRootManager.h"
24 #include "FairRunAna.h"
25 #include "FairRuntimeDb.h"
26 #include "FairDetector.h"
27 #include "FairRun.h"
28 #include "FairRuntimeDb.h"
29 
30 #include "TClonesArray.h"
31 #include "TROOT.h"
32 #include "TGeoVolume.h"
33 #include "TGeoMatrix.h"
34 #include "TGeoManager.h"
35 #include "TVector3.h"
36 #include "TSystem.h"
37 #include "TString.h"
38 
39 static Int_t HowManyPoints = 0;
40 static Int_t HowManyHitsAll = 0;
41 static Int_t HowManyHitsAboveThreshold = 0;
42 
43 // ----- Default constructor -------------------------------------------
45  PndPersistencyTask("Ideal EMC hit Producer"),
46  fUse_nonuniformity(0), fNonuniformityFile(""), fPointArray(), fMCTrackArray(), fHitArray(), fVolumeArray(), fMapVersion(0), fEnergyThreshold(0), emcX(), emcY(), emcZ(), fEmcStr(), fMapper(), fDigiPar(), fGeoPar(), fNonuniformityPar(), fDayOne(false)
47 {
48  fNonuniformityFile=gSystem->Getenv("VMCWORKDIR");
49  fNonuniformityFile+="/input/EmcDigiNoniformityPars.root";
50  SetPersistency(kTRUE);
51 }
52 // -------------------------------------------------------------------------
53 
55  PndPersistencyTask("Ideal EMC hit Producer"),
56  fUse_nonuniformity(0), fNonuniformityFile(""), fPointArray(), fMCTrackArray(), fHitArray(), fVolumeArray(), fMapVersion(0), fEnergyThreshold(0), emcX(), emcY(), emcZ(), fEmcStr(), fMapper(), fDigiPar(), fGeoPar(), fNonuniformityPar(), fDayOne(false)
57 {
58  fNonuniformityFile=gSystem->Getenv("VMCWORKDIR");
59  fNonuniformityFile+="/input/EmcDigiNoniformityPars.root";
60  SetPersistency(val);
61 }
62 
63 // ----- Destructor ----------------------------------------------------
65 // -------------------------------------------------------------------------
66 
67 
68 // ----- Public method Init --------------------------------------------
70 
71  cout << " -I- PndEmcHitProducer INITIALIZATION *********************" << endl;
72 
73  //FairDetector::Initialize();
74  //FairRun* sim = FairRun::Instance();
75  //FairRuntimeDb* rtdb=sim->GetRuntimeDb();
76 
77  // Get RootManager
78  FairRootManager* ioman = FairRootManager::Instance();
79  if (! ioman ){
80  cout << "-E- PndEmcHitProducer::Init: "
81  << "RootManager not instantiated!" << endl;
82  return kFATAL;
83  }
84 
85  // Get input array
86  fPointArray = (TClonesArray*) ioman->GetObject("EmcPoint");
87  if (! fPointArray ){
88  cout << "-W- PndEmcHitProducer::Init: "
89  << "No EmcPoint array!" << endl;
90  return kERROR;
91  }
92 
93  // Get input array
94  fMCTrackArray = (TClonesArray*) ioman->GetObject("MCTrack");
95  if (! fMCTrackArray ){
96  cout << "-W- PndEmcMakeCluster::Init: "
97  << "No MCTrack array! Needed for MC Truth" << endl;
98  //return kERROR;
99  }
100 
101  // Create and register output array
102  fHitArray = new TClonesArray("PndEmcHit");
103 
104  ioman->Register("EmcHit","Emc",fHitArray, GetPersistency());
105 
109 
110  emcX=fEmcStr->GetEmcX();
111  emcY=fEmcStr->GetEmcY();
112  emcZ=fEmcStr->GetEmcZ();;
113 
116 
117  if(fUse_nonuniformity){
118  cout << "-I- PndEmcHitProducer: Using nonuniform lightoutput" << endl;
119  }
120  if(fUse_nonuniformity && fNonuniformityFile.Length()>0){
121  TFile *nonuniformityfile = new TFile(fNonuniformityFile);
122  if(nonuniformityfile==NULL){
123  cout << "-E- PndEmcHitProducer: Could not open file " << fNonuniformityFile.Data() << " for Nonuniformity Information" << endl;
124  } else {
125  PndEmcDigiNonuniParObject *parObject;
126  nonuniformityfile->GetObject("PndEmcDigiNonuniParObject",parObject);
127  if(parObject == NULL){
128  cout << "-E- PndEmcHitProducer: Could not get Nonuniformity information from file " << fNonuniformityFile.Data() << endl;
129  } else {
131  }
132  }
133  }
134 
135 
136  printf("HitProducer has EnergyHitThreshold of %f GeV and Use_nonuniformity %i\n", fEnergyThreshold, fUse_nonuniformity);
137  cout << "-I- PndEmcHitProducer: Intialization successfull" << endl;
138 
139  return kSUCCESS;
140 }
141 
143 
144  // Get run and runtime database
145  FairRun* run = FairRun::Instance();
146  if (! run ) Fatal("SetParContainers", "No analysis run");
147 
148  FairRuntimeDb* db = run->GetRuntimeDb();
149  if (! db ) Fatal("SetParContainers", "No runtime database");
150 
151  // Get Emc geometry parameter container
152  fGeoPar = (PndEmcGeoPar*) db->getContainer("PndEmcGeoPar");
153 
154  // Get Emc digitisation parameter container
155  fDigiPar = (PndEmcDigiPar*) db->getContainer("PndEmcDigiPar");
156 
157  fNonuniformityPar = (PndEmcDigiNonuniformityPar*) db->getContainer("PndEmcDigiNonuniformityPar");
158 
159  fDigiPar->setChanged();
160  fDigiPar->setInputVersion(run->GetRunId(),1);
161 
162  fNonuniformityPar->setChanged();
163  fNonuniformityPar->setInputVersion(run->GetRunId(),1);
164 }
165 
166 // -------------------------------------------------------------------------
167 
168 // Helper function, does not depend on class, identical to the one in PndEmcMakeCluster
169 void PndEmcHitProducer::cleansortmclist( std::vector <Int_t> &newlist,TClonesArray* mcTrackArray)
170 {
171  std::vector <Int_t> tmplist;
172  std::vector <Int_t> tmplist2;
173  // Sort list...
174  std::sort( newlist.begin(), newlist.end());
175  // and copy every id only once (even so it might be in the list several times)
176  std::unique_copy( newlist.begin(), newlist.end(), std::back_inserter( tmplist ) );
177 
178  // Now check if mother or (grand)^x-mother are already in the list
179  // (which means i am a secondary)... if so, remove myself
180  for(Int_t j=tmplist.size()-1; j>=0; j--){
181  bool flag = false;
182  PndMCTrack *pt;
183  Int_t id = tmplist[j];
184  // if -1 index put it in the list and continue
185  if(id < 0) {
186  tmplist2.push_back(id);
187  continue;
188  }
189  while(!flag){
190  pt=((PndMCTrack*)mcTrackArray->At(id));
191  // If the particle is primary store it and stop
192  if(pt->GetMotherID()<0) {
193  tmplist2.push_back(id);
194  break;
195  }
196  // Stop when it finds the first MCTrack not produced in emc
197  TString node = gGeoManager->FindNode(pt->GetStartVertex().X(),pt->GetStartVertex().Y(),pt->GetStartVertex().Z())->GetName();
198  if ( !(node.BeginsWith("emc") || node.BeginsWith("CrystalVol") || node.BeginsWith("Fsc") ) ) {
199  tmplist2.push_back(id);
200  break;
201  }
202  // not exactly clear this part of the code, it needs to be checked (SS, 18/03/2015)
203  id = pt->GetMotherID();
204  for(Int_t k=j-1; k>=0; k--){
205  if(tmplist[k]==id){
206  tmplist.erase(tmplist.begin()+j);
207  flag=true;
208  break;
209  }
210  }
211  }
212  }
213  newlist.clear();
214  std::unique_copy( tmplist2.begin(), tmplist2.end(), std::back_inserter( newlist) );
215 }
216 
217 // ----- Public method Exec --------------------------------------------
218 void PndEmcHitProducer::Exec(Option_t*)
219 {
220  if (fVerbose>1) cout << " -I- PndEmcHitProducer POINT EXECUTION *********************" << endl;
221  // Reset output array
222  if (! fHitArray ) Fatal("Exec", "No DigiArray");
223 
224  fHitArray->Delete();
225 
226  // Declare some variables
227  //PndEmcPoint* point = NULL;
228  Int_t DetId;
229 
230  fTrackEnergy.clear();
231  fTrackTime.clear();
232  fTrackMcTruth.clear();
233  fPointMatch.clear();
234  fTrackEntering.clear();
235  fTrackExiting.clear();
236 
237  map<Int_t, Float_t>::const_iterator p;
238 
239  std::vector<PndEmcPoint*> fPointList;// to pass to EmcHit
240  const PndEmcTciXtalMap &XtalMap = fEmcStr->GetTciXtalMap();
241  TVector3 frontvec;
242  TVector3 normvec;
243  TVector3 pointvec;
244  TVector3 distvec;
245  Double_t zpos;
246  Double_t energyscalefactor=1.0;
247  Double_t c[3];
248  PndEmcXtal *tmpXtal;
249  PndEmcTwoCoordIndex *tmpTCI;
250  // Loop over EmcPoints
251  Int_t nPoints = fPointArray->GetEntriesFast();
252 
253  Double_t point_time = 0.00;
254  //------- init containers ---
255 
256  for (Int_t iPoint = 0; iPoint < nPoints; iPoint++){
257  PndEmcPoint* point = (PndEmcPoint*) fPointArray->At(iPoint);
258  if ( ! AcceptDayOne(point) ) continue;
259  fTrackEnergy[point->GetDetectorID()] = 0.00;
260  fTrackTime [point->GetDetectorID()] = std::numeric_limits<float>::max();
261  }
262 
263  //----------------------------
264 
265  //Int_t Counter[3] = {0};
266  HowManyPoints += nPoints;
267 
268  for (Int_t iPoint=0; iPoint<nPoints; iPoint++)
269  {
270  PndEmcPoint* point = (PndEmcPoint*) fPointArray->At(iPoint);
271  if ( ! AcceptDayOne(point) ) continue;
272  DetId = point->GetDetectorID();
273 
274  if (point->GetEntering()){
275  fTrackEntering[DetId].AddLinks(point->GetLinksWithType(FairRootManager::Instance()->GetBranchId("MCTrack")));
276  }
277  if (point->GetExiting()){
278  fTrackExiting[DetId].AddLinks(point->GetLinksWithType(FairRootManager::Instance()->GetBranchId("MCTrack")));
279  }
280  if(point->GetEnergyLoss() == 0 ) continue;
281  if(point->GetModule() == 10 )
282  {
283  cout << " -I- PndEmcHitProducer::Exec" << "\t" << "Skipping Module 10 (FscFiber)" << endl;
284  continue;
285  }
286  //if(point->GetTrackID() < 0)
287  // std::cout<<"negative track id #"<<point->GetTrackID()<<std::endl;
288 
289  //std::cout<<"point belongs to track #"<<point->GetTrackID()<<std::endl;
290  //if(point->GetTrackID() == 0) ++Counter[0];
291  //if(point->GetTrackID() == 1) ++Counter[1];
292  //if(point->GetTrackID() == 2) ++Counter[2];
293 
294  if(fUse_nonuniformity !=0 ){
295  //light output is z-dependent, so calculate z
296 
297  tmpTCI = fMapper->GetTCI(DetId);
298  if(tmpTCI == NULL){
299  printf("no TCI found for DetectorID %d\n",DetId);
300  continue;
301  }
302  tmpXtal =XtalMap.find(tmpTCI)->second;
303  point->Position(pointvec);
304  frontvec = tmpXtal->frontCentre();
305  normvec = tmpXtal->normalToFrontFace();
306  distvec = pointvec-frontvec;
307  zpos = distvec.Dot(normvec);
309  energyscalefactor=c[0]+zpos*(c[1]+zpos*c[2]);
310  fTrackEnergy[DetId] += point->GetEnergyLoss() * energyscalefactor;
311  fPointMatch[DetId].push_back(iPoint);
312  // printf("point with detID %d has z Position %f and energyloss %f scaled with %f\n",DetId,zpos, point->GetEnergyLoss(),energyscalefactor);
313  // printf("front is at x: %f y: %f z: %f\n", frontvec.X(),frontvec.Y(),frontvec.Z());
314  } else {
315  fTrackEnergy[DetId] += point->GetEnergyLoss();
316  fPointMatch[DetId].push_back(iPoint);
317  // printf("point with detID %d has z Position %f and energyloss %f not scaled\n",DetId,zpos, point->GetEnergyLoss());
318  }
319  point_time=point->GetTime();
320 
321  if (point_time < fTrackTime[point->GetDetectorID()]){
322  fTrackTime[point->GetDetectorID()] = point_time;
323  }
324 
325  // Check and save MC truth information
326  // Eloss==0 tracks are only stored in point, if track is entering detector from outside
327  // and thats what we are interested in...
328  //std::cout<<"track id #"<<point->GetTrackID()<<", Energyloss #"<<point->GetEnergyLoss()<<endl;
329  if(point->GetEnergyLoss() >0)
330  (fTrackMcTruth[point->GetDetectorID()]).push_back(point->GetTrackID());
331  //if(point->GetEnergyLoss() == 0 ){
332  // cout << "ELoss== 0 : " <<point->GetEnergyLoss()<<", ID "<<
333  // point->GetTrackID()<<","<<point->GetDetectorID()<<","<<point->GetXPad()<<","<<point->GetYPad()<<endl;
334  //}else{
335  // cout << "ELoss>0 : " <<point->GetEnergyLoss()<<", ID "<<
336  // point->GetTrackID()<<","<<point->GetDetectorID()<<","<<point->GetXPad()<<","<<point->GetYPad()<<endl;
337  //}
338  }
339 
340  // Loop over EmcPoint
341 
342  // Loop to register EmcHit
343  Int_t idx = 0;
344  for( p = fTrackEnergy.begin(); p != fTrackEnergy.end(); ++p){
345  ++HowManyHitsAll;
346  ++idx;
347  if ((*p).second > fEnergyThreshold){
349  // Check and save MC truth information B.S.
350  // remove MC Truth particles which are not needed (eg grand^x-daugherts)
351  if( fMCTrackArray){
352  std::vector<Int_t>& plist = fTrackMcTruth[(*p).first];
354  //std::cout<<"The "<<(idx)<<" hit produced by track #";
355  //for(Int_t ip=0;ip<plist.size();++ip){
356  // cout<<plist[ip]<<", ";
357  //}
358  //cout<<endl;
359  }
360  //std::vector<Int_t>& track = fTrackMcTruth[(*p).first];
361  //std::cout<<track.size()<<" tracks contributes energy to this hit, ";
362  //for(size_t i=0;i<track.size();++i){
363  // if(i < track.size() - 1){
364  // std::cout<<track[i]<<", ";
365  // }else{
366  // std::cout<<track[i];
367  // }
368  //}
369  //std::cout<<std::endl;
370  AddHit(1, (*p).first, (*p).second, fTrackTime[(*p).first], fTrackMcTruth[(*p).first], fTrackEntering[(*p).first], fTrackExiting[(*p).first]);
371  //myHit->AddLinks(FairMultiLinkedData("EmcPoint", fPointMatch[p->first]));
372  //myHit->AddLinks(FairMultiLinkedData("MCTrack", fTrackMcTruth[(*p).first));
373  }
374  }
375  //check
376  //Int_t nTrack = fMCTrackArray->GetEntriesFast();
377  //for(Int_t itrack = 0; itrack < nTrack; ++itrack){
378  // PndMCTrack* pt1 =((PndMCTrack*)fMCTrackArray->At(itrack));
379  // if(pt1->IsGeneratorCreated()){
380  // pt1->Print(itrack);
381  // }
382  //}
383 
384 
385 }
386 
387 // -------------------------------------------------------------------------
388 // ----- Private method AddDigi --------------------------------------------
389 PndEmcHit* PndEmcHitProducer::AddHit(Int_t trackID,Int_t detID, Float_t energy,
390  Float_t time, std::vector <Int_t> &mctruth, FairMultiLinkedData entering, FairMultiLinkedData exiting)
391 {
392  // It fills the PndEmcHit category
393 
394  //cout << "PndEmcHitProducer: track " << trackID << " evt " << eventID
395  //<< " sec " << sec << " plane " << pla << " strip " << strip << "box
396  //" << box << " tube " << tub << endl;
397  TClonesArray& clref = *fHitArray;
398  Int_t size = clref.GetEntriesFast();
399  PndEmcHit* hit = new(clref[size]) PndEmcHit(trackID, detID, energy, time, emcX[detID],
400  emcY[detID], emcZ[detID], mctruth, entering, exiting);
401  //hit->Print();
402  return hit;
403 }
404 // ----
405 
407 {
408  fStoreHits=val;
409  return;
410 }
411 
413 {
414  std::cout<<"========================================================="<<std::endl;
415  std::cout<<"PndEmcHitProducer::FinishTask"<<std::endl;
416  std::cout<<"*********************************************************"<<std::endl;
417  if(fDayOne) std::cout<<" DAY 1 Setup active, only 12/16 Slices available "<<std::endl;
418  std::cout<<"Read points # "<<HowManyPoints<<std::endl;
419  std::cout<<"Produc hits# "<<HowManyHitsAll<<", threshold# "<<fEnergyThreshold<<std::endl;
420  std::cout<<"Hits above threshhod#"<<HowManyHitsAboveThreshold<<std::endl;
421  std::cout<<"*********************************************************"<<std::endl;
422 }
423 
425 {
426  if(!fDayOne) return true;
427  if(p->GetDetectorID()>250e6) return true; // afaik this is convention for the barrel
428  float phi=p->GetPhi();
429  if(abs(phi-90)<22.5) return false;
430  if(abs(phi-270)<22.5) return false;
431  if(abs(phi+90)<22.5) return false;
432  return true;
433 }
434 
435 
represents a mc hit in an emc crystal
Definition: PndEmcPoint.h:19
Double_t p
Definition: anasim.C:58
int fVerbose
Definition: poormantracks.C:24
printf("RealTime=%f seconds, CpuTime=%f seconds\n", rtime, ctime)
Int_t run
Definition: autocutx.C:47
PndEmcDigiPar * fDigiPar
Short_t GetModule() const
Definition: PndEmcPoint.h:60
Double_t val[nBoxes][nFEBox]
Definition: createCalib.C:11
map< Int_t, FairMultiLinkedData > fTrackExiting
PndEmcStructure * fEmcStr
creates PndEmcHits from PndEmcPoints
labels push_back("electron")
TClonesArray * fMCTrackArray
const TVector3 & frontCentre() const
Definition: PndEmcXtal.cxx:145
represents coordinates of one crystal
Definition: PndEmcXtal.h:36
static Int_t HowManyPoints
void SetPersistency(Bool_t val=kTRUE)
Int_t GetUse_nonuniformity()
Definition: PndEmcDigiPar.h:51
stores crystal index coordinates (x,y) or (theta,phi)
PndEmcTwoCoordIndex * GetTCI(Int_t DetectorId)
friend F32vec4 max(const F32vec4 &a, const F32vec4 &b)
Definition: P4_F32vec4.h:26
TGeoManager * gGeoManager
TClonesArray * fHitArray
map< Int_t, Float_t > fTrackEnergy
Bool_t GetExiting() const
Definition: PndEmcPoint.h:65
void InitEmcMapper()
const mapper & GetEmcY() const
TString pt(TString pts, TString exts="px py pz")
Definition: invexp.C:133
int idx[MAX]
Definition: autocutx.C:38
const TVector3 & normalToFrontFace() const
Definition: PndEmcXtal.cxx:151
void SetStorageOfData(Bool_t val)
Double_t GetPhi() const
Definition: PndEmcPoint.h:56
virtual void SetParContainers()
Double_t
static Int_t HowManyHitsAll
virtual void Exec(Option_t *opt)
const Double_t zpos
parameter set of Emc digitisation
Definition: PndEmcDigiPar.h:12
static Int_t HowManyHitsAboveThreshold
const PndEmcTciXtalMap & GetTciXtalMap() const
Double_t GetEnergyHitThreshold()
Definition: PndEmcDigiPar.h:15
map< Int_t, FairMultiLinkedData > fTrackEntering
void cleansortmclist(std::vector< Int_t > &newlist, TClonesArray *mcTrackArray)
represents the deposited energy of one emc crystal from simulation
Definition: PndEmcHit.h:26
bool AcceptDayOne(PndEmcPoint *p)
map< Int_t, std::vector< Int_t > > fTrackMcTruth
TClonesArray * fPointArray
Bool_t GetEntering() const
Definition: PndEmcPoint.h:64
virtual InitStatus Init()
PndEmcHit * AddHit(Int_t trackID, Int_t detID, Float_t energy, Float_t time, std::vector< Int_t > &mctruth, FairMultiLinkedData entering, FairMultiLinkedData exiting)
PndEmcDigiNonuniformityPar * fNonuniformityPar
std::map< PndEmcTwoCoordIndex *, PndEmcXtal * > PndEmcTciXtalMap
PndEmcGeoPar * fGeoPar
void SetNonuniParObject(PndEmcDigiNonuniParObject *ParObject)
ClassImp(PndAnaContFact)
map< Int_t, Float_t > fTrackTime
PndSdsMCPoint * hit
Definition: anasim.C:70
map< Int_t, std::vector< Int_t > > fPointMatch
TVector3 GetStartVertex() const
Definition: PndMCTrack.h:76
static PndEmcStructure * Instance()
Int_t GetMotherID() const
Definition: PndMCTrack.h:74
PndEmcMapper * fMapper
static PndEmcMapper * Instance()
void GetNonuniformityParameters(Int_t DetId, Double_t *pars)
const mapper & GetEmcZ() const
const mapper & GetEmcX() const
Double_t energy
Definition: plot_dirc.C:15
PndSdsMCPoint * point
Definition: anaLmdCluster.C:72