 |
DD4hep
1.37.0
Detector Description Toolkit for High Energy Physics
|
Go to the documentation of this file.
23 #include <G4HCofThisEvent.hh>
24 #include <G4ParticleTable.hh>
55 TDirectory::TContext ctxt(
m_file.get());
56 info(
"+++ Closing ROOT output file %s",
m_file->GetName());
69 TDirectory::TContext ctxt(
m_file.get());
70 TTree* t =
new TTree(nam.c_str(), (
"Geant4 " + nam +
" information").c_str());
85 fname +=
_toString(run->GetRunID(),
".run%08d");
86 if ( idx != std::string::npos )
89 if ( !
m_file && !fname.empty() ) {
90 TDirectory::TContext ctxt(TDirectory::CurrentDirectory());
91 if ( !gSystem->AccessPathName(fname.c_str()) ) {
92 gSystem->Unlink(fname.c_str());
94 std::unique_ptr<TFile> file(TFile::Open(fname.c_str(),
"RECREATE",
"dd4hep Simulation data"));
96 file.reset(TFile::Open((fname+
".1").c_str(),
"RECREATE",
"dd4hep Simulation data"));
99 except(
"Failed to create ROOT output file:'%s'", fname.c_str());
101 if (file->IsZombie()) {
102 except(
"Failed to open ROOT output file:'%s'", fname.c_str());
113 TBranch* b =
nullptr;
116 const std::type_info& typ = type.type();
117 if (
auto* cl = TBuffer::GetClass(typ)) {
118 b =
m_tree->Branch(nam.c_str(), cl->GetName(),
static_cast<void*
>(
nullptr));
119 b->SetAutoDelete(
false);
123 throw std::runtime_error(
"No ROOT TClass object available for object type:" + typeName(typ));
129 const Long64_t evt = b->GetEntries();
130 const Long64_t nevt = b->GetTree()->GetEntries();
132 b->SetAddress(
nullptr);
133 for (Long64_t num = nevt - evt; num > 0; --num)
137 const int nbytes = b->Fill();
139 throw std::runtime_error(
"Failed to write ROOT collection:" + nam +
"!");
147 auto* a =
m_tree->GetListOfBranches();
148 const Long64_t evt =
m_tree->GetEntries() + 1;
149 const Int_t nb = a->GetEntriesFast();
151 for (Int_t i = 0; i < nb; ++i) {
152 auto* br_ptr =
static_cast<TBranch*
>(a->UncheckedAt(i));
153 const Long64_t br_evt = br_ptr->GetEntries();
155 br_ptr->SetAddress(
nullptr);
156 for (Long64_t num = evt - br_evt; num > 0; --num)
171 G4ParticleTable* table = G4ParticleTable::GetParticleTable();
173 std::vector<void*> particles;
174 particles.reserve(pm.size());
175 for (
const auto& i : pm ) {
177 G4ParticleDefinition* def = table->FindParticle(p->pdgID);
178 p->charge =
static_cast<char>(3.0 * (def ? def->GetPDGCharge() : -1.0));
179 particles.emplace_back(p);
188 const std::string hc_nam = collection->GetName();
194 const size_t nhits = coll->GetSize();
195 std::vector<void*> hits;
197 coll->getHitsUnchecked(hits);
200 for(
size_t i=0; i<nhits; ++i) {
207 for(
auto& t : cal_hit->truth)
213 error(
"+++ Exception while saving collection %s.",hc_nam.c_str());
216 fill(hc_nam, coll->vector_type(), &hits);
void saveEvent(OutputContext< G4Event > &ctxt) override
Callback to store the Geant4 event.
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)
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.
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.
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.
Generic context to extend user, run and event information.
Geant4Context * context() const
Access the context.