Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JetHepMCLoader.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file JetHepMCLoader.cc
1 // $Id: $
2 
11 #include "JetHepMCLoader.h"
12 #include <jetbase/JetContainer.h> // for JetContainer
13 #include <jetbase/JetContainer.h>
14 #include <jetbase/Jetv1.h>
15 
18 
19 #include <fun4all/Fun4AllBase.h> // for Fun4AllBase::VERBOSITY_A_LOT
22 #include <fun4all/Fun4AllServer.h>
23 #include <fun4all/SubsysReco.h> // for SubsysReco
24 
25 #include <phool/PHCompositeNode.h>
26 #include <phool/PHIODataNode.h>
27 #include <phool/PHNode.h> // for PHNode
28 #include <phool/PHNodeIterator.h>
29 #include <phool/PHObject.h> // for PHObject
30 #include <phool/getClass.h>
31 #include <phool/phool.h> // for PHWHERE
32 
33 #include <TAxis.h> // for TAxis
34 #include <TH1.h>
35 #include <TH2.h>
36 #include <TNamed.h> // for TNamed
37 
38 #pragma GCC diagnostic push
39 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
40 #include <HepMC/GenEvent.h> // for GenEvent, GenEvent::particl...
41 #pragma GCC diagnostic pop
42 
43 #include <HepMC/GenParticle.h> // for GenParticle
44 #include <HepMC/SimpleVector.h> // for FourVector
45 #include <HepMC/Units.h> // for conversion_factor, GEV
46 
47 #include <algorithm> // for max
48 #include <cassert>
49 #include <iostream>
50 
52  : SubsysReco("JetHepMCLoader_" + jetInputCategory)
53  , m_jetInputCategory(jetInputCategory)
54 
55 {
56 }
57 
59 {
60  if (m_saveQAPlots)
61  {
63  assert(hm);
64 
65  const int n_bins = 1 + m_jetSrc.size();
66 
67  TH1D *h = new TH1D("hNormalization", //
68  "Normalization;Items;Summed quantity", n_bins, .5, n_bins + .5);
69  int i = 1;
70  h->GetXaxis()->SetBinLabel(i++, "Event count");
71  for (const hepmc_jet_src &src : m_jetSrc)
72  {
73  h->GetXaxis()->SetBinLabel(i++, (std::string("SubEvent ") + src.m_name).c_str());
74  }
75  h->GetXaxis()->LabelsOption("v");
76  hm->registerHisto(h);
77 
78  for (const hepmc_jet_src &src : m_jetSrc)
79  {
80  hm->registerHisto(
81  new TH2F((std::string("hJetEtEta_") + src.m_name).c_str(), //
82  ("Jet distribution from " + src.m_name + ";Jet #eta;Jet E_{T} [GeV]").c_str(),
83  40, -4., 4.,
84  100, 0, 100));
85  }
86  } // if (m_saveQAPlots)
87 
88  return CreateNodes(topNode);
89 }
90 
92 {
93  // For pile-up simulation: define GenEventMap
94  PHHepMCGenEventMap *genevtmap = findNode::getClass<PHHepMCGenEventMap>(topNode, "PHHepMCGenEventMap");
95 
96  if (!genevtmap)
97  {
98  static bool once = true;
99 
100  if (once and Verbosity())
101  {
102  once = false;
103 
104  std::cout << "HepMCNodeReader::process_event - No PHHepMCGenEventMap node. Do not perform HepMC->Geant4 input" << std::endl;
105  }
106 
108  }
109 
110  if (m_saveQAPlots)
111  {
113  assert(hm);
114  TH1D *h_norm = dynamic_cast<TH1D *>(hm->getHisto("hNormalization"));
115  assert(h_norm);
116  h_norm->Fill("Event count", 1);
117  } // if (m_saveQAPlots)
118 
119  for (const hepmc_jet_src &src : m_jetSrc)
120  {
121  JetContainer *jets = findNode::getClass<JetContainer>(topNode, src.m_name);
122  assert(jets);
123 
124  jets->set_algo(src.m_algorithmID);
125  jets->set_par(src.m_parameter);
127 
128  PHHepMCGenEvent *genevt = genevtmap->get(src.m_embeddingID);
129 
130  if (genevt == nullptr) continue;
131 
132  HepMC::GenEvent *evt = genevt->getEvent();
133  if (!evt)
134  {
135  std::cout << PHWHERE << " no evt pointer under HEPMC Node found:";
136  genevt->identify();
138  }
139 
140  assert(genevt->get_embedding_id() == src.m_embeddingID);
141 
142  TH2F *hjet = nullptr;
143 
144  if (m_saveQAPlots)
145  {
147  assert(hm);
148  TH1D *h_norm = dynamic_cast<TH1D *>(hm->getHisto("hNormalization"));
149  assert(h_norm);
150  h_norm->Fill((std::string("SubEvent ") + src.m_name).c_str(), 1);
151 
152  hjet = dynamic_cast<TH2F *>(hm->getHisto(std::string("hJetEtEta_") + src.m_name));
153  assert(hjet);
154 
155  } // if (m_saveQAPlots)
156 
157  const double mom_factor = HepMC::Units::conversion_factor(evt->momentum_unit(), HepMC::Units::GEV);
158 
159  for (HepMC::GenEvent::particle_const_iterator p = evt->particles_begin();
160  p != evt->particles_end(); ++p)
161  {
162  HepMC::GenParticle *part = (*p);
163 
164  assert(part);
165  if (Verbosity() >= VERBOSITY_A_LOT)
166  {
167  part->print();
168  }
169 
170  if (part->status() == src.m_tagStatus and part->pdg_id() == src.m_tagPID)
171  {
172  Jet *jet = jets->add_jet(); // returns a new Jetv2
173 
174  jet->set_px(part->momentum().px() * mom_factor);
175  jet->set_py(part->momentum().py() * mom_factor);
176  jet->set_pz(part->momentum().pz() * mom_factor);
177  jet->set_e(part->momentum().e() * mom_factor);
178 
179  jet->insert_comp(Jet::HEPMC_IMPORT, part->barcode());
180  if (hjet)
181  {
182  hjet->Fill(jet->get_eta(), jet->get_et());
183  }
184 
185  } // if (part->status() == src.m_tagStatus and part->pdg_id() == src.m_tagPID)
186  }
187  } // for (const hepmc_jet_src &src : m_jetSrc)
188 
190 }
191 
193 {
194  if (m_saveQAPlots)
195  {
197  assert(hm);
198 
199  std::cout << "JetHepMCLoader::End - saving QA histograms to " << Name() + ".root" << std::endl;
200  hm->dumpHistos(Name() + ".root", "RECREATE");
201  }
202 
204 }
205 
207  const std::string &name,
208  int embeddingID,
210  double parameter,
211  int tagPID,
212  int tagStatus)
213 {
214  std::string algorithmName = "Undefined_Jet_Algorithm";
215 
216  switch (algorithm)
217  {
218  case Jet::ANTIKT:
219  algorithmName = "ANTIKT";
220  break;
221 
222  case Jet::KT:
223  algorithmName = "KT";
224  break;
225 
226  case Jet::CAMBRIDGE:
227  algorithmName = "CAMBRIDGE";
228  break;
229 
230  default:
231 
232  break;
233  }
234 
235  hepmc_jet_src src{name, embeddingID, algorithmName, algorithm, parameter, tagPID, tagStatus};
236 
237  m_jetSrc.push_back(src);
238 }
239 
241 {
242  PHNodeIterator iter(topNode);
243 
244  // Looking for the DST node
245  PHCompositeNode *dstNode = static_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "DST"));
246  if (!dstNode)
247  {
248  std::cout << PHWHERE << "DST Node missing, doing nothing." << std::endl;
250  }
251 
252  for (const hepmc_jet_src &src : m_jetSrc)
253  {
254  // Create the AntiKt node if required
255  PHCompositeNode *AlgoNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", src.m_algorithmName.c_str()));
256  if (!AlgoNode)
257  {
258  AlgoNode = new PHCompositeNode(src.m_algorithmName.c_str());
259  dstNode->addNode(AlgoNode);
260  }
261 
262  // Create the Input node if required
263  PHCompositeNode *InputNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", m_jetInputCategory.c_str()));
264  if (!InputNode)
265  {
266  InputNode = new PHCompositeNode(m_jetInputCategory.c_str());
267  AlgoNode->addNode(InputNode);
268  }
269 
270  JetContainer *jets = findNode::getClass<JetContainer>(topNode, src.m_name);
271  if (!jets)
272  {
273  jets = new JetContainer();
274  PHIODataNode<PHObject> *JetContainerNode = new PHIODataNode<PHObject>(jets, src.m_name.c_str(), "PHObject");
275  InputNode->addNode(JetContainerNode);
276  }
277  }
278 
280 }
281 
284 {
285  std::string histname(Name() + "_HISTOS");
286 
288  Fun4AllHistoManager *hm = se->getHistoManager(histname);
289 
290  if (not hm)
291  {
292  std::cout
293  << "TPCDataStreamEmulator::get_HistoManager - Making Fun4AllHistoManager " << histname
294  << std::endl;
295  hm = new Fun4AllHistoManager(histname);
296  se->registerHistoManager(hm);
297  }
298 
299  assert(hm);
300 
301  return hm;
302 }