star-travex
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
stiscan.cc
Go to the documentation of this file.
1 #include <fstream>
2 #include <string>
3 #include <unordered_map>
4 
5 #include "TChain.h"
6 #include "TError.h"
7 #include "TGeoNavigator.h"
8 #include "TGeoManager.h"
9 #include "TRandom.h"
10 
11 #include "GeaRootIO/TGeaEvent.h"
12 #include "StiScan/StiScanEvent.h"
15 
16 typedef std::unordered_map<size_t, std::string> Hash2StringMap;
17 
18 TGeoManager *gGeoManager = nullptr;
19 
20 
21 void loop_over_tree(StiScanPrgOptions &prgOpts);
22 void create_volume_hash_map(TGeoNavigator &geoNav, Hash2StringMap &hash2PathMap);
23 
24 
25 int main(int argc, char **argv)
26 {
27  const std::string stiTreeName = "t";
28  const std::string geantStepTreeName = "stepping";
29 
30  StiScanPrgOptions prgOpts(argc, argv, stiTreeName, geantStepTreeName);
31  prgOpts.ProcessOptions();
32 
33  // Initialize gGeoManager with geometry from a ROOT file
34  TGeoManager::Import(prgOpts.PathToGeometryFile().c_str());
35 
36  if (!gGeoManager)
37  Fatal(__PRETTY_FUNCTION__, "Cannot load TGeo geometry from %s", prgOpts.PathToGeometryFile().c_str());
38 
39  gGeoManager->cd("HALL_1/CAVE_1");
40  TGeoNavigator* geoNav = gGeoManager->GetCurrentNavigator();
41  assert(geoNav);
42 
43  Hash2StringMap hash2PathMap;
44  create_volume_hash_map(*geoNav, hash2PathMap);
45 
46  TGeaEvent::fgHash2PathMap = &hash2PathMap;
47 
48  loop_over_tree(prgOpts);
49 
50  return EXIT_SUCCESS;
51 }
52 
53 
55 {
56  TChain *stiChain = prgOpts.GetStiTChain();
57  TChain *geantStepChain = prgOpts.GetGeantStepChain();
58 
59  StiScanRootFile outRootFile(prgOpts, "recreate");
60 
61  int nTreeEvents = stiChain->GetEntries();
62  int nProcEvents = 0;
63 
64  Info("loop_over_tree", "Found tree/chain with N entries: %d", nTreeEvents);
65 
66  nTreeEvents = (prgOpts.GetMaxEventsUser() < nTreeEvents) ? prgOpts.GetMaxEventsUser() : nTreeEvents;
67 
68  Info("loop_over_tree", "Will process %d events", nTreeEvents);
69 
70  StiScanEvent *stiScanEvent = new StiScanEvent();
71  stiChain->SetBranchAddress("e.", &stiScanEvent);
72  stiChain->SetBranchStatus("e.TStiEvent*", false);
73  stiChain->SetBranchStatus("e.TStiEvent.fTStiKalmanTracks*", true);
74 
75 
76  // Prepare resources for geant event
77  TGeaEvent *geantEvent = new TGeaEvent();
78 
79  if (prgOpts.DoGeantStepTree())
80  geantStepChain->SetBranchAddress("TGeaEvent", &geantEvent);
81 
82  TRandom myRandom;
83 
84  Info("loop_over_tree", "Looping over tree/chain...");
85 
86  for (int iEvent = 1; iEvent <= nTreeEvents; iEvent++, nProcEvents++)
87  {
88  if ( nTreeEvents >= 10 && iEvent % int(nTreeEvents*0.1) == 0 )
89  Info("loop_over_tree", "Analyzing event %d", iEvent);
90 
91  if (myRandom.Rndm() > prgOpts.GetSparsity()) continue;
92 
93  stiChain->GetEntry(iEvent-1);
94 
95  outRootFile.FillHists(*stiScanEvent, &prgOpts.GetVolumeList());
96 
97  if (prgOpts.DoGeantStepTree()) {
98  geantStepChain->GetEntry(iEvent-1);
99  outRootFile.FillHists(*geantEvent, &prgOpts.GetVolumeList());
100  }
101  }
102 
103  delete stiScanEvent;
104  delete geantEvent;
105 
106  outRootFile.Finalize();
107  outRootFile.Write();
108  outRootFile.Close();
109 }
110 
111 
112 void create_volume_hash_map(TGeoNavigator &geoNav, Hash2StringMap &hash2PathMap)
113 {
114  TGeoNode* currNode = geoNav.GetCurrentNode();
115 
116  if (!currNode) {
117  Warning("create_volume_hash_map", "Invalid TGeoNode provided as input. Skipping...");
118  return;
119  }
120 
121  // Keep track of current level with respect to the original node
122  static int level = 0;
123 
124  std::string currentPath( geoNav.GetPath() );
125 
126  int nDaughters = currNode->GetVolume()->GetNdaughters();
127 
128  if (nDaughters) {
129 
130  TGeoNode* motherNode = currNode;
131  for (int iDaughter = 0; iDaughter < nDaughters; iDaughter++)
132  {
133  TGeoNode *daughter = motherNode->GetVolume()->GetNode(iDaughter);
134 
135  geoNav.CdDown(daughter);
136  level++;
137 
138  create_volume_hash_map(geoNav, hash2PathMap);
139  }
140 
141  } else { // We are here if this node is a leaf, i.e. no daughters
142 
143  // Add this volume to the hash map
144  std::string hashedPath(currentPath);
145 
146  // Remove a "TpcRefSys_1/" substring as it not relevant for geometry trees used in simulation
147  size_t first_pos = hashedPath.find("TpcRefSys_1/");
148  if (first_pos != std::string::npos) {
149  hashedPath.replace(first_pos, std::string("TpcRefSys_1/").length(), "");
150  }
151 
152  std::hash<std::string> hash_fn;
153  std::size_t hash_value = hash_fn(hashedPath);
154 
155  std::pair<size_t, std::string> hash2Path(hash_value, hashedPath);
156 
157  hash2PathMap.insert(hash2Path);
158 
159  geoNav.cd(currentPath.c_str());
160  }
161 
162  if (level > 0) {
163  geoNav.CdUp();
164  level--;
165  }
166 }
static void * fgHash2PathMap
Definition: TGeaEvent.h:24
std::unordered_map< size_t, std::string > Hash2StringMap
Definition: stiscan.cc:16
void FillHists(const StiScanEvent &stiEvent, const std::set< std::string > *volumeList=0)
int main(int argc, char **argv)
Definition: stiscan.cc:25
Processes and controls user options provided in the command line.
void ProcessOptions()
Takes the standard command line arguments and parses them with the boost program_options utility...
bool DoGeantStepTree() const
TGeoManager * gGeoManager
Definition: stiscan.cc:18
void loop_over_tree(StiScanPrgOptions &prgOpts)
Definition: stiscan.cc:54
std::string PathToGeometryFile() const
TChain * GetGeantStepChain()
const std::set< std::string > & GetVolumeList() const
void create_volume_hash_map(TGeoNavigator &geoNav, Hash2StringMap &hash2PathMap)
Definition: stiscan.cc:112