DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Geant4ROOTDump.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 // Framework include files
15 #include <DD4hep/Detector.h>
16 #include <DD4hep/Factories.h>
17 #include <DD4hep/Primitives.h>
18 #include <DDG4/Geant4DataDump.h>
19 #include <DDG4/Geant4Particle.h>
20 
21 // ROOT include files
22 #include <TInterpreter.h>
23 #include <TSystem.h>
24 #include <TFile.h>
25 #include <TTree.h>
26 #include <TROOT.h>
27 
28 static long usage() {
29  using namespace dd4hep;
30  printout(FATAL,"Geant4ROOTDump","usage: Geant4ROOTDump -opt (app-opts) --opt=value (plugin-opts)");
31  printout(FATAL,"Geant4ROOTDump"," app-opts: ");
32  printout(FATAL,"Geant4ROOTDump"," -print INFO Print container summaries only.");
33  printout(FATAL,"Geant4ROOTDump"," -print DEBUG Print container content as well.");
34  printout(FATAL,"Geant4ROOTDump"," -print VERBOSE Print full data.");
35  printout(FATAL,"Geant4ROOTDump"," plugin opts: ");
36  printout(FATAL,"Geant4ROOTDump"," --usage: Print this output.");
37  printout(FATAL,"Geant4ROOTDump"," --input=<file> Print content of this file.");
38  printout(FATAL,"Geant4ROOTDump"," --entry=<number> Print specified event in the file. (starts with 0!)");
39  return 'H';
40 }
41 
42 static std::pair<TClass*,void*> load(TBranch* branch, int entry) {
43  TClass* cl = gROOT->GetClass(branch->GetClassName(),kTRUE);
44  if ( !cl ) {
45  return std::pair<TClass*,void*>(0,0);
46  }
47  void *obj = cl->New();
48  branch->SetAddress(&obj);
49  branch->SetAutoDelete(kFALSE);
50  int nb = branch->GetEntry(entry);
51  if ( nb < 0 ) {
52  cl->Destructor(obj);
53  return std::pair<TClass*,void*>(0,0);
54  }
55  return std::pair<TClass*,void*>(cl,obj);
56 }
57 
58 static long dump_root(dd4hep::Detector&, int argc, char** argv) {
59  using namespace dd4hep;
60  std::string input = "", tag="Geant4ROOTDump";
61  int entry = -1;
62 
63  for(int j=0; j<argc; ++j) {
64  std::string a = argv[j];
65  if ( a[0]=='-' ) a = argv[j]+1;
66  if ( a[0]=='-' ) a = argv[j]+2;
67  size_t idx = a.find('=');
68  if ( strncmp(a.c_str(),"usage",5)==0 ) {
69  usage();
70  return 1;
71  }
72  if ( idx > 0 ) {
73  std::string p1 = a.substr(0,idx);
74  std::string p2 = a.substr(idx+1);
75  if ( strncmp(p1.c_str(),"input",3)==0 ) {
76  input = p2;
77  }
78  if ( strncmp(p1.c_str(),"entry",5)==0 ) {
79  if ( 1 != ::sscanf(p2.c_str(),"%d",&entry) ) {
80  printout(FATAL,tag,"+++ Argument %s is not properly formatted. must be --entry=<number>.",argv[j]);
81  return usage();
82  }
83  }
84  continue;
85  }
86  printout(FATAL,tag,"+++ Argument %s is IGNORED! No value is found (string has no '=')",argv[j]);
87  return usage();
88  }
89  const char* line = "+----------------------------------------------------------------------------------+";
90  printout(INFO,tag,line);
91  printout(INFO,tag,"| Input:%-74s|",input.c_str());
92  printout(INFO,tag,line);
93 
94  if ( input.empty() ) {
95  return usage();
96  }
97 
98  dd4hep::sim::Geant4DataDump dump("Geant4Data");
99  TFile* f = TFile::Open(input.c_str());
100 
101  if ( f && !f->IsZombie() ) {
102  TTree* tree = (TTree*)f->Get("EVENT");
103  TClass* cl_calo = gROOT->GetClass(typeid(dd4hep::sim::Geant4DataDump::CalorimeterHits));
104  TClass* cl_tracker = gROOT->GetClass(typeid(dd4hep::sim::Geant4DataDump::TrackerHits));
105  TClass* cl_particles = gROOT->GetClass(typeid(dd4hep::sim::Geant4DataDump::Particles));
106  TObjArray* branches = tree->GetListOfBranches();
107  Int_t nbranches = branches->GetEntriesFast();
108  typedef std::pair<TClass*,void*> ENTRY;
109  typedef std::map<std::string,ENTRY> ENTRIES;
110 
111  for(Int_t ievt=entry<0 ? 0 : entry, nevt=entry<0 ? tree->GetEntries() : entry+1; ievt<nevt; ++ievt) {
112  ENTRIES event;
113  printout(INFO,tag,line);
114  printout(INFO,tag,"| Event: %6d |",ievt);
115  printout(INFO,tag,line);
116 
117  // First suck in all data
118  for (Int_t i=0;i<nbranches;i++) {
119  TBranch* branch = (TBranch*)branches->UncheckedAt(i);
120  std::pair<TClass*,void*> data = load(branch,ievt);
121  if ( data.first ) event[branch->GetName()] = std::move(data);
122  }
123  // Now dump the stuff
124  for(ENTRIES::const_iterator i=event.begin(); i!=event.end(); ++i) {
125  std::pair<TClass*,void*> data = (*i).second;
126  if ( data.first == cl_particles ) {
127  auto* parts = (dd4hep::sim::Geant4DataDump::Particles*)data.second;
128  dump.print(INFO, (*i).first, parts);
129  for_each(parts->begin(), parts->end(), detail::DestroyObject<dd4hep::sim::Geant4Particle*>());
130  }
131  }
132  for(ENTRIES::const_iterator i=event.begin(); i!=event.end(); ++i) {
133  std::pair<TClass*,void*> data = (*i).second;
134  if ( data.first == cl_particles ) {
135  }
136  else if ( data.first == cl_tracker ) {
137  auto* hits = (dd4hep::sim::Geant4DataDump::TrackerHits*)data.second;
138  dump.print(INFO, (*i).first, hits);
139  for_each(hits->begin(), hits->end(), detail::DestroyObject<dd4hep::sim::Geant4Tracker::Hit*>());
140  }
141  else if ( data.first == cl_calo ) {
142  auto* hits = (dd4hep::sim::Geant4DataDump::CalorimeterHits*)data.second;
143  dump.print(INFO, (*i).first, hits);
144  for_each(hits->begin(), hits->end(), detail::DestroyObject<dd4hep::sim::Geant4Calorimeter::Hit*>());
145  }
146  if ( data.first ) data.first->Destructor(data.second);
147  }
148  }
149  delete f;
150  }
151  else {
152  printout(FATAL,tag,"+++ FAILED to open input file:%s",input.c_str());
153  return usage();
154  }
155  return 1;
156 }
157 
158 DECLARE_APPLY(Geant4ROOTDump,dump_root)
Detector.h
dd4hep::sim::Geant4DataDump
Class to dump the records of the intrinsic Geant4 event model.
Definition: Geant4DataDump.h:37
DECLARE_APPLY
#define DECLARE_APPLY(name, func)
Definition: Factories.h:281
Factories.h
Geant4DataDump.h
dd4hep::sim::Geant4DataDump::CalorimeterHits
std::vector< SimpleCalorimeter::Hit * > CalorimeterHits
Definition: Geant4DataDump.h:46
dd4hep::sim::Geant4DataDump::TrackerHits
std::vector< SimpleTracker::Hit * > TrackerHits
Definition: Geant4DataDump.h:43
Primitives.h
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
Geant4Particle.h
dd4hep::Detector
The main interface to the dd4hep detector description package.
Definition: Detector.h:90
dd4hep::sim::Geant4DataDump::Particles
std::vector< Particle * > Particles
Definition: Geant4DataDump.h:40
usage
void usage(std::string argv0)
Definition: listcomponents.cpp:41