DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Geant4Output2ROOT.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/Printout.h>
16 #include <DD4hep/Primitives.h>
17 #include <DD4hep/InstanceCount.h>
19 #include <DDG4/Geant4Output2ROOT.h>
20 #include <DDG4/Geant4Particle.h>
21 #include <DDG4/Geant4Data.h>
22 
23 // Geant4 include files
24 #include <G4HCofThisEvent.hh>
25 #include <G4ParticleTable.hh>
26 #include <G4Run.hh>
27 
28 // ROOT include files
29 #include <TFile.h>
30 #include <TTree.h>
31 #include <TBranch.h>
32 #include <TSystem.h>
33 
34 
35 using namespace dd4hep::sim;
36 
39  : Geant4OutputAction(ctxt, nam), m_file(nullptr), m_tree(nullptr) {
40  declareProperty("Section", m_section = "EVENT");
41  declareProperty("HandleMCTruth", m_handleMCTruth = true);
42  declareProperty("DisabledCollections", m_disabledCollections);
43  declareProperty("DisableParticles", m_disableParticles);
44  declareProperty("FilesByRun", m_filesByRun = false);
46 }
47 
50  closeOutput();
52 }
53 
56  if (m_file) {
57  TDirectory::TContext ctxt(m_file);
58  Sections::iterator i = m_sections.find(m_section);
59  info("+++ Closing ROOT output file %s", m_file->GetName());
60  if ( i != m_sections.end() )
61  m_sections.erase(i);
62  m_branches.clear();
63  m_tree->Write();
64  m_file->Close();
65  m_tree = nullptr;
66  detail::deletePtr (m_file);
67  }
68 }
69 
71 TTree* Geant4Output2ROOT::section(const std::string& nam) {
72  Sections::const_iterator i = m_sections.find(nam);
73  if (i == m_sections.end()) {
74  TDirectory::TContext ctxt(m_file);
75  TTree* t = new TTree(nam.c_str(), ("Geant4 " + nam + " information").c_str());
76  m_sections.emplace(nam, t);
77  return t;
78  }
79  return (*i).second;
80 }
81 
83 void Geant4Output2ROOT::beginRun(const G4Run* run) {
84  std::string fname = m_output;
85  if ( m_filesByRun ) {
86  size_t idx = m_output.rfind(".");
87  if ( m_file ) {
88  closeOutput();
89  }
90  fname = m_output.substr(0, idx);
91  fname += _toString(run->GetRunID(), ".run%08d");
92  if ( idx != std::string::npos )
93  fname += m_output.substr(idx);
94  }
95  if ( !m_file && !fname.empty() ) {
96  TDirectory::TContext ctxt(TDirectory::CurrentDirectory());
97  if ( !gSystem->AccessPathName(fname.c_str()) ) {
98  gSystem->Unlink(fname.c_str());
99  }
100  std::unique_ptr<TFile> file(TFile::Open(fname.c_str(), "RECREATE", "dd4hep Simulation data"));
101  if ( !file ) {
102  file.reset(TFile::Open((fname+".1").c_str(), "RECREATE", "dd4hep Simulation data"));
103  }
104  if ( !file ) {
105  except("Failed to create ROOT output file:'%s'", fname.c_str());
106  }
107  if (file->IsZombie()) {
108  detail::deletePtr (m_file);
109  except("Failed to open ROOT output file:'%s'", fname.c_str());
110  }
111  m_file = file.release();
113  }
115 }
116 
118 int Geant4Output2ROOT::fill(const std::string& nam, const ComponentCast& type, void* ptr) {
119  if (m_file) {
120  TBranch* b = 0;
121  Branches::const_iterator i = m_branches.find(nam);
122  if (i == m_branches.end()) {
123  const std::type_info& typ = type.type();
124  TClass* cl = TBuffer::GetClass(typ);
125  if (cl) {
126  b = m_tree->Branch(nam.c_str(), cl->GetName(), (void*) 0);
127  b->SetAutoDelete(false);
128  m_branches.emplace(nam, b);
129  }
130  else {
131  throw std::runtime_error("No ROOT TClass object availible for object type:" + typeName(typ));
132  }
133  }
134  else {
135  b = (*i).second;
136  }
137  Long64_t evt = b->GetEntries(), nevt = b->GetTree()->GetEntries(), num = nevt - evt;
138  if (nevt > evt) {
139  b->SetAddress(0);
140  while (num > 0) {
141  b->Fill();
142  --num;
143  }
144  }
145  b->SetAddress(&ptr);
146  int nbytes = b->Fill();
147  if (nbytes < 0) {
148  throw std::runtime_error("Failed to write ROOT collection:" + nam + "!");
149  }
150  return nbytes;
151  }
152  return 0;
153 }
154 
157  if (m_file) {
158  TObjArray* a = m_tree->GetListOfBranches();
159  Long64_t evt = m_tree->GetEntries() + 1;
160  Int_t nb = a->GetEntriesFast();
162  for (Int_t i = 0; i < nb; ++i) {
163  TBranch* br_ptr = (TBranch*) a->UncheckedAt(i);
164  Long64_t br_evt = br_ptr->GetEntries();
165  if (br_evt < evt) {
166  Long64_t num = evt - br_evt;
167  br_ptr->SetAddress(0);
168  while (num > 0) {
169  br_ptr->Fill();
170  --num;
171  }
172  }
173  }
174  m_tree->SetEntries(evt);
175  }
177 }
178 
181  if ( !m_disableParticles ) {
183  if ( parts ) {
184  typedef Geant4HitWrapper::HitManipulator Manip;
185  typedef Geant4ParticleMap::ParticleMap ParticleMap;
186  Manip* manipulator = Geant4HitWrapper::manipulator<Geant4Particle>();
187  G4ParticleTable* table = G4ParticleTable::GetParticleTable();
188  const ParticleMap& pm = parts->particles();
189  std::vector<void*> particles;
190  particles.reserve(pm.size());
191  for ( const auto& i : pm ) {
192  auto* p = i.second;
193  G4ParticleDefinition* def = table->FindParticle(p->pdgID);
194  p->charge = int(3.0 * (def ? def->GetPDGCharge() : -1.0)); // Assume e-/pi-
195  particles.emplace_back((ParticleMap::mapped_type*)p);
196  }
197  fill("MCParticles",manipulator->vec_type,&particles);
198  }
199  }
200 }
201 
204  Geant4HitCollection* coll = dynamic_cast<Geant4HitCollection*>(collection);
205  std::string hc_nam = collection->GetName();
206  for(const auto& n : m_disabledCollections) {
207  if ( n == hc_nam ) {
208  return;
209  }
210  }
211  if (coll) {
212  std::vector<void*> hits;
213  coll->getHitsUnchecked(hits);
214  size_t nhits = coll->GetSize();
215  if ( m_handleMCTruth && m_truth && nhits > 0 ) {
216  hits.reserve(nhits);
217  try {
218  for(size_t i=0; i<nhits; ++i) {
219  Geant4HitData* h = coll->hit(i);
220  Geant4Tracker::Hit* trk_hit = dynamic_cast<Geant4Tracker::Hit*>(h);
221  if ( 0 != trk_hit ) {
222  Geant4HitData::Contribution& t = trk_hit->truth;
223  int trackID = t.trackID;
224  t.trackID = m_truth->particleID(trackID);
225  }
226  Geant4Calorimeter::Hit* cal_hit = dynamic_cast<Geant4Calorimeter::Hit*>(h);
227  if ( 0 != cal_hit ) {
228  Geant4HitData::Contributions& c = cal_hit->truth;
229  for(Geant4HitData::Contributions::iterator j=c.begin(); j!=c.end(); ++j) {
231  int trackID = t.trackID;
232  t.trackID = m_truth->particleID(trackID);
233  }
234  }
235  }
236  }
237  catch(...) {
238  error("+++ Exception while saving collection %s.",hc_nam.c_str());
239  }
240  }
241  fill(hc_nam, coll->vector_type(), &hits);
242  }
243 }
dd4hep::sim::Geant4HitCollection::GetSize
virtual size_t GetSize() const override
Access the collection size.
Definition: Geant4HitCollection.h:321
Geant4HitCollection.h
dd4hep::sim::Geant4Output2ROOT::saveEvent
virtual void saveEvent(OutputContext< G4Event > &ctxt) override
Callback to store the Geant4 event.
Definition: Geant4Output2ROOT.cpp:180
dd4hep::sim::Geant4ParticleMap::particles
const ParticleMap & particles() const
Access the particle map.
Definition: Geant4Particle.h:361
dd4hep::sim::Geant4HitCollection
Generic hit container class using Geant4HitWrapper objects.
Definition: Geant4HitCollection.h:201
dd4hep::sim::Geant4HitCollection::hit
Geant4HitWrapper & hit(size_t which)
Access the hit wrapper.
Definition: Geant4HitCollection.h:325
dd4hep::_toString
std::string _toString(bool value)
String conversions: boolean value to string.
Definition: Handle.cpp:332
G4VHitsCollection
Class of the Geant4 toolkit. See http://www-geant4.kek.jp/Reference.
Definition: Geant4Classes.h:47
dd4hep::sim::Geant4OutputAction
Base class to output Geant4 event data to persistent media.
Definition: Geant4OutputAction.h:40
dd4hep::InstanceCount::increment
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
Geant4Data.h
dd4hep::sim::Geant4Context::event
Geant4Event & event() const
Access the geant4 event – valid only between BeginEvent() and EndEvent()!
Definition: Geant4Context.cpp:84
dd4hep::sim::Geant4HitCollection::getHitsUnchecked
void getHitsUnchecked(std::vector< void * > &result)
Release all hits from the Geant4 container. Ownership stays with the container.
Definition: Geant4HitCollection.cpp:172
dd4hep::sim::Geant4Output2ROOT::m_handleMCTruth
bool m_handleMCTruth
Property: Flag if Monte-Carlo truth should be followed and checked.
Definition: Geant4Output2ROOT.h:58
dd4hep::sim::Geant4Output2ROOT::~Geant4Output2ROOT
virtual ~Geant4Output2ROOT()
Default destructor.
Definition: Geant4Output2ROOT.cpp:49
dd4hep::sim::Geant4Output2ROOT::commit
virtual void commit(OutputContext< G4Event > &ctxt) override
Commit data at end of filling procedure.
Definition: Geant4Output2ROOT.cpp:156
dd4hep::sim::Geant4OutputAction::OutputContext
Helper class for thread savety.
Definition: Geant4OutputAction.h:43
dd4hep::sim::Geant4ParticleMap
Data structure to map particles produced during the generation and the simulation.
Definition: Geant4Particle.h:337
dd4hep::sim::Geant4Action::info
void info(const char *fmt,...) const
Support of info messages.
Definition: Geant4Action.cpp:215
dd4hep::sim::Geant4Event::extension
T * extension(bool alert=true)
Access to type safe extension object. Exception is thrown if the object is invalid.
Definition: Geant4Context.h:151
dd4hep::sim::Geant4Output2ROOT::Geant4Output2ROOT
Geant4Output2ROOT(Geant4Context *context, const std::string &nam)
Standard constructor.
Definition: Geant4Output2ROOT.cpp:38
dd4hep::sim::Geant4Action::except
void except(const char *fmt,...) const
Support of exceptions: Print fatal message and throw runtime_error.
Definition: Geant4Action.cpp:256
dd4hep::sim::Geant4HitData::Contributions
std::vector< MonteCarloContrib > Contributions
Definition: Geant4Data.h:232
dd4hep::sim::Geant4Output2ROOT::m_tree
TTree * m_tree
Reference to the event data tree.
Definition: Geant4Output2ROOT.h:48
dd4hep::sim::Geant4Output2ROOT::fill
int fill(const std::string &nam, const ComponentCast &type, void *ptr)
Fill single EVENT branch entry (Geant4 collection data)
Definition: Geant4Output2ROOT.cpp:118
dd4hep::sim::Geant4OutputAction::commit
virtual void commit(OutputContext< G4Event > &ctxt)
Commit data at end of filling procedure.
Definition: Geant4OutputAction.cpp:104
dd4hep::sim::Geant4Action::error
void error(const char *fmt,...) const
Support of error messages.
Definition: Geant4Action.cpp:231
dd4hep::sim::Geant4HitCollection::vector_type
const ComponentCast & vector_type() const
Type information of the vector type for extracting data.
Definition: Geant4HitCollection.cpp:92
dd4hep::sim::Geant4Tracker::Hit
DDG4 tracker hit class used by the generic DDG4 tracker sensitive detector.
Definition: Geant4Data.h:263
dd4hep::sim::Geant4ParticleMap::ParticleMap
std::map< int, Particle * > ParticleMap
Definition: Geant4Particle.h:340
dd4hep::sim::Geant4HitData
Base class for geant4 hit structures used by the default DDG4 sensitive detector implementations.
Definition: Geant4Data.h:110
dd4hep::sim::Geant4OutputAction::m_truth
Geant4ParticleMap * m_truth
Reference to MC truth object.
Definition: Geant4OutputAction.h:60
dd4hep::sim::Geant4Action::declareProperty
Geant4Action & declareProperty(const std::string &nam, T &val)
Declare property.
Definition: Geant4Action.h:366
dd4hep::InstanceCount::decrement
static void decrement(T *)
Decrement count according to type information.
Definition: InstanceCount.h:102
dd4hep::sim::Geant4OutputAction::beginRun
virtual void beginRun(const G4Run *run)
Callback to initialize storing the Geant4 information.
Definition: Geant4OutputAction.cpp:108
dd4hep::sim::Geant4Output2ROOT::m_branches
Branches m_branches
Branches in the event tree.
Definition: Geant4Output2ROOT.h:44
dd4hep::sim::Geant4Tracker::Hit::truth
Contribution truth
Monte Carlo / Geant4 information.
Definition: Geant4Data.h:276
dd4hep::sim::Geant4Calorimeter::Hit
DDG4 calorimeter hit class used by the generic DDG4 calorimeter sensitive detector.
Definition: Geant4Data.h:323
dd4hep::sim::Geant4Output2ROOT::section
TTree * section(const std::string &nam)
Create/access tree by name for non collection user data.
Definition: Geant4Output2ROOT.cpp:71
dd4hep::sim::Geant4ParticleMap::particleID
int particleID(int track, bool throw_if_not_found=true) const
Access the equivalent track id (shortcut to the usage of TrackEquivalents)
Definition: Geant4Particle.cpp:541
dd4hep::sim::Geant4HitData::MonteCarloContrib::trackID
int trackID
Geant 4 Track identifier.
Definition: Geant4Data.h:141
dd4hep::sim::Geant4Output2ROOT::saveCollection
virtual void saveCollection(OutputContext< G4Event > &ctxt, G4VHitsCollection *collection) override
Callback to store each Geant4 hit collection.
Definition: Geant4Output2ROOT.cpp:203
dd4hep::sim::Geant4Output2ROOT::m_sections
Sections m_sections
Known file sections.
Definition: Geant4Output2ROOT.h:42
dd4hep::sim::Geant4Output2ROOT::m_disabledCollections
std::vector< std::string > m_disabledCollections
Property: vector with disabled collections.
Definition: Geant4Output2ROOT.h:54
dd4hep::sim::Geant4OutputAction::m_output
std::string m_output
Property: "Output" output destination.
Definition: Geant4OutputAction.h:56
Primitives.h
dd4hep::sim
Namespace for the Geant4 based simulation part of the AIDA detector description toolkit.
Definition: Geant4Output2EDM4hep.cpp:49
dd4hep::sim::Geant4Output2ROOT::beginRun
virtual void beginRun(const G4Run *run) override
Callback to store the Geant4 run information.
Definition: Geant4Output2ROOT.cpp:83
dd4hep::sim::Geant4Output2ROOT::m_section
std::string m_section
Property: name of the event tree.
Definition: Geant4Output2ROOT.h:52
dd4hep::sim::Geant4Output2ROOT::m_disableParticles
bool m_disableParticles
Property: vector with disabled collections.
Definition: Geant4Output2ROOT.h:56
dd4hep::sim::Geant4HitWrapper::HitManipulator
Generic type manipulation class for generic hit structures created in Geant4 sensitive detectors.
Definition: Geant4HitCollection.h:65
dd4hep::sim::Geant4Calorimeter::Hit::truth
Contributions truth
Hit contributions by individual particles.
Definition: Geant4Data.h:330
dd4hep::sim::Geant4Output2ROOT::m_filesByRun
bool m_filesByRun
Property: Flag if Monte-Carlo truth should be followed and checked.
Definition: Geant4Output2ROOT.h:60
Geant4Particle.h
dd4hep::sim::Geant4HitData::MonteCarloContrib
Utility class describing the monte carlo contribution of a given particle to a hit.
Definition: Geant4Data.h:138
dd4hep::sim::Geant4Output2ROOT::closeOutput
virtual void closeOutput()
Close current output file.
Definition: Geant4Output2ROOT.cpp:55
InstanceCount.h
dd4hep::sim::Geant4Output2ROOT::m_file
TFile * m_file
Reference to the ROOT file to open.
Definition: Geant4Output2ROOT.h:46
Printout.h
Geant4Output2ROOT.h
dd4hep::sim::Geant4Context
Generic context to extend user, run and event information.
Definition: Geant4Context.h:201
dd4hep::sim::Geant4Action::context
Geant4Context * context() const
Access the context.
Definition: Geant4Action.h:270