14 #ifndef DD4HEP_DDG4_GEANT4OUTPUT2LCIO_H
15 #define DD4HEP_DDG4_GEANT4OUTPUT2LCIO_H
24 #include <G4Threading.hh>
25 #include <G4AutoLock.hh>
28 #include <G4Version.hh>
32 #include <IO/LCWriter.h>
33 #include <IMPL/LCEventImpl.h>
34 #include <IMPL/LCCollectionVec.h>
35 #include <EVENT/LCParameters.h>
37 using namespace lcio ;
46 template <
class T=lcio::LCEventImpl>
void EventParameters::extractParameters(T& event){
47 auto& lcparameters =
event.parameters();
48 event.setRunNumber(this->runNumber());
49 event.setEventNumber(this->eventNumber());
50 for(
auto const& ival: this->intParameters()) {
51 lcparameters.setValues(ival.first, ival.second);
53 for(
auto const& ival: this->fltParameters()) {
54 lcparameters.setValues(ival.first, ival.second);
56 for(
auto const& ival: this->strParameters()) {
57 lcparameters.setValues(ival.first, ival.second);
59 #if LCIO_VERSION_GE(2, 17)
60 for(
auto const& ival: this->dblParameters()) {
61 lcparameters.setValues(ival.first, ival.second);
66 template <
class T=lcio::LCRunHeaderImpl>
void RunParameters::extractParameters(T& runHeader){
67 auto& lcparameters = runHeader.parameters();
68 for(
auto const& ival: this->intParameters()) {
69 lcparameters.setValues(ival.first, ival.second);
71 for(
auto const& ival: this->fltParameters()) {
72 lcparameters.setValues(ival.first, ival.second);
74 for(
auto const& ival: this->strParameters()) {
75 lcparameters.setValues(ival.first, ival.second);
77 #if LCIO_VERSION_GE(2, 17)
78 for(
auto const& ival: this->dblParameters()) {
79 lcparameters.setValues(ival.first, ival.second);
112 virtual void beginRun(
const G4Run* run);
114 virtual void endRun(
const G4Run* run);
117 virtual void saveRun(
const G4Run* run);
126 virtual void begin(
const G4Event* event);
129 template <
typename T>
130 void saveEventParameters(lcio::LCEventImpl* event,
const std::map<std::string, std::string >& parameters);
134 template <
typename T>
135 inline void Geant4Output2LCIO::saveEventParameters(lcio::LCEventImpl* event,
const std::map<std::string, std::string >& parameters) {
136 for(std::map<std::string, std::string >::const_iterator iter = parameters.begin(), endIter = parameters.end() ; iter != endIter ; ++iter) {
138 std::istringstream iss(iter->second);
139 if ( (iss >> parameter).fail() ) {
140 printout(FATAL,
"saveEventParameters",
"+++ Event parameter %s: FAILED to convert to type :%s",iter->first.c_str(),
typeid(T).name());
143 event->parameters().setValue(iter->first,parameter);
149 inline void Geant4Output2LCIO::saveEventParameters<std::string>(lcio::LCEventImpl* event,
const std::map<std::string, std::string >& parameters) {
150 for(std::map<std::string, std::string >::const_iterator iter = parameters.begin(), endIter = parameters.end() ; iter != endIter ; ++iter) {
151 event->parameters().setValue(iter->first,iter->second);
157 #endif // DD4HEP_DDG4_GEANT4OUTPUT2LCIO_H
183 #include <G4ParticleDefinition.hh>
184 #include <G4VProcess.hh>
185 #include <G4Event.hh>
189 #include <IMPL/LCEventImpl.h>
190 #include <IMPL/LCRunHeaderImpl.h>
191 #include <IMPL/LCCollectionVec.h>
192 #include <IMPL/ClusterImpl.h>
193 #include <IMPL/SimTrackerHitImpl.h>
194 #include <IMPL/SimCalorimeterHitImpl.h>
195 #include <IMPL/MCParticleImpl.h>
196 #include <UTIL/ILDConf.h>
202 G4Mutex action_mutex=G4MUTEX_INITIALIZER;
208 Geant4Output2LCIO::Geant4Output2LCIO(
Geant4Context* ctxt,
const string& nam)
210 :
Geant4OutputAction(ctxt,nam), m_file(0), m_runNo(0), m_runNumberOffset(0), m_eventNumberOffset(0)
212 declareProperty(
"RunHeader", m_runHeader);
213 declareProperty(
"EventParametersInt", m_eventParametersInt);
214 declareProperty(
"EventParametersFloat", m_eventParametersFloat);
215 declareProperty(
"EventParametersString", m_eventParametersString);
216 declareProperty(
"RunNumberOffset", m_runNumberOffset);
217 declareProperty(
"EventNumberOffset", m_eventNumberOffset);
223 G4AutoLock protection_lock(&action_mutex);
226 detail::deletePtr(
m_file);
234 G4AutoLock protection_lock(&action_mutex);
235 m_file = lcio::LCFactory::getInstance()->createLCWriter();
251 G4AutoLock protection_lock(&action_mutex);
255 except(
"+++ Failed to write output file. [Stream is not open]");
260 G4AutoLock protection_lock(&action_mutex);
262 lcio::LCRunHeaderImpl* rh =
new lcio::LCRunHeaderImpl;
263 for (std::map< std::string, std::string >::iterator it =
m_runHeader.begin(); it !=
m_runHeader.end(); ++it) {
264 rh->parameters().setValue( it->first, it->second );
267 rh->parameters().setValue(
"GEANT4Version", G4Version);
270 rh->setDetectorName(
context()->detectorDescription().header().
name());
275 m_file->writeRunHeader(rh);
279 lcio::LCEventImpl* e =
new lcio::LCEventImpl;
287 typedef detail::ReferenceBitMask<const int>
PropertyMask;
290 size_t nparts = pm.size();
291 lcio::LCCollectionVec* lc_coll =
new lcio::LCCollectionVec(lcio::LCIO::MCPARTICLE);
292 lc_coll->reserve(nparts);
296 vector<const Geant4Particle*> p_part(pm.size(),0);
297 vector<MCParticleImpl*> p_lcio(pm.size(),0);
299 for(ParticleMap::const_iterator i=pm.begin(); i!=pm.end();++i, ++cnt) {
304 const G4ParticleDefinition* def = p.
definition();
305 MCParticleImpl* q =
new lcio::MCParticleImpl();
308 float ps_fa[3] = {float(p->
psx/CLHEP::GeV),float(p->
psy/CLHEP::GeV),float(p->
psz/CLHEP::GeV)};
309 q->setMomentum( ps_fa );
311 #if LCIO_VERSION_GE(2,7)
312 float pe_fa[3] = {float(p->
pex/CLHEP::GeV),float(p->
pey/CLHEP::GeV),float(p->
pez/CLHEP::GeV)};
313 q->setMomentumAtEndpoint( pe_fa );
315 double vs_fa[3] = { p->
vsx/CLHEP::mm, p->
vsy/CLHEP::mm, p->
vsz/CLHEP::mm } ;
316 q->setVertex( vs_fa );
318 double ve_fa[3] = { p->
vex/CLHEP::mm, p->
vey/CLHEP::mm, p->
vez/CLHEP::mm } ;
319 q->setEndpoint( ve_fa );
321 q->setTime(p->
time/CLHEP::ns);
322 q->setMass(p->
mass/CLHEP::GeV);
323 q->setCharge(def ? def->GetPDGCharge() : 0);
326 q->setGeneratorStatus(0);
347 q->setOverlay(
false );
350 if( q->isCreatedInSimulation() )
351 q->setGeneratorStatus( 0 ) ;
356 lc_coll->addElement(q);
363 for(
size_t i=0, n=p_ids.size(); i<n; ++i) {
364 map<int,int>::iterator k;
366 MCParticleImpl* q = p_lcio[i];
368 for( Geant4Particle::Particles::const_iterator j=dau.begin(); j != dau.end(); ++j ) {
370 if ( (k=p_ids.find(idau)) == p_ids.end() ) {
371 printout(FATAL,
"Geant4Conversion",
"+++ Particle %d: FAILED to find daughter with ID:%d",p->
id,idau);
374 int iqdau = (*k).second;
375 MCParticleImpl* qdau = p_lcio[iqdau];
379 for( Geant4Particle::Particles::const_iterator j=par.begin(); j != par.end(); ++j ) {
382 if( (k=p_ids.find(ipar)) == p_ids.end() ) {
383 printout(FATAL,
"Geant4Conversion",
"+++ Particle %d: FAILED to find parent with ID:%d",p->
id,ipar);
386 int iqpar = (*k).second;
387 MCParticleImpl* qpar = p_lcio[iqpar];
400 int runNumber(0), eventNumber(0);
403 double eventWeight{0};
406 runNumber = parameters->runNumber() + runNumberOffset;
407 eventNumber = parameters->eventNumber() + eventNumberOffset;
408 parameters->extractParameters(*e);
409 #if LCIO_VERSION_GE(2, 17)
410 eventWeight = e->getParameters().getDoubleVal(
"EventWeights");
413 runNumber =
m_runNo + runNumberOffset;
414 eventNumber = ctxt.
context->GetEventID() + eventNumberOffset;
416 print(
"+++ Saving LCIO event %d run %d ....", eventNumber, runNumber);
417 e->setRunNumber(runNumber);
418 e->setEventNumber(eventNumber);
419 e->setWeight(eventWeight);
420 e->setDetectorName(
context()->detectorDescription().header().
name());
427 print(
"+++ Saving %d LCIO particles....",
int(part_map->particleMap.size()));
428 if ( part_map->particleMap.size() > 0 ) {
430 evt->addCollection(col,lcio::LCIO::MCPARTICLE);
437 size_t nhits = collection->GetSize();
438 std::string hc_nam = collection->GetName();
439 print(
"+++ Saving LCIO collection %s with %d entries....",hc_nam.c_str(),
int(nhits));
440 typedef pair<const Geant4Context*,G4VHitsCollection*> _Args;
443 cnv(_Args(
context(),collection));