Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dNdEtaINTT.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file dNdEtaINTT.cc
1 //____________________________________________________________________________..
2 //
3 // This is a template for a Fun4All SubsysReco module with all methods from the
4 // $OFFLINE_MAIN/include/fun4all/SubsysReco.h baseclass
5 // You do not have to implement all of them, you can just remove unused methods
6 // here and in dNdEtaINTT.h.
7 //
8 // dNdEtaINTT(const std::string &name = "dNdEtaINTT")
9 // everything is keyed to dNdEtaINTT, duplicate names do work but it makes
10 // e.g. finding culprits in logs difficult or getting a pointer to the module
11 // from the command line
12 //
13 // dNdEtaINTT::~dNdEtaINTT()
14 // this is called when the Fun4AllServer is deleted at the end of running. Be
15 // mindful what you delete - you do loose ownership of object you put on the node tree
16 //
17 // int dNdEtaINTT::Init(PHCompositeNode *topNode)
18 // This method is called when the module is registered with the Fun4AllServer. You
19 // can create historgrams here or put objects on the node tree but be aware that
20 // modules which haven't been registered yet did not put antyhing on the node tree
21 //
22 // int dNdEtaINTT::InitRun(PHCompositeNode *topNode)
23 // This method is called when the first event is read (or generated). At
24 // this point the run number is known (which is mainly interesting for raw data
25 // processing). Also all objects are on the node tree in case your module's action
26 // depends on what else is around. Last chance to put nodes under the DST Node
27 // We mix events during readback if branches are added after the first event
28 //
29 // int dNdEtaINTT::process_event(PHCompositeNode *topNode)
30 // called for every event. Return codes trigger actions, you find them in
31 // $OFFLINE_MAIN/include/fun4all/Fun4AllReturnCodes.h
32 // everything is good:
33 // return Fun4AllReturnCodes::EVENT_OK
34 // abort event reconstruction, clear everything and process next event:
35 // return Fun4AllReturnCodes::ABORT_EVENT;
36 // proceed but do not save this event in output (needs output manager setting):
37 // return Fun4AllReturnCodes::DISCARD_EVENT;
38 // abort processing:
39 // return Fun4AllReturnCodes::ABORT_RUN
40 // all other integers will lead to an error and abort of processing
41 //
42 // int dNdEtaINTT::ResetEvent(PHCompositeNode *topNode)
43 // If you have internal data structures (arrays, stl containers) which needs clearing
44 // after each event, this is the place to do that. The nodes under the DST node are cleared
45 // by the framework
46 //
47 // int dNdEtaINTT::EndRun(const int runnumber)
48 // This method is called at the end of a run when an event from a new run is
49 // encountered. Useful when analyzing multiple runs (raw data). Also called at
50 // the end of processing (before the End() method)
51 //
52 // int dNdEtaINTT::End(PHCompositeNode *topNode)
53 // This is called at the end of processing. It needs to be called by the macro
54 // by Fun4AllServer::End(), so do not forget this in your macro
55 //
56 // int dNdEtaINTT::Reset(PHCompositeNode *topNode)
57 // not really used - it is called before the dtor is called
58 //
59 // void dNdEtaINTT::Print(const std::string &what) const
60 // Called from the command line - useful to print information when you need it
61 //
62 //____________________________________________________________________________..
63 
64 #include "dNdEtaINTT.h"
65 
67 #include <fun4all/PHTFileServer.h>
68 #include <phool/PHCompositeNode.h>
69 #include <trackbase/TrkrHitv2.h>
70 
71 namespace
72 {
73 template <class T> void CleanVec(std::vector<T> &v)
74 {
75  std::vector<T>().swap(v);
76  v.shrink_to_fit();
77 }
78 } // namespace
79 
80 //____________________________________________________________________________..
81 dNdEtaINTT::dNdEtaINTT(const std::string &name, const std::string &outputfile, const bool &isData)
82  : SubsysReco(name), _get_truth_pv(true), _get_reco_cluster(true), _get_centrality(true), _get_trkr_hit(true), _outputFile(outputfile), IsData(isData), svtx_evalstack(nullptr), truth_eval(nullptr), clustereval(nullptr), hiteval(nullptr),
83  dst_clustermap(nullptr), clusterhitmap(nullptr), hitsets(nullptr), _tgeometry(nullptr), m_truth_info(nullptr), m_CentInfo(nullptr)
84 {
85  std::cout << "dNdEtaINTT::dNdEtaINTT(const std::string &name) Calling ctor" << std::endl;
86 }
87 
88 //____________________________________________________________________________..
89 dNdEtaINTT::~dNdEtaINTT() { std::cout << "dNdEtaINTT::~dNdEtaINTT() Calling dtor" << std::endl; }
90 
91 //____________________________________________________________________________..
93 {
94  std::cout << "dNdEtaINTT::Init(PHCompositeNode *topNode) Initializing" << std::endl << "Running on Data or simulation? -> IsData = " << IsData << std::endl;
95 
96  PHTFileServer::get().open(_outputFile, "RECREATE");
97  outtree = new TTree("EventTree", "EventTree");
98  outtree->Branch("event", &event_);
99  if (!IsData)
100  {
101  outtree->Branch("ncoll", &ncoll_);
102  outtree->Branch("npart", &npart_);
103  outtree->Branch("centrality_bimp", &centrality_bimp_);
104  outtree->Branch("centrality_impactparam", &centrality_impactparam_);
105  outtree->Branch("centrality_mbd", &centrality_mbd_);
106  outtree->Branch("centrality_mbdquantity", &centrality_mbdquantity_);
107  outtree->Branch("NTruthVtx", &NTruthVtx_);
108  outtree->Branch("TruthPV_trig_x", &TruthPV_trig_x_);
109  outtree->Branch("TruthPV_trig_y", &TruthPV_trig_y_);
110  outtree->Branch("TruthPV_trig_z", &TruthPV_trig_z_);
111  outtree->Branch("NGenPart", &NGenPart_);
112  outtree->Branch("UniqueAncG4P_Pt", &UniqueAncG4P_Pt_);
113  outtree->Branch("UniqueAncG4P_Eta", &UniqueAncG4P_Eta_);
114  outtree->Branch("UniqueAncG4P_Phi", &UniqueAncG4P_Phi_);
115  outtree->Branch("UniqueAncG4P_E", &UniqueAncG4P_E_);
116  outtree->Branch("UniqueAncG4P_PID", &UniqueAncG4P_PID_);
117  }
118 
119  outtree->Branch("NClus_Layer1", &NClus_Layer1_);
120  outtree->Branch("NClus", &NClus_);
121  outtree->Branch("NTrkrhits", &NTrkrhits_);
122  outtree->Branch("TrkrHitRow", &TrkrHitRow_);
123  outtree->Branch("TrkrHitColumn", &TrkrHitColumn_);
124  outtree->Branch("TrkrHitLadderZId", &TrkrHitLadderZId_);
125  outtree->Branch("TrkrHitLadderPhiId", &TrkrHitLadderPhiId_);
126  outtree->Branch("TrkrHitLayer", &TrkrHitLayer_);
127  outtree->Branch("TrkrHitADC", &TrkrHitADC_);
128  outtree->Branch("ClusLayer", &ClusLayer_);
129  outtree->Branch("ClusX", &ClusX_);
130  outtree->Branch("ClusY", &ClusY_);
131  outtree->Branch("ClusZ", &ClusZ_);
132  outtree->Branch("ClusR", &ClusR_);
133  outtree->Branch("ClusPhi", &ClusPhi_);
134  outtree->Branch("ClusEta", &ClusEta_);
135  outtree->Branch("ClusAdc", &ClusAdc_);
136  outtree->Branch("ClusPhiSize", &ClusPhiSize_);
137  outtree->Branch("ClusZSize", &ClusZSize_);
138  outtree->Branch("ClusLadderZId", &ClusLadderZId_);
139  outtree->Branch("ClusLadderPhiId", &ClusLadderPhiId_);
140  outtree->Branch("ClusTrkrHitSetKey", &ClusTrkrHitSetKey_);
141  outtree->Branch("ClusTimeBucketId", &ClusTimeBucketId_);
142 
144 }
145 
146 //____________________________________________________________________________..
148 {
149  std::cout << "dNdEtaINTT::InitRun(PHCompositeNode *topNode) Initializing for Run XXX" << std::endl;
151 }
152 
153 //____________________________________________________________________________..
155 {
156  // std::cout << "dNdEtaINTT::process_event(PHCompositeNode *topNode) Processing Event" << InputFileListIndex * NEvtPerFile + eventNum << std::endl;
157  std::cout << "dNdEtaINTT::process_event(PHCompositeNode *topNode) Processing Event" << eventNum << std::endl;
158 
159  PHNodeIterator nodeIter(topNode);
160 
161  if (!svtx_evalstack)
162  {
163  svtx_evalstack = new SvtxEvalStack(topNode);
167  }
168 
169  svtx_evalstack->next_event(topNode);
170 
171  // eventheader = findNode::getClass<EventHeader>(topNode, "EventHeader");
172  // if (!eventheader)
173  // {
174  // std::cout << PHWHERE << "Error, can't find EventHeader" << std::endl;
175  // return Fun4AllReturnCodes::ABORTEVENT;
176  // }
177 
178  // Centrality info
179  m_CentInfo = findNode::getClass<CentralityInfo>(topNode, "CentralityInfo");
180  if (!m_CentInfo)
181  {
182  std::cout << PHWHERE << "Error, can't find CentralityInfov1" << std::endl;
184  }
185 
186  dst_clustermap = findNode::getClass<TrkrClusterContainerv4>(topNode, "TRKR_CLUSTER");
187  if (!dst_clustermap)
188  {
189  std::cout << PHWHERE << "Error, can't find trkr clusters" << std::endl;
191  }
192 
193  clusterhitmap = findNode::getClass<TrkrClusterHitAssoc>(topNode, "TRKR_CLUSTERHITASSOC");
194  if (!clusterhitmap)
195  {
196  std::cout << PHWHERE << "Error, can't find cluster-hit map" << std::endl;
198  }
199 
200  hitsets = findNode::getClass<TrkrHitSetContainer>(topNode, "TRKR_HITSET");
201  if (!hitsets)
202  {
203  std::cout << PHWHERE << "Error, can't find cluster-hit map" << std::endl;
205  }
206 
207  _tgeometry = findNode::getClass<ActsGeometry>(topNode, "ActsGeometry");
208  if (!_tgeometry)
209  {
210  std::cout << PHWHERE << "Error, can't find Acts geometry" << std::endl;
212  }
213 
214  // Truth information for truth vertex
215  m_truth_info = findNode::getClass<PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
216  if (!m_truth_info)
217  {
218  std::cout << PHWHERE << "Error, can't find G4TruthInfo" << std::endl;
220  }
221 
222  if (!IsData)
223  {
224  std::cout << "Running on simulation" << std::endl;
225 
226  eventheader = findNode::getClass<EventHeader>(topNode, "EventHeader");
227  if (!eventheader)
228  {
229  std::cout << PHWHERE << "Error, can't find EventHeader" << std::endl;
231  }
232 
233  if (_get_truth_pv)
234  GetTruthPVInfo(topNode);
235  }
236 
237  if (_get_centrality)
238  GetCentralityInfo(topNode);
239 
240  if (_get_trkr_hit)
241  GetTrkrHitInfo(topNode);
242 
243  if (_get_reco_cluster)
244  GetRecoClusterInfo(topNode);
245 
246  // event_ = InputFileListIndex * NEvtPerFile + eventNum;
247  event_ = eventNum;
248  outtree->Fill();
249  eventNum++;
250 
251  ResetVectors();
252 
254 }
255 
256 //____________________________________________________________________________..
258 {
259  std::cout << "dNdEtaINTT::ResetEvent(PHCompositeNode *topNode) Resetting internal structures, prepare for next event" << std::endl;
261 }
262 
263 //____________________________________________________________________________..
265 {
266  std::cout << "dNdEtaINTT::EndRun(const int runnumber) Ending Run for Run " << runnumber << std::endl;
268 }
269 
270 //____________________________________________________________________________..
272 {
273  std::cout << "dNdEtaINTT::End(PHCompositeNode *topNode) This is the End - Output to " << _outputFile << std::endl;
274 
276  outtree->Write("", TObject::kOverwrite);
277 
278  delete svtx_evalstack;
279 
281 }
282 
283 //____________________________________________________________________________..
285 {
286  std::cout << "dNdEtaINTT::Reset(PHCompositeNode *topNode) being Reset" << std::endl;
288 }
289 
290 //____________________________________________________________________________..
291 void dNdEtaINTT::Print(const std::string &what) const { std::cout << "dNdEtaINTT::Print(const std::string &what) const Printing info for " << what << std::endl; }
292 //____________________________________________________________________________..
294 {
295  // std::cout << "In GetG4Ancestor: " << std::endl << "start with original particle: ";
296  // p->identify();
297 
299  PHG4Particle *prevpart = p;
300  while (ancestor != nullptr)
301  {
302  // ancestor->identify();
303  prevpart = ancestor;
304  ancestor = m_truth_info->GetParticle(ancestor->get_parent_id());
305  }
306  return prevpart;
307 }
308 //____________________________________________________________________________..
310 {
311  std::cout << "Get centrality info." << std::endl;
312 
313  if (!IsData)
314  {
315  centrality_bimp_ = m_CentInfo->get_centile(CentralityInfo::PROP::bimp);
316  centrality_impactparam_ = m_CentInfo->get_quantity(CentralityInfo::PROP::bimp);
317 
318  // Glauber parameter information
321  }
322  centrality_mbd_ = m_CentInfo->get_centile(CentralityInfo::PROP::mbd_NS);
323  centrality_mbdquantity_ = m_CentInfo->get_quantity(CentralityInfo::PROP::mbd_NS);
324 
325  std::cout << "Centrality: (bimp,impactparam) = (" << centrality_bimp_ << ", " << centrality_impactparam_ << "); (mbd,mbdquantity) = (" << centrality_mbd_ << ", " << centrality_mbdquantity_ << ")" << std::endl;
326  std::cout << "Glauber parameter information: (ncoll,npart) = (" << ncoll_ << ", " << npart_ << ")" << std::endl;
327 }
328 //____________________________________________________________________________..
330 {
331  std::cout << "Get TrkrHit info." << std::endl;
332 
334  for (TrkrHitSetContainer::ConstIterator hitset_iter = hitset_range.first; hitset_iter != hitset_range.second; ++hitset_iter)
335  {
336  TrkrHitSet::ConstRange hit_range = hitset_iter->second->getHits();
337  for (TrkrHitSet::ConstIterator hit_iter = hit_range.first; hit_iter != hit_range.second; ++hit_iter)
338  {
339  TrkrDefs::hitkey hitKey = hit_iter->first;
340  TrkrDefs::hitsetkey hitSetKey = hitset_iter->first;
341 
342  TrkrHitRow_.push_back(InttDefs::getRow(hitKey));
343  TrkrHitColumn_.push_back(InttDefs::getCol(hitKey));
344  TrkrHitLadderZId_.push_back(InttDefs::getLadderZId(hitSetKey));
345  TrkrHitLadderPhiId_.push_back(InttDefs::getLadderPhiId(hitSetKey));
346  TrkrHitLayer_.push_back(TrkrDefs::getLayer(hitSetKey));
347  TrkrHitADC_.push_back(hit_iter->second->getAdc());
348  }
349  }
350  NTrkrhits_ = TrkrHitRow_.size();
351 }
352 //____________________________________________________________________________..
354 {
355  std::cout << "Get reconstructed cluster info." << std::endl;
356 
357  std::vector<int> _NClus = {0, 0};
358  std::map<int, unsigned int> AncG4P_cluster_count;
359  AncG4P_cluster_count.clear();
360 
361  // Reference:
362  // https://github.com/sPHENIX-Collaboration/coresoftware/blob/master/simulation/g4simulation/g4eval/SvtxEvaluator.cc#L1731-L1984
363  // for (const auto &hitsetkey : dst_clustermap->getHitSetKeys())
365  {
366  auto range = dst_clustermap->getClusters(hitsetkey);
367  for (auto iter = range.first; iter != range.second; ++iter)
368  {
369  // std::cout << "----------" << std::endl;
370  TrkrDefs::cluskey ckey = iter->first;
371  TrkrCluster *cluster = dst_clustermap->findCluster(ckey);
372 
373  unsigned int trkrId = TrkrDefs::getTrkrId(ckey);
374  if (trkrId != TrkrDefs::inttId)
375  continue; // we want only INTT clusters
376 
377  int layer = (TrkrDefs::getLayer(ckey) == 3 || TrkrDefs::getLayer(ckey) == 4) ? 0 : 1;
378  _NClus[layer]++;
379  if (cluster == nullptr)
380  {
381  std::cout << "cluster is nullptr, ckey=" << ckey << std::endl;
382  }
383  auto globalpos = _tgeometry->getGlobalPosition(ckey, cluster);
384  ClusLayer_.push_back(TrkrDefs::getLayer(ckey));
385  ClusX_.push_back(globalpos(0));
386  ClusY_.push_back(globalpos(1));
387  ClusZ_.push_back(globalpos(2));
388  ClusAdc_.push_back(cluster->getAdc());
389  TVector3 pos(globalpos(0), globalpos(1), globalpos(2));
390  ClusR_.push_back(pos.Perp());
391  ClusPhi_.push_back(pos.Phi());
392  ClusEta_.push_back(pos.Eta());
393  ClusPhiSize_.push_back(cluster->getPhiSize());
394  ClusZSize_.push_back(cluster->getZSize());
395  ClusLadderZId_.push_back(InttDefs::getLadderZId(ckey));
397  ClusTrkrHitSetKey_.push_back(hitsetkey);
399  if (!IsData)
400  {
401  // std::cout << "This cluster: (x,y,z)=(" << globalpos(0) << "," << globalpos(1) << "," << globalpos(2) << ")" << std::endl;
403  // if (g4p) g4p->identify();
404 
405  if (g4p)
406  {
407  PHG4Particle *g4pancestor = GetG4PAncestor(g4p);
408 
409  if (g4pancestor && AncG4P_cluster_count[g4pancestor->get_track_id()] == 0)
410  {
411  UniqueAncG4P_PID_.push_back(g4pancestor->get_pid());
412  TLorentzVector g4pAncvec;
413  g4pAncvec.SetPxPyPzE(g4pancestor->get_px(), g4pancestor->get_py(), g4pancestor->get_pz(), g4pancestor->get_e());
414  UniqueAncG4P_E_.push_back(g4pancestor->get_e());
415  UniqueAncG4P_Pt_.push_back(g4pAncvec.Pt());
416  UniqueAncG4P_Eta_.push_back(g4pAncvec.Eta());
417  UniqueAncG4P_Phi_.push_back(g4pAncvec.Phi());
418 
419  ++AncG4P_cluster_count[g4pancestor->get_track_id()];
420  }
421  }
422  }
423  }
424  }
425 
426  NClus_ = _NClus[0] + _NClus[1];
427  NClus_Layer1_ = _NClus[0];
428  std::cout << "Number of clusters (total,0,1)=(" << NClus_ << "," << _NClus[0] << "," << _NClus[1] << ")" << std::endl;
429  NGenPart_ = UniqueAncG4P_PID_.size();
430 
431  int uniqueAncG4P = 0, N_MatchedClus = 0;
432  for (auto it = AncG4P_cluster_count.cbegin(); it != AncG4P_cluster_count.cend(); ++it)
433  {
434  // std::cout << "(Ancestor G4P trackID, matched cluster counts)=(" << it->first << "," << it->second << ")" << std::endl;
435  uniqueAncG4P++;
436  N_MatchedClus += it->second;
437  }
438  std::cout << "(# of unique ancestor G4P, # of matched clusters, ratio)=(" << uniqueAncG4P << "," << N_MatchedClus << "," << (float)uniqueAncG4P / N_MatchedClus << ")" << std::endl;
439 }
440 //____________________________________________________________________________..
442 {
443  std::cout << "Get truth primary vertex info." << std::endl;
444 
445  auto vrange = m_truth_info->GetPrimaryVtxRange();
446 
447  int NTruthPV = 0;
448  for (auto iter = vrange.first; iter != vrange.second; ++iter) // process all primary vertices
449  {
450  const int point_id = iter->first;
451  PHG4VtxPoint *point = iter->second;
452 
453  if (point)
454  {
455  if (m_truth_info->isEmbededVtx(point_id) == 0)
456  {
457  TruthPV_trig_x_ = point->get_x();
458  TruthPV_trig_y_ = point->get_y();
459  TruthPV_trig_z_ = point->get_z();
460  }
461 
462  NTruthPV++;
463  }
464  }
465 
466  // print out the truth vertex information
467  std::cout << "Number of truth primary vertices = " << NTruthPV << "; Triggered truth vtx pos (x,y,z)=(" << TruthPV_trig_x_ << "," << TruthPV_trig_y_ << "," << TruthPV_trig_z_ << ")" << std::endl;
468 
469  NTruthVtx_ = NTruthPV;
470 }
471 //____________________________________________________________________________..
473 {
474  // CleanVec(ClusHitcount_);
476  CleanVec(ClusX_);
477  CleanVec(ClusY_);
478  CleanVec(ClusZ_);
479  CleanVec(ClusR_);
500 }