Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DEventProcessor_mc_tree.cc
Go to the documentation of this file.
1 /*
2  * DEventProcessor_mc_tree.cc
3  *
4  * Created on: Aug 1, 2012
5  * Author: yqiang
6  *
7  * Modified on: Oct 10 2012, with full Cherenkov support
8  * Oct 7, 2013, yqiang, modified RithTruthHit
9  */
10 
12 
13 /*
14  #define MIN_CDC_HITS 8
15  #define MIN_FDC_HITS 8
16  #define MIN_CDC_FDC_HITS 8
17  #define MIN_BCAL_HITS 4
18  #define MIN_FCAL_HITS 4
19  #define MIN_UPV_HITS 4
20  #define MIN_TOF_HITS 1
21  */
22 
23 // Routine used to create our DEventProcessor
24 extern "C" {
25 void InitPlugin(JApplication *app) {
26  InitJANAPlugin(app);
27  app->AddProcessor(new DEventProcessor_mc_tree());
28 }
29 } // "C"
30 
31 //------------------
32 // DEventProcessor_mc_tree
33 //------------------
35  tree_thrown = NULL;
36  evt_thrown = NULL;
37 }
38 
39 //------------------
40 // ~DEventProcessor_mc_tree
41 //------------------
43 }
44 
45 //------------------
46 // init
47 //------------------
49  // create directory
50  TDirectory *dir = new TDirectoryFile("DMC", "DMC");
51  dir->cd();
52 
53  // define tree
54  tree_thrown = new TTree("thrown", "tree for thrown events");
55  evt_thrown = new Event();
56 
57  // create branches
58  tree_thrown->Branch("T", &evt_thrown);
59 
60  dir->cd("../");
61 
62  return NOERROR;
63 }
64 
65 //------------------
66 // evnt
67 //------------------
68 jerror_t DEventProcessor_mc_tree::evnt(JEventLoop *loop, uint64_t eventnumber) {
69  vector<const DBeamPhoton*> beam_photons;
70  vector<const DMCThrown*> mcthrowns;
71  vector<const DMCTrackHit*> mctrackhits;
72  vector<const DRichHit*> richhits;
73  vector<const DCereHit*> cerehits;
74  vector<const DRichTruthHit*> richtruthhits;
75 
76  loop->Get(beam_photons);
77  loop->Get(mcthrowns);
78  loop->Get(mctrackhits);
79  loop->Get(richhits);
80  loop->Get(cerehits);
81  loop->Get(richtruthhits);
82 
83  TVector3 VertexGen = TVector3(mcthrowns[0]->position().X(),
84  mcthrowns[0]->position().Y(), mcthrowns[0]->position().Z());
85  // Make Particle object for beam photon
86  TLorentzVector beam_photon(0.0, 0.0, 9.0, 9.0);
87  if (beam_photons.size() > 0) {
88  const DLorentzVector &lv = beam_photons[0]->lorentzMomentum();
89  beam_photon.SetPxPyPzE(lv.Px(), lv.Py(), lv.Pz(), lv.E());
90  }
91 
92  // Target is proton at rest in lab frame
93  TLorentzVector target(0.0, 0.0, 0.0, 0.93827);
94 
95  /*
96  // Count FDC anode hits
97  vector<const DFDCHit*> fdchits;
98  loop->Get(fdchits);
99  unsigned int Nfdc_anode = 0;
100  for(unsigned int i=0; i<fdchits.size(); i++){
101  if(fdchits[i]->type==0){
102  Nfdc_anode ++;
103  }
104  }
105 
106  // Count CDC hits
107  vector<const DCDCTrackHit*> cdctrackhits;
108  loop->Get(cdctrackhits);
109  unsigned int Ncdc_anode = 0;
110  Ncdc_anode = cdctrackhits.size();
111  */
112 
113  // Initialize particle set
114  particle_set thr;
115 
116  // Loop over track hits
117  // map first: DMCTrack index+1
118  // map second: number of hits
119  map<int, int> cdchits;
120  map<int, int> fdchits;
121  map<int, int> bcalhits;
122  map<int, int> fcalhits;
123  map<int, int> upvhits;
124  map<int, int> tofhits;
125  // add truth points from RICH and Cere
126  map<int, int> richpoints;
127  map<int, int> cerepoints;
128 
129  for (unsigned int i = 0; i < mctrackhits.size(); i++) {
130  const DMCTrackHit *mctrackhit = mctrackhits[i];
131 
132  switch (mctrackhit->system) {
133  case SYS_CDC:
134  if (mctrackhit->primary)
135  cdchits[mctrackhit->track]++;
136  break;
137  case SYS_FDC:
138  if (mctrackhit->primary)
139  fdchits[mctrackhit->track]++;
140  break;
141  case SYS_BCAL:
142  bcalhits[mctrackhit->track]++;
143  break;
144  case SYS_FCAL:
145  fcalhits[mctrackhit->track]++;
146  break;
147  case SYS_UPV:
148  upvhits[mctrackhit->track]++;
149  break;
150  case SYS_TOF:
151  tofhits[mctrackhit->track]++;
152  break;
153  case SYS_RICH:
154  richpoints[mctrackhit->track]++;
155  break;
156  case SYS_CHERENKOV:
157  cerepoints[mctrackhit->track]++;
158  break;
159  default:
160  break;
161  }
162  }
163 
164  // Create Particle objects for thrown particles
165  bool all_fiducial = true;
166 
167  for (unsigned int j = 0; j < mcthrowns.size(); j++) {
168 
169  // find hits
170  hit_set hits;
171  if (cdchits.find(j + 1) != cdchits.end())
172  hits.hits_cdc = cdchits.find(j + 1)->second;
173  else
174  hits.hits_cdc = 0;
175  if (fdchits.find(j + 1) != fdchits.end())
176  hits.hits_fdc = fdchits.find(j + 1)->second;
177  else
178  hits.hits_fdc = 0;
179  if (bcalhits.find(j + 1) != bcalhits.end())
180  hits.hits_bcal = bcalhits.find(j + 1)->second;
181  else
182  hits.hits_bcal = 0;
183  if (fcalhits.find(j + 1) != fcalhits.end())
184  hits.hits_fcal = fcalhits.find(j + 1)->second;
185  else
186  hits.hits_fcal = 0;
187  if (upvhits.find(j + 1) != upvhits.end())
188  hits.hits_upv = upvhits.find(j + 1)->second;
189  else
190  hits.hits_upv = 0;
191  if (tofhits.find(j + 1) != tofhits.end())
192  hits.hits_tof = tofhits.find(j + 1)->second;
193  else
194  hits.hits_tof = 0;
195  if (richpoints.find(j + 1) != richpoints.end())
196  hits.hits_rich = richpoints.find(j + 1)->second;
197  else
198  hits.hits_rich = 0;
199  if (cerepoints.find(j + 1) != cerepoints.end())
200  hits.hits_cere = cerepoints.find(j + 1)->second;
201  else
202  hits.hits_cere = 0;
203 
204  switch (mcthrowns[j]->type) {
205  case Gamma: // photons
206  thr.photons.push_back(
207  MakeParticle((DKinematicData*) mcthrowns[j], 0.0, hits));
208  all_fiducial &= IsFiducial(mcthrowns[j]);
209  break;
210  case PiPlus: // piplus
211  thr.piplus.push_back(
212  MakeParticle((DKinematicData*) mcthrowns[j],
213  ParticleMass(PiPlus), hits));
214  all_fiducial &= IsFiducial(mcthrowns[j]);
215  break;
216  case PiMinus: // piminus
217  thr.piminus.push_back(
218  MakeParticle((DKinematicData*) mcthrowns[j],
219  ParticleMass(PiMinus), hits));
220  all_fiducial &= IsFiducial(mcthrowns[j]);
221  break;
222  case KPlus: // Kplus
223  thr.Kplus.push_back(
224  MakeParticle((DKinematicData*) mcthrowns[j],
225  ParticleMass(KPlus), hits));
226  all_fiducial &= IsFiducial(mcthrowns[j]);
227  break;
228  case KMinus: // Kminus
229  thr.Kminus.push_back(
230  MakeParticle((DKinematicData*) mcthrowns[j],
231  ParticleMass(KMinus), hits));
232  all_fiducial &= IsFiducial(mcthrowns[j]);
233  break;
234  case Neutron: // neutrons
235  thr.neutrons.push_back(
236  MakeParticle((DKinematicData*) mcthrowns[j],
237  ParticleMass(Neutron), hits));
238  all_fiducial &= IsFiducial(mcthrowns[j]);
239  break;
240  case Proton: // protons
241  thr.protons.push_back(
242  MakeParticle((DKinematicData*) mcthrowns[j],
243  ParticleMass(Proton), hits));
244  all_fiducial &= IsFiducial(mcthrowns[j]);
245  break;
246  case Electron: // electrons
247  thr.electrons.push_back(
248  MakeParticle((DKinematicData*) mcthrowns[j],
249  ParticleMass(Electron), hits));
250  all_fiducial &= IsFiducial(mcthrowns[j]);
251  break;
252  case Positron: // positrons
253  thr.positrons.push_back(
254  MakeParticle((DKinematicData*) mcthrowns[j],
255  ParticleMass(Positron), hits));
256  all_fiducial &= IsFiducial(mcthrowns[j]);
257  break;
258  default:
259  break;
260  }
261  }
262  /*
263  * add RICH hits to MC particle set (for now), yqiang
264  */
265  for (unsigned int j = 0; j < richhits.size(); j++)
266  thr.richhits.push_back(MakeRichHit((DRichHit*) richhits[j]));
267  // add cere hits, yqiang Oct 11 2012
268  for (unsigned int j = 0; j < cerehits.size(); j++)
269  thr.cerehits.push_back(MakeCereHit((DCereHit*) cerehits[j]));
270  // add RICH truth hit, yqiang Oct 8, 2013
271  for (unsigned int j = 0; j < richtruthhits.size(); j++)
272  thr.richtruthhits.push_back(
273  MakeRichTruthHit((DRichTruthHit*) richtruthhits[j]));
274 
275  // Although we are only filling objects local to this plugin, TTree::Fill() periodically writes to file: Global ROOT lock
276  japp->RootWriteLock(); //ACQUIRE ROOT LOCK
277 
278  // Fill in Event objects
279  evt_thrown->Clear();
280  FillEvent(evt_thrown, thr);
281 
282  // Copy fiducial
283  evt_thrown->all_fiducial = all_fiducial;
284 
285  // Copy reaction vectors
286  evt_thrown->beam = beam_photon;
287  evt_thrown->target = target;
288  evt_thrown->vertex = VertexGen;
289 
290  // Copy event number
291  evt_thrown->event = eventnumber;
292  tree_thrown->Fill();
293 
294  japp->RootUnLock(); //RELEASE ROOT LOCK
295 
296  return NOERROR;
297 }
298 
299 /*
300  * Make RICH hits
301  */
303 
304  double x = rhit->x;
305  double y = rhit->y;
306  double z = rhit->z;
307 
308  RichHit hit;
309  hit.t = rhit->t;
310  hit.x.SetXYZ(x, y, z);
311 
312  return hit;
313 }
314 /*
315  * Make Cherenkov hits
316  */
318 
319  CereHit hit;
320  hit.sector = chit->sector;
321  hit.pe = chit->pe;
322  hit.t = chit->t;
323 
324  return hit;
325 }
326 
327 /*
328  * Make RICH Truth hits, added by yqiang Oct 7, 2013
329  */
331  const DRichTruthHit *rthit) {
332 
333  double x = rthit->x;
334  double y = rthit->y;
335  double z = rthit->z;
336  double px = rthit->px;
337  double py = rthit->py;
338  double pz = rthit->pz;
339  RichTruthHit hit;
340  hit.x.SetXYZ(x, y, z);
341  hit.p.SetXYZ(px, py, pz);
342  hit.t = rthit->t;
343  hit.E = rthit->E;
344  hit.track = rthit->track;
345  hit.primary = rthit->primary;
346  hit.ptype = rthit->ptype;
347 
348  return hit;
349 }
350 
351 //------------------
352 // MakeParticle
353 //------------------
355  double mass, hit_set hits) {
356  // Create a ROOT TLorentzVector object out of a Hall-D DKinematic Data object.
357  // Here, we have the mass passed in explicitly rather than use the mass contained in
358  // the DKinematicData object itself. This is because right now (Feb. 2009) the
359  // PID code is not mature enough to give reasonable guesses. See above code.
360 
361  double p = kd->momentum().Mag();
362  double theta = kd->momentum().Theta();
363  double phi = kd->momentum().Phi();
364  double px = p * sin(theta) * cos(phi);
365  double py = p * sin(theta) * sin(phi);
366  double pz = p * cos(theta);
367  double E = sqrt(mass * mass + p * p);
368  double x = kd->position().X();
369  double y = kd->position().Y();
370  double z = kd->position().Z();
371 
372  Particle part;
373  part.p.SetPxPyPzE(px, py, pz, E);
374  part.x.SetXYZ(x, y, z);
375  part.P = p;
376  part.E = E;
377  part.Th = theta;
378  part.Ph = phi;
379  part.is_fiducial = IsFiducial(kd);
380  part.hits_cdc = hits.hits_cdc;
381  part.hits_fdc = hits.hits_fdc;
382  part.hits_bcal = hits.hits_bcal;
383  part.hits_fcal = hits.hits_fcal;
384  part.hits_upv = hits.hits_upv;
385  part.hits_tof = hits.hits_tof;
386  part.hits_rich = hits.hits_rich;
387  part.hits_cere = hits.hits_cere;
388 
389  return part;
390 }
391 
392 //------------------
393 // FillEvent
394 //------------------
396  vector<Particle> &photon = pset.photons;
397  vector<Particle> &neutron = pset.neutrons;
398  vector<Particle> &pip = pset.piplus;
399  vector<Particle> &pim = pset.piminus;
400  vector<Particle> &Kp = pset.Kplus;
401  vector<Particle> &Km = pset.Kminus;
402  vector<Particle> &proton = pset.protons;
403  vector<Particle> &electron = pset.electrons;
404  vector<Particle> &positron = pset.positrons;
405  vector<RichHit> &richhit = pset.richhits;
406  vector<CereHit> &cerehit = pset.cerehits;
407  vector<RichTruthHit> &richtruthhit = pset.richtruthhits;
408 
409  // Sort particle arrays by energy
410  sort(photon.begin(), photon.end(), CompareLorentzEnergy);
411  sort(neutron.begin(), neutron.end(), CompareLorentzEnergy);
412  sort(pip.begin(), pip.end(), CompareLorentzEnergy);
413  sort(pim.begin(), pim.end(), CompareLorentzEnergy);
414  sort(Kp.begin(), Kp.end(), CompareLorentzEnergy);
415  sort(Km.begin(), Km.end(), CompareLorentzEnergy);
416  sort(proton.begin(), proton.end(), CompareLorentzEnergy);
417  sort(electron.begin(), electron.end(), CompareLorentzEnergy);
418  sort(positron.begin(), positron.end(), CompareLorentzEnergy);
419 
420  // Add photons
421  for (unsigned int i = 0; i < photon.size(); i++) {
422  TClonesArray &prts = *(evt->photon);
423  Particle *prt = new (prts[evt->Nphoton++]) Particle();
424  *prt = photon[i];
425  }
426 
427  // Add neutrons
428  for (unsigned int i = 0; i < neutron.size(); i++) {
429  TClonesArray &prts = *(evt->neutron);
430  Particle *prt = new (prts[evt->Nneutron++]) Particle();
431  *prt = neutron[i];
432  }
433 
434  // Add piplus
435  for (unsigned int i = 0; i < pip.size(); i++) {
436  TClonesArray &prts = *(evt->pip);
437  Particle *prt = new (prts[evt->Npip++]) Particle();
438  *prt = pip[i];
439  }
440 
441  // Add piminus
442  for (unsigned int i = 0; i < pim.size(); i++) {
443  TClonesArray &prts = *(evt->pim);
444  Particle *prt = new (prts[evt->Npim++]) Particle();
445  *prt = pim[i];
446  }
447 
448  // Add Kplus
449  for (unsigned int i = 0; i < Kp.size(); i++) {
450  TClonesArray &prts = *(evt->Kp);
451  Particle *prt = new (prts[evt->NKp++]) Particle();
452  *prt = Kp[i];
453  }
454 
455  // Add Kminus
456  for (unsigned int i = 0; i < Km.size(); i++) {
457  TClonesArray &prts = *(evt->Km);
458  Particle *prt = new (prts[evt->NKm++]) Particle();
459  *prt = Km[i];
460  }
461 
462  // Add proton
463  for (unsigned int i = 0; i < proton.size(); i++) {
464  TClonesArray &prts = *(evt->proton);
465  Particle *prt = new (prts[evt->Nproton++]) Particle();
466  *prt = proton[i];
467  }
468 
469  // Add electron
470  for (unsigned int i = 0; i < electron.size(); i++) {
471  TClonesArray &prts = *(evt->electron);
472  Particle *prt = new (prts[evt->Nelectron++]) Particle();
473  *prt = electron[i];
474  }
475 
476  // Add positron
477  for (unsigned int i = 0; i < positron.size(); i++) {
478  TClonesArray &prts = *(evt->positron);
479  Particle *prt = new (prts[evt->Npositron++]) Particle();
480  *prt = positron[i];
481  }
482 
483  /*
484  * Add RICH hit
485  */
486  for (unsigned int i = 0; i < richhit.size(); i++) {
487  TClonesArray &hits = *(evt->richhit);
488  RichHit *hit = new (hits[evt->Nrichhit++]) RichHit();
489  *hit = richhit[i];
490  }
491  /*
492  * Add Cherenkov hit
493  */
494  for (unsigned int i = 0; i < cerehit.size(); i++) {
495  TClonesArray &hits = *(evt->cerehit);
496  CereHit *hit = new (hits[evt->Ncerehit++]) CereHit();
497  *hit = cerehit[i];
498  }
499  /*
500  * Add RICH Truth hit
501  */
502  for (unsigned int i = 0; i < richtruthhit.size(); i++) {
503  TClonesArray &hits = *(evt->richtruthhit);
504  RichTruthHit *hit = new (hits[evt->Nrichtruthhit++]) RichTruthHit();
505  *hit = richtruthhit[i];
506  }
507  // Calculate W of reconstructed particles
508  for (unsigned int i = 0; i < photon.size(); i++)
509  evt->W += photon[i].p;
510  for (unsigned int i = 0; i < neutron.size(); i++)
511  evt->W += neutron[i].p;
512  for (unsigned int i = 0; i < pip.size(); i++)
513  evt->W += pip[i].p;
514  for (unsigned int i = 0; i < pim.size(); i++)
515  evt->W += pim[i].p;
516  for (unsigned int i = 0; i < Kp.size(); i++)
517  evt->W += Kp[i].p;
518  for (unsigned int i = 0; i < Km.size(); i++)
519  evt->W += Km[i].p;
520  for (unsigned int i = 0; i < electron.size(); i++)
521  evt->W += electron[i].p;
522  for (unsigned int i = 0; i < positron.size(); i++)
523  evt->W += positron[i].p;
524 }
525 
526 //------------------
527 // IsFiducial
528 //------------------
530  double theta_degrees = kd->momentum().Theta() * TMath::RadToDeg();
531  double p = kd->momentum().Mag();
532 
533  if (kd->charge() == 0.0) {
534  // photons
535  if (theta_degrees < 2.0 || theta_degrees > 110.0)
536  return false;
537  if (p < 0.100)
538  return false;
539  } else {
540  // charged particles
541  if (theta_degrees < 1.0 || theta_degrees > 120.0)
542  return false;
543  if (p < 0.400)
544  return false;
545  }
546 
547  return true;
548 }
549 
550 //------------------
551 // erun
552 //------------------
554  return NOERROR;
555 }
556 
557 //------------------
558 // fini
559 //------------------
561  return NOERROR;
562 }
CereHit MakeCereHit(const DCereHit *chit)
UInt_t Nelectron
Definition: mc_tree/Event.h:43
Int_t hits_cere
TClonesArray * Kp
Definition: mc_tree/Event.h:51
Definition: photon.h:15
Double_t pe
Definition: CereHit.h:28
RichTruthHit MakeRichTruthHit(const DRichTruthHit *rthit)
Int_t hits_tof
Double_t P
Double_t x[NCHANNELS]
Definition: st_tw_resols.C:39
static bool CompareLorentzEnergy(const Particle &a, const Particle &b)
#define y
TClonesArray * pip
Definition: mc_tree/Event.h:49
const DVector3 & position(void) const
UInt_t Npip
Definition: mc_tree/Event.h:36
TClonesArray * photon
Definition: mc_tree/Event.h:54
TClonesArray * cerehit
Definition: mc_tree/Event.h:59
Particle MakeParticle(const DKinematicData *kd, double mass, hit_set hits)
UInt_t Npositron
Definition: mc_tree/Event.h:44
void Clear(void)
Int_t hits_upv
UInt_t Nphoton
Definition: mc_tree/Event.h:41
UInt_t Npim
Definition: mc_tree/Event.h:37
UInt_t Nneutron
Definition: mc_tree/Event.h:42
int sector
Definition: DCereHit.h:20
TLorentzVector DLorentzVector
TClonesArray * neutron
Definition: mc_tree/Event.h:55
#define X(str)
Definition: hddm-c.cpp:83
Definition: GlueX.h:17
Definition: GlueX.h:19
jerror_t erun(void)
Invoked via DEventProcessor virtual method.
JApplication * japp
Int_t hits_fcal
DetectorSystem_t system
particle type
Definition: DMCTrackHit.h:25
int track
Track number.
Definition: RichTruthHit.h:30
UInt_t NKm
Definition: mc_tree/Event.h:39
Int_t hits_rich
TVector3 x
Definition: RichHit.h:24
Double_t t
Definition: RichHit.h:28
jerror_t init(void)
Invoked via DEventProcessor virtual method.
UInt_t NKp
Definition: mc_tree/Event.h:38
TVector3 x
InitPlugin_t InitPlugin
Definition: GlueX.h:20
Definition: GlueX.h:18
UInt_t Ncerehit
Definition: mc_tree/Event.h:46
TClonesArray * proton
Definition: mc_tree/Event.h:53
Definition: GlueX.h:22
void FillEvent(Event *evt, particle_set &pset)
TLorentzVector W
Definition: mc_tree/Event.h:65
RichHit MakeRichHit(const DRichHit *rhit)
TLorentzVector target
Definition: mc_tree/Event.h:62
Double_t E
UInt_t Nrichhit
Definition: mc_tree/Event.h:45
UInt_t Nproton
Definition: mc_tree/Event.h:40
Bool_t is_fiducial
TLorentzVector p
UInt_t Nrichtruthhit
Definition: mc_tree/Event.h:47
double charge(void) const
bool IsFiducial(const DKinematicData *kd)
Int_t hits_bcal
TVector3 x
Definition: RichTruthHit.h:23
jerror_t evnt(JEventLoop *loop, uint64_t eventnumber)
Invoked via DEventProcessor virtual method.
TH1D * py[NCHANNELS]
bool all_fiducial
Definition: mc_tree/Event.h:67
TClonesArray * richtruthhit
Definition: mc_tree/Event.h:60
static double ParticleMass(Particle_t p)
double sqrt(double)
double sin(double)
TClonesArray * pim
Definition: mc_tree/Event.h:50
Double_t t
Definition: CereHit.h:29
TClonesArray * positron
Definition: mc_tree/Event.h:57
int primary
primary track=1 not primary track=0
Definition: DMCTrackHit.h:23
Double_t Th
const DVector3 & momentum(void) const
float t
Definition: DCereHit.h:22
TClonesArray * richhit
Definition: mc_tree/Event.h:58
TVector3 p
Definition: RichTruthHit.h:27
UInt_t event
Int_t sector
Definition: CereHit.h:24
TClonesArray * Km
Definition: mc_tree/Event.h:52
Int_t hits_cdc
TLorentzVector beam
Int_t hits_fdc
TDirectory * dir
Definition: bcal_hist_eff.C:31
Definition: GlueX.h:23
int primary
primary track=1 not primary track=0
Definition: RichTruthHit.h:31
int track
Track number.
Definition: DMCTrackHit.h:21
for(auto locVertexInfo:dStepVertexInfos)
TClonesArray * electron
Definition: mc_tree/Event.h:56
Double_t Ph
TVector3 vertex
jerror_t fini(void)
Invoked via DEventProcessor virtual method.
float pe
Definition: DCereHit.h:21