Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SynRadAna.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SynRadAna.cc
1 
2 
3 #include "SynRadAna.h"
4 
6 
9 
12 
13 #include <g4eval/CaloEvalStack.h>
15 #include <g4eval/SvtxEvalStack.h>
16 
17 #include <g4main/PHG4Hit.h>
19 #include <g4main/PHG4Particle.h>
21 #include <g4main/PHG4VtxPoint.h>
22 
23 #include <calobase/RawCluster.h>
24 #include <calobase/RawTower.h>
25 #include <calobase/RawTowerContainer.h>
26 #include <calobase/RawTowerGeomContainer.h>
27 
28 #include <mvtx/CylinderGeom_Mvtx.h>
29 #include <mvtx/MvtxDefs.h>
30 //#include <mvtx/MvtxHit.h>
31 #include <trackbase/TrkrDefs.h>
32 #include <trackbase/TrkrHit.h> // for TrkrHit
33 #include <trackbase/TrkrHitSet.h>
37 
38 #include <g4eval/SvtxTrackEval.h> // for SvtxTrackEval
39 #include <g4eval/SvtxTruthEval.h> // for SvtxTruthEval
40 
43 #include <fun4all/SubsysReco.h>
44 
45 #include <phool/PHCompositeNode.h>
46 #include <phool/PHDataNode.h>
47 #include <phool/PHNode.h> // for PHNode
48 #include <phool/PHNodeIterator.h>
49 #include <phool/PHObject.h>
50 #include <phool/PHRandomSeed.h>
51 #include <phool/getClass.h>
52 #include <phool/phool.h>
53 #include <phool/recoConsts.h>
54 
55 #include <HepMC/GenEvent.h>
56 #include <HepMC/GenParticle.h>
57 #include <HepMC/GenVertex.h>
58 #include <HepMC/IteratorRange.h>
59 #include <HepMC/SimpleVector.h>
60 #include <HepMC/Units.h>
61 
62 #include <gsl/gsl_const.h>
63 #include <gsl/gsl_randist.h>
64 #include <gsl/gsl_rng.h>
65 
66 #include <TAxis.h>
67 #include <TDatabasePDG.h>
68 #include <TH1.h>
69 #include <TH2.h>
70 #include <TNamed.h>
71 #include <TString.h>
72 #include <TVector3.h>
73 
74 #include <phool/PHCompositeNode.h>
75 #include <cassert>
76 #include <cmath>
77 #include <cstdlib>
78 #include <iostream>
79 #include <iterator>
80 #include <list>
81 #include <utility>
82 
83 using namespace std;
84 
85 //____________________________________________________________________________..
87  : SubsysReco("SynRadAna")
88  , _embedding_id(1)
89  , m_eventWeight(0)
90  , do_photon(true)
91  , do_MVTXHits(true)
92  , do_TPCHits(true)
93  , m_outputFIle(fname)
94 {
95  if (Verbosity())
96  cout << "SynRadAna::SynRadAna(const std::string &name) Calling ctor" << endl;
97 }
98 
99 //____________________________________________________________________________..
101 {
102  if (Verbosity())
103  cout << "SynRadAna::~SynRadAna() Calling dtor" << endl;
104 }
105 
106 //____________________________________________________________________________..
108 {
109  if (Verbosity())
110  cout << "SynRadAna::Init(PHCompositeNode *topNode) Initializing" << endl;
111 
113  assert(hm);
114 
115  {
116  TH1 *h(nullptr);
117  // n events and n tracks histogram
118  h = new TH1D(TString(get_histo_prefix()) + "Normalization",
119  TString(get_histo_prefix()) + " Normalization;Items;Count", 10, .5, 10.5);
120  int i = 1;
121  h->GetXaxis()->SetBinLabel(i++, "Event");
122  h->GetXaxis()->SetBinLabel(i++, "Photon");
123  h->GetXaxis()->SetBinLabel(i++, "Flux");
124  h->GetXaxis()->SetBinLabel(i++, "G4Particle");
125  h->GetXaxis()->SetBinLabel(i++, "G4PrimaryParticle");
126  h->GetXaxis()->SetBinLabel(i++, "G4Vertex");
127  h->GetXaxis()->LabelsOption("v");
128  hm->registerHisto(h);
129  }
130 
131  for (const auto &hitnode : _node_postfix)
132  {
133  TH2D *h2(nullptr);
134 
135  h2 = new TH2D(TString(get_histo_prefix()) + hitnode + "_nHit",
136  "Hit sum;Sum number of hits", 2000, -.5, 2000 - .5, 2, .5, 2.5);
137  // QAHistManagerDef::useLogBins(h->GetXaxis());
138  h2->GetYaxis()->SetBinLabel(1, "Flux");
139  h2->GetYaxis()->SetBinLabel(2, "Photon");
140  hm->registerHisto(h2);
141 
142  h2 = new TH2D(TString(get_histo_prefix()) + hitnode + "_sumEdep",
143  "Hit sum energy distribution;Ionizing energy deposition [keV]", 2000, 0, 100, 2, .5, 2.5);
144  // QAHistManagerDef::useLogBins(h->GetXaxis());
145  h2->GetYaxis()->SetBinLabel(1, "Flux");
146  h2->GetYaxis()->SetBinLabel(2, "Photon");
147  hm->registerHisto(h2);
148 
149  h2 = new TH2D(TString(get_histo_prefix()) + hitnode + "_photonEnergy",
150  "Hit source photon;Photon energy [keV]", 2000, 0, 100, 2, .5, 2.5);
151  // QAHistManagerDef::useLogBins(h->GetXaxis());
152  h2->GetYaxis()->SetBinLabel(1, "Flux");
153  h2->GetYaxis()->SetBinLabel(2, "Photon");
154  hm->registerHisto(h2);
155 
156  h2 = new TH2D(TString(get_histo_prefix()) + hitnode + "_photonZ",
157  "#gamma z crossing interface facet [cm]", 800, -400, 400, 2, .5, 2.5);
158  // QAHistManagerDef::useLogBins(h->GetXaxis());
159  h2->GetYaxis()->SetBinLabel(1, "Flux");
160  h2->GetYaxis()->SetBinLabel(2, "Photon");
161  hm->registerHisto(h2);
162  }
163 
164  for (vector<string>::const_iterator it = _tower_postfix.begin();
165  it != _tower_postfix.end(); ++it)
166  {
167  }
168 
169  if (do_photon)
170  {
171  TH2D *h2 = new TH2D(TString(get_histo_prefix()) + "photonEnergy",
172  "Source photon;Photon energy [keV]", 2000, 0, 100, 2, .5, 2.5);
173  // QAHistManagerDef::useLogBins(h->GetXaxis());
174  h2->GetYaxis()->SetBinLabel(1, "Flux");
175  h2->GetYaxis()->SetBinLabel(2, "Photon");
176  hm->registerHisto(h2);
177 
178  h2 = new TH2D(TString(get_histo_prefix()) + "photonZ",
179  "#gamma z crossing interface facet [cm]", 800, -400, 400, 2, .5, 2.5);
180  // QAHistManagerDef::useLogBins(h->GetXaxis());
181  h2->GetYaxis()->SetBinLabel(1, "Flux");
182  h2->GetYaxis()->SetBinLabel(2, "Photon");
183  hm->registerHisto(h2);
184  }
185 
186  if (do_MVTXHits)
187  {
188  TH2D *h2(nullptr);
189 
190  h2 = new TH2D(TString(get_histo_prefix()) + "MVTXHit" + "_nHit",
191  "Hit sum;Sum number of hits", 2000, -.5, 2000 - .5, 2, .5, 2.5);
192  // QAHistManagerDef::useLogBins(h->GetXaxis());
193  h2->GetYaxis()->SetBinLabel(1, "Flux");
194  h2->GetYaxis()->SetBinLabel(2, "Photon");
195  hm->registerHisto(h2);
196 
197  h2 = new TH2D(TString(get_histo_prefix()) + "MVTXHit" + "_nHit_Layer",
198  "Hit sum;Sum number of hits;layer", 2000, -.5, 2000 - .5, 3, -.5, 2.5);
199  hm->registerHisto(h2);
200  }
201 
202  if (do_TPCHits)
203  {
204  TH2D *h2(nullptr);
205 
206  h2 = new TH2D(TString(get_histo_prefix()) + "TPCHit" + "_nHit",
207  "Hit sum;Sum number of hits", 2000, -.5, 2000 - .5, 2, .5, 2.5);
208  // QAHistManagerDef::useLogBins(h->GetXaxis());
209  h2->GetYaxis()->SetBinLabel(1, "Flux");
210  h2->GetYaxis()->SetBinLabel(2, "Photon");
211  hm->registerHisto(h2);
212 
213  h2 = new TH2D(TString(get_histo_prefix()) + "TPCHit" + "_nHit_Layer",
214  "Hit sum;Sum number of hits;layer", 2000, -.5, 2000 - .5, 61, -.5, 60.5);
215  hm->registerHisto(h2);
216  }
217 
219 }
220 
221 //____________________________________________________________________________..
223 {
224  if (Verbosity())
225  cout << "SynRadAna::InitRun(PHCompositeNode *topNode) Initializing for Run XXX" << endl;
226 
228 }
229 
230 //____________________________________________________________________________..
232 {
233  if (Verbosity())
234  cout << "SynRadAna::process_event(PHCompositeNode *topNode) Processing Event" << endl;
235 
236  static const double GeV2keV = 1e6;
237 
238  // histogram manager
240  assert(hm);
241  // n events and n tracks histogram
242  TH1 *h_norm = dynamic_cast<TH1 *>(hm->getHisto(get_histo_prefix() + "Normalization"));
243  assert(h_norm);
244  h_norm->Fill("Event", 1);
245 
247  PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
248  assert(truthInfoList);
249  h_norm->Fill("G4Particle", truthInfoList->size());
250  h_norm->Fill("G4Vertex", truthInfoList->GetNumVertices());
251  h_norm->Fill("G4PrimaryParticle", truthInfoList->GetNumPrimaryVertexParticles());
252 
253  // For pile-up simulation: define GenEventMap
254  PHHepMCGenEventMap *genevtmap = findNode::getClass<PHHepMCGenEventMap>(topNode, "PHHepMCGenEventMap");
255  assert(genevtmap);
256  PHHepMCGenEvent *genev = genevtmap->get(_embedding_id);
257  assert(genev);
258  HepMC::GenEvent *hepmc_evt = genev->getEvent();
259  assert(hepmc_evt);
260 
261  if (Verbosity() >= 2)
262  {
263  hepmc_evt->print();
264  }
265 
266  //weight calculation/normalization
267  const auto &weightcontainer = hepmc_evt->weights();
268  assert(weightcontainer.size() >= 2);
269  h_norm->Fill("Flux", weightcontainer[0]);
270  h_norm->Fill("Photon", weightcontainer[1]);
271  if (weightcontainer[1] != 1)
272  {
273  cout << __PRETTY_FUNCTION__ << "- warning - weightcontainer[1] = " << weightcontainer[1]
274  << " != 1. Can not precisely pin point each photon's flux!"
275  << endl;
276  }
277  m_eventWeight = weightcontainer[0] / weightcontainer[1];
278 
279  // initial photons
280  if (do_photon)
281  {
282  TH2 *h_photonEnergy = dynamic_cast<TH2 *>(hm->getHisto(string(get_histo_prefix()) + "photonEnergy"));
283  assert(h_photonEnergy);
284  TH2 *h_photonZ = dynamic_cast<TH2 *>(hm->getHisto(string(get_histo_prefix()) + "photonZ"));
285  assert(h_photonZ);
286 
287  PHG4TruthInfoContainer::ConstRange primary_range =
288  truthInfoList->GetPrimaryParticleRange();
289 
290  for (PHG4TruthInfoContainer::ConstIterator particle_iter =
291  primary_range.first;
292  particle_iter != primary_range.second;
293  ++particle_iter)
294  {
295  const auto particle = particle_iter->second;
296 
297  assert(particle);
298  if (particle->get_pid() == 22)
299  {
300  const double photon_e_keV = particle->get_e() * GeV2keV;
301  h_photonEnergy->Fill(photon_e_keV, "Flux", m_eventWeight);
302  h_photonEnergy->Fill(photon_e_keV, "Photon", 1);
303 
304  PHG4VtxPoint *vtx = truthInfoList->GetVtx(particle->get_vtx_id());
305  assert(vtx);
306 
307  const double photon_z = vtx->get_z();
308  h_photonZ->Fill(photon_z, "Flux", m_eventWeight);
309  h_photonZ->Fill(photon_z, "Photon", 1);
310  }
311  else
312  {
313  cout << __PRETTY_FUNCTION__ << "- warning - non-photon source!";
314  particle->identify();
315  }
316  } // if (_load_all_particle) else
317  }
318 
319  for (const auto &hitnode : _node_postfix)
320  {
321  PHG4HitContainer *hits = findNode::getClass<PHG4HitContainer>(topNode,
322  string("G4HIT_" + hitnode));
323  if (!hits)
324  {
325  cout << __PRETTY_FUNCTION__ << " - Fatal Error - missing " << string("G4HIT_" + hitnode) << endl;
326  }
327 
328  assert(hits);
329  PHG4HitContainer::ConstRange hit_range = hits->getHits();
330 
331  if (Verbosity() >= 2)
332  cout << "SynRadAna::process_event - processing "
333  << hitnode << " and received " << hits->size() << " hits"
334  << endl;
335 
336  int nhit(0);
337  double sumEdep_keV(0);
338 
339  map<PHG4Particle *, pair<double, double> > primary_photon_map;
340  for (PHG4HitContainer::ConstIterator hit_iter = hit_range.first;
341  hit_iter != hit_range.second; hit_iter++)
342  {
343  PHG4Hit *hit = hit_iter->second;
344  assert(hit);
345 
346  ++nhit;
347 
348  const double eion_keV = hit->get_eion() * GeV2keV;
349  sumEdep_keV += eion_keV;
350 
351  PHG4Particle *hit_particle =
352  truthInfoList->GetParticle(hit->get_trkid());
353  assert(hit_particle);
354 
355  PHG4Particle *primary_particle = truthInfoList->GetParticle(hit_particle->get_primary_id());
356  assert(primary_particle);
357  if (primary_particle->get_pid() != 22)
358  {
359  cout << "SynRadAna::process_event - WARNING - unexpected primary particle that is not photon: ";
360  primary_particle->identify();
361 
362  continue;
363  }
364  const double photon_e_keV = primary_particle->get_e() * GeV2keV;
365 
366  PHG4VtxPoint *vtx = truthInfoList->GetVtx(primary_particle->get_vtx_id());
367  assert(vtx);
368  const double photon_z = vtx->get_z();
369 
370  primary_photon_map.insert(
371  make_pair(primary_particle,
372  make_pair(photon_e_keV, photon_z)));
373  }
374 
375  TH2 *h_nHit = dynamic_cast<TH2 *>(hm->getHisto(string(get_histo_prefix()) + hitnode + "_nHit"));
376  assert(h_nHit);
377 
378  TH2 *h_sumEdep = dynamic_cast<TH2 *>(hm->getHisto(string(get_histo_prefix()) + hitnode + "_sumEdep"));
379  assert(h_sumEdep);
380 
381  TH2 *h_photonEnergy = dynamic_cast<TH2 *>(hm->getHisto(string(get_histo_prefix()) + hitnode + "_photonEnergy"));
382  assert(h_photonEnergy);
383 
384  TH2 *h_photonZ = dynamic_cast<TH2 *>(hm->getHisto(string(get_histo_prefix()) + hitnode + "_photonZ"));
385  assert(h_photonZ);
386 
387  h_nHit->Fill(nhit, "Flux", m_eventWeight);
388  h_nHit->Fill(nhit, "Photon", 1);
389 
390  if (nhit > 0)
391  {
392  if (primary_photon_map.size() != 1)
393  {
394  cout << "SynRadAna::process_event - WARNING - primary_photon_map.size() = " << primary_photon_map.size() << endl;
395 
396  continue;
397  }
398 
399  for (auto &pair : primary_photon_map)
400  {
401  const std::pair<double, double> & e_z = pair.second;
402 
403  h_photonEnergy->Fill(e_z.first, "Flux", m_eventWeight);
404  h_photonEnergy->Fill(e_z.first, "Photon", 1);
405 
406  h_photonZ->Fill(e_z.second, "Flux", m_eventWeight);
407  h_photonZ->Fill(e_z.second, "Photon", 1);
408  }
409 
410  h_sumEdep->Fill(sumEdep_keV, "Flux", m_eventWeight);
411  h_sumEdep->Fill(sumEdep_keV, "Photon", 1);
412  }
413 
414  } // for (const auto &hitnode : _node_postfix)
415 
416  if (do_MVTXHits)
417  {
418  // Get the TrkrHitSetContainer node
419  TrkrHitSetContainer *trkrhitsetcontainer = findNode::getClass<TrkrHitSetContainer>(topNode, "TRKR_HITSET");
420  if (!trkrhitsetcontainer)
421  {
422  cout << "Could not locate TRKR_HITSET node, quit! " << endl;
423  exit(1);
424  }
425 
426  int nhit(0);
427  map<int, int> layer_nhit{{0, 0}, {0, 0}, {0, 0}};
428  // We want all hitsets for the Mvtx
429  TrkrHitSetContainer::ConstRange hitset_range = trkrhitsetcontainer->getHitSets(TrkrDefs::TrkrId::mvtxId);
430  for (TrkrHitSetContainer::ConstIterator hitset_iter = hitset_range.first;
431  hitset_iter != hitset_range.second;
432  ++hitset_iter)
433  {
434  // we have an itrator to one TrkrHitSet for the mvtx from the trkrHitSetContainer
435  // get the hitset key so we can find the layer
436  TrkrDefs::hitsetkey hitsetkey = hitset_iter->first;
437  int layer = TrkrDefs::getLayer(hitsetkey);
438  if (Verbosity() >= 2) cout << __PRETTY_FUNCTION__ << ": found MVTX hitset with key: " << hitsetkey << " in layer " << layer << endl;
439 
440  TrkrHitSet *hitset = hitset_iter->second;
441  TrkrHitSet::ConstRange hit_range = hitset->getHits();
442  for (TrkrHitSet::ConstIterator hit_iter = hit_range.first;
443  hit_iter != hit_range.second;
444  ++hit_iter)
445  {
446  TrkrHit *hit = hit_iter->second;
447  assert(hit);
448  if (Verbosity() >= 2)
449  {
450  cout << hit->getAdc() << "ADC hit: ";
451  hit->identify();
452  }
453 
454  if (hit->getAdc())
455  {
456  ++nhit;
457 
458  layer_nhit[layer] += 1;
459  }
460  }
461  }
462 
463  TH2 *h_nHit = dynamic_cast<TH2 *>(hm->getHisto(string(get_histo_prefix()) + "MVTXHit" + "_nHit"));
464  assert(h_nHit);
465 
466  TH2 *h_nHit_Layer = dynamic_cast<TH2 *>(hm->getHisto(string(get_histo_prefix()) + "MVTXHit" + "_nHit_Layer"));
467  assert(h_nHit_Layer);
468 
469  h_nHit->Fill(nhit, "Flux", m_eventWeight);
470  h_nHit->Fill(nhit, "Photon", 1);
471 
472  for (auto &pair : layer_nhit)
473  {
474  if (Verbosity() >= 2)
475  cout << __PRETTY_FUNCTION__ << ": MVTX Summary: found " << pair.second << " hits in layer " << pair.first << endl;
476 
477  h_nHit_Layer->Fill(pair.second, pair.first, m_eventWeight);
478  }
479 
480  } // if (do_MVTXHits)
481 
482  if (do_TPCHits)
483  {
484  // Get the TrkrHitSetContainer node
485  TrkrHitSetContainer *trkrhitsetcontainer = findNode::getClass<TrkrHitSetContainer>(topNode, "TRKR_HITSET");
486  if (!trkrhitsetcontainer)
487  {
488  cout << "Could not locate TRKR_HITSET node, quit! " << endl;
489  exit(1);
490  }
491 
492  int nhit(0);
493  map<int, int> layer_nhit{};
494  // We want all hitsets for the TPC
495  TrkrHitSetContainer::ConstRange hitset_range = trkrhitsetcontainer->getHitSets(TrkrDefs::TrkrId::tpcId);
496  for (TrkrHitSetContainer::ConstIterator hitset_iter = hitset_range.first;
497  hitset_iter != hitset_range.second;
498  ++hitset_iter)
499  {
500  // we have an itrator to one TrkrHitSet for the mvtx from the trkrHitSetContainer
501  // get the hitset key so we can find the layer
502  TrkrDefs::hitsetkey hitsetkey = hitset_iter->first;
503  int layer = TrkrDefs::getLayer(hitsetkey);
504  if (Verbosity() >= 2) cout << __PRETTY_FUNCTION__ << ": found TPC hitset with key: " << hitsetkey << " in layer " << layer << endl;
505 
506  TrkrHitSet *hitset = hitset_iter->second;
507  TrkrHitSet::ConstRange hit_range = hitset->getHits();
508  for (TrkrHitSet::ConstIterator hit_iter = hit_range.first;
509  hit_iter != hit_range.second;
510  ++hit_iter)
511  {
512  TrkrHit *hit = hit_iter->second;
513  assert(hit);
514  if (Verbosity() >= 2)
515  {
516  cout << hit->getAdc() << " ADC hit. " << endl;
517  }
518 
519  if (hit->getAdc())
520  {
521  ++nhit;
522 
523  if (layer_nhit.find(layer) == layer_nhit.end())
524  layer_nhit.insert(make_pair(layer, 0));
525 
526  layer_nhit[layer] += 1;
527  }
528  }
529  }
530 
531  TH2 *h_nHit = dynamic_cast<TH2 *>(hm->getHisto(string(get_histo_prefix()) + "TPCHit" + "_nHit"));
532  assert(h_nHit);
533 
534  TH2 *h_nHit_Layer = dynamic_cast<TH2 *>(hm->getHisto(string(get_histo_prefix()) + "TPCHit" + "_nHit_Layer"));
535  assert(h_nHit_Layer);
536 
537  h_nHit->Fill(nhit, "Flux", m_eventWeight);
538  h_nHit->Fill(nhit, "Photon", 1);
539 
540  for (auto &pair : layer_nhit)
541  {
542  if (Verbosity() >= 2)
543  cout << __PRETTY_FUNCTION__ << ": TPC summary: found " << pair.second << " hits in layer " << pair.first << endl;
544 
545  h_nHit_Layer->Fill(pair.second, pair.first, m_eventWeight);
546  }
547 
548  } // if (do_TPCHits)
549 
551 }
552 
553 //____________________________________________________________________________..
555 {
556  if (Verbosity())
557  cout << "SynRadAna::ResetEvent(PHCompositeNode *topNode) Resetting internal structures, prepare for next event" << endl;
559 }
560 
561 //____________________________________________________________________________..
563 {
564  if (Verbosity())
565  cout << "SynRadAna::EndRun(const int runnumber) Ending Run for Run " << runnumber << endl;
567 }
568 
569 //____________________________________________________________________________..
571 {
572  if (Verbosity())
573  cout << "SynRadAna::End(PHCompositeNode *topNode) This is the End..." << endl;
574 
576 
578 }
579 
580 //____________________________________________________________________________..
582 {
583  if (Verbosity())
584  cout << "SynRadAna::Reset(PHCompositeNode *topNode) being Reset" << endl;
586 }
587 
588 //____________________________________________________________________________..
589 void SynRadAna::Print(const std::string &what) const
590 {
591  cout << "SynRadAna::Print(const std::string &what) const Printing info for " << what << endl;
592 }
593 
597 {
598  static const string HistoManagerName("HistoManager_SynRadAna");
599 
601  Fun4AllHistoManager *hm = se->getHistoManager(HistoManagerName);
602 
603  if (not hm)
604  {
605  // cout
606  // << "QAHistManagerDef::get_HistoManager - Making Fun4AllHistoManager EMCalAna_HISTOS"
607  // << endl;
608  hm = new Fun4AllHistoManager(HistoManagerName);
609  se->registerHistoManager(hm);
610  }
611 
612  assert(hm);
613 
614  return hm;
615 }
616 
617 string
619 {
620  return string("h_") + Name() + string("_");
621 }