 |
DD4hep
1.37.0
Detector Description Toolkit for High Energy Physics
|
Go to the documentation of this file.
25 #include <G4HCofThisEvent.hh>
26 #include <G4ParticleTable.hh>
64 if (nThreads == 0 || done >= nThreads) {
73 TDirectory::TContext ctxt(
m_file.get());
74 info(
"+++ Closing ROOT output file %s",
m_file->GetName());
77 if (tree !=
m_tree) tree->Write();
96 TDirectory::TContext ctxt(
m_file.get());
97 TTree* t =
new TTree(nam.c_str(), (
"Geant4 " + nam +
" information").c_str());
117 fname +=
_toString(run->GetRunID(),
".run%08d");
118 if ( idx != std::string::npos )
121 if ( !
m_file && !fname.empty() ) {
122 TDirectory::TContext ctxt(TDirectory::CurrentDirectory());
123 if ( !gSystem->AccessPathName(fname.c_str()) ) {
124 gSystem->Unlink(fname.c_str());
126 std::unique_ptr<TFile> file(TFile::Open(fname.c_str(),
"RECREATE",
"dd4hep Simulation data"));
128 file.reset(TFile::Open((fname+
".1").c_str(),
"RECREATE",
"dd4hep Simulation data"));
131 except(
"Failed to create ROOT output file:'%s'", fname.c_str());
133 if (file->IsZombie()) {
134 except(
"Failed to open ROOT output file:'%s'", fname.c_str());
146 TBranch* b =
nullptr;
149 const std::type_info& typ = type.type();
150 if (
auto* cl = TBuffer::GetClass(typ)) {
151 b =
m_tree->Branch(nam.c_str(), cl->GetName(),
static_cast<void*
>(
nullptr));
152 b->SetAutoDelete(
false);
156 throw std::runtime_error(
"No ROOT TClass object available for object type:" + typeName(typ));
162 const Long64_t evt = b->GetEntries();
163 const Long64_t nevt = b->GetTree()->GetEntries();
165 b->SetAddress(
nullptr);
166 for (Long64_t num = nevt - evt; num > 0; --num)
170 const int nbytes = b->Fill();
172 throw std::runtime_error(
"Failed to write ROOT collection:" + nam +
"!");
181 Int_t evtid = ctxt.
context->GetEventID();
182 TTree* id_tree =
section(
"G4EventIDs");
183 TBranch* br = id_tree->GetBranch(
"G4EventID");
185 br = id_tree->Branch(
"G4EventID", &evtid,
"G4EventID/I");
188 br->SetAddress(&evtid);
192 auto* a =
m_tree->GetListOfBranches();
193 const Long64_t evt =
m_tree->GetEntries() + 1;
194 const Int_t nb = a->GetEntriesFast();
196 for (Int_t i = 0; i < nb; ++i) {
197 auto* br_ptr =
static_cast<TBranch*
>(a->UncheckedAt(i));
198 const Long64_t br_evt = br_ptr->GetEntries();
200 br_ptr->SetAddress(
nullptr);
201 for (Long64_t num = evt - br_evt; num > 0; --num)
216 G4ParticleTable* table = G4ParticleTable::GetParticleTable();
218 std::vector<void*> particles;
219 particles.reserve(pm.size());
220 for (
const auto& i : pm ) {
222 G4ParticleDefinition* def = table->FindParticle(p->pdgID);
223 p->charge =
static_cast<char>(3.0 * (def ? def->GetPDGCharge() : -1.0));
224 particles.emplace_back(p);
233 const std::string hc_nam = collection->GetName();
239 const size_t nhits = coll->GetSize();
240 std::vector<void*> hits;
242 coll->getHitsUnchecked(hits);
245 for(
size_t i=0; i<nhits; ++i) {
252 for(
auto& t : cal_hit->truth)
258 error(
"+++ Exception while saving collection %s.",hc_nam.c_str());
261 fill(hc_nam, coll->vector_type(), &hits);
virtual void endRun(const G4Run *run)
Callback to store the Geant4 run information.
void closeOutputLocked()
Close the current output file. Must be called with s_rootMutex held.
void saveEvent(OutputContext< G4Event > &ctxt) override
Callback to store the Geant4 event.
static std::mutex s_rootMutex
Static mutex to protect ROOT I/O operations in multi-threaded mode.
Generic hit container class using Geant4HitWrapper objects.
std::string _toString(bool value)
String conversions: boolean value to string.
Class of the Geant4 toolkit. See http://www-geant4.kek.jp/Reference.
Base class to output Geant4 event data to persistent media.
static void increment(T *)
Increment count according to type information.
Geant4Event & event() const
Access the geant4 event – valid only between BeginEvent() and EndEvent()!
bool m_handleMCTruth
Property: Flag if Monte-Carlo truth should be followed and checked.
void commit(OutputContext< G4Event > &ctxt) override
Commit data at end of filling procedure.
Helper class for thread savety.
Data structure to map particles produced during the generation and the simulation.
void info(const char *fmt,...) const
Support of info messages.
T * extension(bool alert=true)
Access to type safe extension object. Exception is thrown if the object is invalid.
Geant4Output2ROOT(Geant4Context *context, const std::string &nam)
Standard constructor.
void except(const char *fmt,...) const
Support of exceptions: Print fatal message and throw runtime_error.
TTree * m_tree
Reference to the event data tree (owned by m_file)
std::atomic< int > m_endRunCount
Counter of worker endRun calls so that closeOutput fires only after all workers are done.
int fill(const std::string &nam, const ComponentCast &type, void *ptr)
Fill single EVENT branch entry (Geant4 collection data)
virtual void commit(OutputContext< G4Event > &ctxt)
Commit data at end of filling procedure.
void error(const char *fmt,...) const
Support of error messages.
DDG4 tracker hit class used by the generic DDG4 tracker sensitive detector.
std::map< int, Particle * > ParticleMap
Base class for geant4 hit structures used by the default DDG4 sensitive detector implementations.
Geant4ParticleMap * m_truth
Reference to MC truth object.
Geant4Action & declareProperty(const std::string &nam, T &val)
Declare property.
static void decrement(T *)
Decrement count according to type information.
virtual void beginRun(const G4Run *run)
Callback to initialize storing the Geant4 information.
std::unique_ptr< TFile > m_file
Reference to the ROOT file to open.
Branches m_branches
Branches in the event tree.
const std::string & name() const
Access name of the action.
DDG4 calorimeter hit class used by the generic DDG4 calorimeter sensitive detector.
TTree * section(const std::string &nam)
Create/access tree by name for non collection user data.
int particleID(int track, bool throw_if_not_found=true) const
Access the equivalent track id (shortcut to the usage of TrackEquivalents)
int trackID
Geant 4 Track identifier.
const ComponentCast & vec_type
void saveCollection(OutputContext< G4Event > &ctxt, G4VHitsCollection *collection) override
Callback to store each Geant4 hit collection.
Sections m_sections
Known file sections.
std::vector< std::string > m_disabledCollections
Property: vector with disabled collections.
std::string m_output
Property: "Output" output destination.
Namespace for the Geant4 based simulation part of the AIDA detector description toolkit.
void beginRun(const G4Run *run) override
Callback to store the Geant4 run information.
Geant4Kernel & kernel() const
Access to the kernel object.
std::string m_section
Property: name of the event tree.
bool m_disableParticles
Property: vector with disabled collections.
Generic type manipulation class for generic hit structures created in Geant4 sensitive detectors.
bool m_filesByRun
Property: Flag to create a new output file for each run.
Utility class describing the monte carlo contribution of a given particle to a hit.
virtual void closeOutput()
Close current output file.
~Geant4Output2ROOT() override
Default destructor.
Geant4Kernel & master() const
Thread's master context.
void endRun(const G4Run *run) override
Callback at end of run: write and close the output file while DDG4 is still alive.
Generic context to extend user, run and event information.
Geant4Context * context() const
Access the context.