File indexing completed on 2025-01-30 09:17:26
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <DD4hep/Detector.h>
0016 #include <DD4hep/Factories.h>
0017 #include <DD4hep/Primitives.h>
0018 #include <DDG4/Geant4DataDump.h>
0019 #include <DDG4/Geant4Particle.h>
0020
0021
0022 #include <TInterpreter.h>
0023 #include <TSystem.h>
0024 #include <TFile.h>
0025 #include <TTree.h>
0026 #include <TROOT.h>
0027
0028 static long usage() {
0029 using namespace dd4hep;
0030 printout(FATAL,"Geant4ROOTDump","usage: Geant4ROOTDump -opt (app-opts) --opt=value (plugin-opts)");
0031 printout(FATAL,"Geant4ROOTDump"," app-opts: ");
0032 printout(FATAL,"Geant4ROOTDump"," -print INFO Print container summaries only.");
0033 printout(FATAL,"Geant4ROOTDump"," -print DEBUG Print container content as well.");
0034 printout(FATAL,"Geant4ROOTDump"," -print VERBOSE Print full data.");
0035 printout(FATAL,"Geant4ROOTDump"," plugin opts: ");
0036 printout(FATAL,"Geant4ROOTDump"," --usage: Print this output.");
0037 printout(FATAL,"Geant4ROOTDump"," --input=<file> Print content of this file.");
0038 printout(FATAL,"Geant4ROOTDump"," --entry=<number> Print specified event in the file. (starts with 0!)");
0039 return 'H';
0040 }
0041
0042 static std::pair<TClass*,void*> load(TBranch* branch, int entry) {
0043 TClass* cl = gROOT->GetClass(branch->GetClassName(),kTRUE);
0044 if ( !cl ) {
0045 return std::pair<TClass*,void*>(0,0);
0046 }
0047 void *obj = cl->New();
0048 branch->SetAddress(&obj);
0049 branch->SetAutoDelete(kFALSE);
0050 int nb = branch->GetEntry(entry);
0051 if ( nb < 0 ) {
0052 cl->Destructor(obj);
0053 return std::pair<TClass*,void*>(0,0);
0054 }
0055 return std::pair<TClass*,void*>(cl,obj);
0056 }
0057
0058 static long dump_root(dd4hep::Detector&, int argc, char** argv) {
0059 using namespace dd4hep;
0060 std::string input = "", tag="Geant4ROOTDump";
0061 int entry = -1;
0062
0063 for(int j=0; j<argc; ++j) {
0064 std::string a = argv[j];
0065 if ( a[0]=='-' ) a = argv[j]+1;
0066 if ( a[0]=='-' ) a = argv[j]+2;
0067 size_t idx = a.find('=');
0068 if ( strncmp(a.c_str(),"usage",5)==0 ) {
0069 usage();
0070 return 1;
0071 }
0072 if ( idx > 0 ) {
0073 std::string p1 = a.substr(0,idx);
0074 std::string p2 = a.substr(idx+1);
0075 if ( strncmp(p1.c_str(),"input",3)==0 ) {
0076 input = p2;
0077 }
0078 if ( strncmp(p1.c_str(),"entry",5)==0 ) {
0079 if ( 1 != ::sscanf(p2.c_str(),"%d",&entry) ) {
0080 printout(FATAL,tag,"+++ Argument %s is not properly formatted. must be --entry=<number>.",argv[j]);
0081 return usage();
0082 }
0083 }
0084 continue;
0085 }
0086 printout(FATAL,tag,"+++ Argument %s is IGNORED! No value is found (string has no '=')",argv[j]);
0087 return usage();
0088 }
0089 const char* line = "+----------------------------------------------------------------------------------+";
0090 printout(INFO,tag,line);
0091 printout(INFO,tag,"| Input:%-74s|",input.c_str());
0092 printout(INFO,tag,line);
0093
0094 if ( input.empty() ) {
0095 return usage();
0096 }
0097
0098 dd4hep::sim::Geant4DataDump dump("Geant4Data");
0099 TFile* f = TFile::Open(input.c_str());
0100
0101 if ( f && !f->IsZombie() ) {
0102 TTree* tree = (TTree*)f->Get("EVENT");
0103 TClass* cl_calo = gROOT->GetClass(typeid(dd4hep::sim::Geant4DataDump::CalorimeterHits));
0104 TClass* cl_tracker = gROOT->GetClass(typeid(dd4hep::sim::Geant4DataDump::TrackerHits));
0105 TClass* cl_particles = gROOT->GetClass(typeid(dd4hep::sim::Geant4DataDump::Particles));
0106 TObjArray* branches = tree->GetListOfBranches();
0107 Int_t nbranches = branches->GetEntriesFast();
0108 typedef std::pair<TClass*,void*> ENTRY;
0109 typedef std::map<std::string,ENTRY> ENTRIES;
0110
0111 for(Int_t ievt=entry<0 ? 0 : entry, nevt=entry<0 ? tree->GetEntries() : entry+1; ievt<nevt; ++ievt) {
0112 ENTRIES event;
0113 printout(INFO,tag,line);
0114 printout(INFO,tag,"| Event: %6d |",ievt);
0115 printout(INFO,tag,line);
0116
0117
0118 for (Int_t i=0;i<nbranches;i++) {
0119 TBranch* branch = (TBranch*)branches->UncheckedAt(i);
0120 std::pair<TClass*,void*> data = load(branch,ievt);
0121 if ( data.first ) event[branch->GetName()] = std::move(data);
0122 }
0123
0124 for(ENTRIES::const_iterator i=event.begin(); i!=event.end(); ++i) {
0125 std::pair<TClass*,void*> data = (*i).second;
0126 if ( data.first == cl_particles ) {
0127 auto* parts = (dd4hep::sim::Geant4DataDump::Particles*)data.second;
0128 dump.print(INFO, (*i).first, parts);
0129 for_each(parts->begin(), parts->end(), detail::DestroyObject<dd4hep::sim::Geant4Particle*>());
0130 }
0131 }
0132 for(ENTRIES::const_iterator i=event.begin(); i!=event.end(); ++i) {
0133 std::pair<TClass*,void*> data = (*i).second;
0134 if ( data.first == cl_particles ) {
0135 }
0136 else if ( data.first == cl_tracker ) {
0137 auto* hits = (dd4hep::sim::Geant4DataDump::TrackerHits*)data.second;
0138 dump.print(INFO, (*i).first, hits);
0139 for_each(hits->begin(), hits->end(), detail::DestroyObject<dd4hep::sim::Geant4Tracker::Hit*>());
0140 }
0141 else if ( data.first == cl_calo ) {
0142 auto* hits = (dd4hep::sim::Geant4DataDump::CalorimeterHits*)data.second;
0143 dump.print(INFO, (*i).first, hits);
0144 for_each(hits->begin(), hits->end(), detail::DestroyObject<dd4hep::sim::Geant4Calorimeter::Hit*>());
0145 }
0146 if ( data.first ) data.first->Destructor(data.second);
0147 }
0148 }
0149 delete f;
0150 }
0151 else {
0152 printout(FATAL,tag,"+++ FAILED to open input file:%s",input.c_str());
0153 return usage();
0154 }
0155 return 1;
0156 }
0157
0158 DECLARE_APPLY(Geant4ROOTDump,dump_root)