diff --git a/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcFloatWriterTool.cxx b/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcFloatWriterTool.cxx index b60f1bc1cf329e249951758bce32fcbc8e7d6752..30e99fce5df6a3514d12ea90f6d416be18b76164 100755 --- a/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcFloatWriterTool.cxx +++ b/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcFloatWriterTool.cxx @@ -26,15 +26,7 @@ static const char * s_protocolSep = ":"; -struct ToLower -{ - char operator() (char c) const { return std::tolower(c); } -}; - /////////////////////////////////////////////////////////////////// -/// Public methods: -/////////////////////////////////////////////////////////////////// - /// Constructors //////////////// HepMcFloatWriterTool::HepMcFloatWriterTool( const std::string& type, @@ -51,8 +43,7 @@ HepMcFloatWriterTool::HepMcFloatWriterTool( const std::string& type, m_ioBackendURL = "ascii:hepmc.genevent.txt", "Name of the back-end we'll use to write out the HepMC::GenEvent." "\nEx: ascii:hepmc.genevent.txt" ); - m_ioBackendURL.declareUpdateHandler( &HepMcFloatWriterTool::setupBackend, - this ); + m_ioBackendURL.declareUpdateHandler( &HepMcFloatWriterTool::setupBackend,this ); declareProperty( "McEvents", m_mcEventsName = "GEN_EVENT", @@ -107,16 +98,13 @@ StatusCode HepMcFloatWriterTool::execute() { // retrieve the McEventCollection const McEventCollection * mcEvts = 0; - if ( evtStore()->retrieve( mcEvts, m_mcEventsName ).isFailure() || - 0 == mcEvts ) { - ATH_MSG_ERROR("Could not retrieve a McEventCollection at [" - << m_mcEventsName << "] !!"); + if ( evtStore()->retrieve( mcEvts, m_mcEventsName ).isFailure() || 0 == mcEvts ) { + ATH_MSG_ERROR("Could not retrieve a McEventCollection at [" << m_mcEventsName << "] !!"); return StatusCode::FAILURE; } if ( mcEvts->empty() ) { - ATH_MSG_WARNING("McEventCollection at [" << m_mcEventsName - << "] is EMPTY !!"); + ATH_MSG_WARNING("McEventCollection at [" << m_mcEventsName << "] is EMPTY !!"); return StatusCode::FAILURE; } @@ -129,18 +117,12 @@ StatusCode HepMcFloatWriterTool::execute() return write(evt); } -/////////////////////////////////////////////////////////////////// -/// Const methods: -/////////////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////// /// Non-const methods: /////////////////////////////////////////////////////////////////// StatusCode HepMcFloatWriterTool::write( const HepMC::GenEvent* evt ) { - //m_ioBackend->write_comment( m_mcEventsName.value() ); - //m_ioBackend->write_event(evt); std::ostringstream out; // precision 8 (# digits following decimal point) is the minimum that @@ -193,20 +175,14 @@ StatusCode HepMcFloatWriterTool::write( const HepMC::GenEvent* evt ) const float py = static_cast<float>(mom.py()); const float pz = static_cast<float>(mom.pz()); const float m = static_cast<float>(mom.m()); - const float e = - static_cast<float>(std::sqrt( std::pow( px, 2 ) + - std::pow( py, 2 ) + - std::pow( pz, 2 ) + - std::pow( m, 2 ) ) ); - buf << px << " " << py << " " << pz << " " << e - << " " << m - << "\n"; + const float e = static_cast<float>(std::sqrt( std::pow( px, 2 ) + std::pow( py, 2 ) + std::pow( pz, 2 ) + std::pow( m, 2 ) ) ); + buf << px << " " << py << " " << pz << " " << e << " " << m << "\n"; out << buf.str(); out << "# "<< p->status() << " " << p->polarization().theta() << " " << p->polarization().phi() - << " " << ( p->end_vertex() ? p->end_vertex()->barcode() : 0 ) + << " " << ( p->end_vertex() ? HepMC::barcode(p->end_vertex()) : 0 ) << " " << p->flow() << "\n"; } @@ -244,14 +220,6 @@ StatusCode HepMcFloatWriterTool::write( const HepMC::GenEvent* evt ) return StatusCode::SUCCESS; } -/////////////////////////////////////////////////////////////////// -/// Protected methods: -/////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////// -/// Const methods: -/////////////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////// /// Non-const methods: /////////////////////////////////////////////////////////////////// @@ -282,23 +250,15 @@ void HepMcFloatWriterTool::setupBackend( Gaudi::Details::PropertyBase& /*prop*/ } // get the protocol name in lower cases - std::transform( protocol.begin(), protocol.end(), - protocol.begin(), - ToLower() ); - + std::transform( protocol.begin(), protocol.end(), protocol.begin(), [](unsigned char c){ return std::tolower(c); } ); if ( "ascii" == protocol ) { - m_ioBackend = new std::ofstream( fileName.c_str(), - std::ios::out | std::ios::trunc ); + m_ioBackend = new std::ofstream( fileName.c_str(), std::ios::out | std::ios::trunc ); } else { - ATH_MSG_WARNING("UNKNOWN protocol [" << protocol << "] !!" << endmsg - << "Will use [ascii] instead..."); + ATH_MSG_WARNING("UNKNOWN protocol [" << protocol << "] !!" << endmsg << "Will use [ascii] instead..."); protocol = "ascii"; - m_ioBackend = new std::ofstream( fileName.c_str(), - std::ios::out | std::ios::trunc ); - } - - ATH_MSG_DEBUG("Using protocol [" << protocol << "] and write to [" - << fileName << "]"); + m_ioBackend = new std::ofstream( fileName.c_str(), std::ios::out | std::ios::trunc ); + } + ATH_MSG_DEBUG("Using protocol [" << protocol << "] and write to ["<< fileName << "]"); return; } diff --git a/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcFloatWriterTool.h b/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcFloatWriterTool.h index 81bff0a2447c71b6e647287d70f2678f01a55e60..22736bdf4aae3f7e8fbeb6b99354d554ae6cd727 100755 --- a/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcFloatWriterTool.h +++ b/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcFloatWriterTool.h @@ -1,7 +1,5 @@ -///////////////////////// -*- C++ -*- ///////////////////////////// - /* - Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration + Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration */ // HepMcFloatWriterTool.h @@ -24,10 +22,8 @@ // Forward declaration #include "AtlasHepMC/GenEvent_fwd.h" -//namespace McAod { class IO_BaseClass; } -class HepMcFloatWriterTool : virtual public IIOHepMcTool, - public AthAlgTool +class HepMcFloatWriterTool : virtual public IIOHepMcTool, public AthAlgTool { /////////////////////////////////////////////////////////////////// @@ -38,9 +34,7 @@ class HepMcFloatWriterTool : virtual public IIOHepMcTool, // Copy constructor: /// Constructor with parameters: - HepMcFloatWriterTool( const std::string& type, - const std::string& name, - const IInterface* parent ); + HepMcFloatWriterTool( const std::string& type, const std::string& name, const IInterface* parent ); /// Destructor: virtual ~HepMcFloatWriterTool(); @@ -50,10 +44,6 @@ class HepMcFloatWriterTool : virtual public IIOHepMcTool, StatusCode execute(); StatusCode finalize(); - /////////////////////////////////////////////////////////////////// - // Const methods: - /////////////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////// // Non-const methods: /////////////////////////////////////////////////////////////////// @@ -99,11 +89,4 @@ class HepMcFloatWriterTool : virtual public IIOHepMcTool, std::ostream* m_ioBackend; }; -/// I/O operators -////////////////////// - -/////////////////////////////////////////////////////////////////// -/// Inline methods: -/////////////////////////////////////////////////////////////////// - #endif //> MCPARTICLETOOLS_HEPMCFLOATWRITERTOOL_H diff --git a/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcReaderTool.cxx b/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcReaderTool.cxx index 691f771c7b8580363b4b888848a33dea4dbf55d2..5d9f75f433afdd9c08ca684de67126bca4e32bea 100755 --- a/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcReaderTool.cxx +++ b/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcReaderTool.cxx @@ -13,9 +13,8 @@ #include <algorithm> #include <cctype> -// FrameWork includes -// CLHEP/HepMC includes +//HepMC includes #include "GeneratorObjects/McEventCollection.h" #include "HepPDT/ParticleDataTable.hh" #include "AtlasHepMC/IO_GenEvent.h" @@ -25,10 +24,6 @@ static const char * s_protocolSep = ":"; -struct ToLower -{ - char operator() (char c) const { return std::tolower(c); } -}; /////////////////////////////////////////////////////////////////// /// Public methods: @@ -40,7 +35,7 @@ HepMcReaderTool::HepMcReaderTool( const std::string& type, const std::string& name, const IInterface* parent ) : AthAlgTool( type, name, parent ), - m_ioFrontend( 0 ) + m_ioFrontend( nullptr ) { // // Property declaration @@ -100,14 +95,12 @@ StatusCode HepMcReaderTool::execute() // create a new McEventCollection and put it into StoreGate McEventCollection * mcEvts = new McEventCollection; if ( evtStore()->record( mcEvts, m_mcEventsOutputName ).isFailure() ) { - ATH_MSG_ERROR("Could not record a McEventCollection at [" - << m_mcEventsOutputName << "] !!"); + ATH_MSG_ERROR("Could not record a McEventCollection at ["<< m_mcEventsOutputName << "] !!"); return StatusCode::FAILURE; } if ( evtStore()->setConst( mcEvts ).isFailure() ) { - ATH_MSG_WARNING("Could not setConst McEventCollection at [" - << m_mcEventsOutputName << "] !!"); + ATH_MSG_WARNING("Could not setConst McEventCollection at ["<< m_mcEventsOutputName << "] !!"); } HepMC::GenEvent * evt = new HepMC::GenEvent; @@ -116,28 +109,21 @@ StatusCode HepMcReaderTool::execute() return read(evt); } -/////////////////////////////////////////////////////////////////// -/// Const methods: -/////////////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////// /// Non-const methods: /////////////////////////////////////////////////////////////////// StatusCode HepMcReaderTool::read( HepMC::GenEvent* evt ) { +#ifdef HEPMC3 + m_ioFrontend->read_event(*evt); +#else m_ioFrontend->fill_next_event(evt); +#endif return StatusCode::SUCCESS; } -/////////////////////////////////////////////////////////////////// -/// Protected methods: -/////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////// -/// Const methods: -/////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////// /// Non-const methods: @@ -169,23 +155,28 @@ void HepMcReaderTool::setupFrontend( Gaudi::Details::PropertyBase& /*prop*/ ) } // get the protocol name in lower cases - std::transform( protocol.begin(), protocol.end(), - protocol.begin(), - ToLower() ); + std::transform( protocol.begin(), protocol.end(), protocol.begin(), [](unsigned char c){ return std::tolower(c); } ); + +#ifdef HEPMC3 + if ( "ascii" == protocol ) { + m_ioFrontend = new HepMC3::ReaderAsciiHepMC2( fileName.c_str()); + } else { + msg(MSG::WARNING) << "UNKNOWN protocol [" << protocol << "] !!" << endmsg<< "Will use [ascii] instead..."<< endmsg; + protocol = "ascii"; + m_ioFrontend = new HepMC3::ReaderAsciiHepMC2( fileName.c_str()); + } +#else if ( "ascii" == protocol ) { m_ioFrontend = new HepMC::IO_GenEvent( fileName.c_str(), std::ios::in ); } else { - msg(MSG::WARNING) - << "UNKNOWN protocol [" << protocol << "] !!" << endmsg - << "Will use [ascii] instead..." - << endmsg; + msg(MSG::WARNING) << "UNKNOWN protocol [" << protocol << "] !!" << endmsg << "Will use [ascii] instead..."<< endmsg; protocol = "ascii"; m_ioFrontend = new HepMC::IO_GenEvent( fileName.c_str(), std::ios::in ); } +#endif - ATH_MSG_DEBUG("Using protocol [" << protocol << "] and write to [" - << fileName << "]"); + ATH_MSG_DEBUG("Using protocol [" << protocol << "] and write to ["<< fileName << "]"); return; } diff --git a/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcReaderTool.h b/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcReaderTool.h index dc1a82e20fc603eaaf51ea198b1a5da4d59bbc09..2e129d4ac5cf26e51766005fe57415237ffe1b51 100755 --- a/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcReaderTool.h +++ b/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcReaderTool.h @@ -1,5 +1,3 @@ -///////////////////////// -*- C++ -*- ///////////////////////////// - /* Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration */ @@ -23,10 +21,13 @@ // Forward declaration #include "AtlasHepMC/GenEvent_fwd.h" -#include "AtlasHepMC/IO_BaseClass_fwd.h" +#include "AtlasHepMC/IO_BaseClass.h" +#ifdef HEPMC3 +#include "HepMC3/Reader.h" +#include "HepMC3/ReaderAsciiHepMC2.h" +#endif -class HepMcReaderTool : virtual public IIOHepMcTool, - public AthAlgTool +class HepMcReaderTool : virtual public IIOHepMcTool, public AthAlgTool { /////////////////////////////////////////////////////////////////// @@ -37,9 +38,7 @@ class HepMcReaderTool : virtual public IIOHepMcTool, // Copy constructor: /// Constructor with parameters: - HepMcReaderTool( const std::string& type, - const std::string& name, - const IInterface* parent ); + HepMcReaderTool( const std::string& type, const std::string& name, const IInterface* parent ); /// Destructor: virtual ~HepMcReaderTool(); @@ -49,10 +48,6 @@ class HepMcReaderTool : virtual public IIOHepMcTool, StatusCode execute(); StatusCode finalize(); - /////////////////////////////////////////////////////////////////// - // Const methods: - /////////////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////// // Non-const methods: /////////////////////////////////////////////////////////////////// @@ -93,15 +88,11 @@ class HepMcReaderTool : virtual public IIOHepMcTool, /** Abstract base class for the back-end */ +#ifdef HEPMC3 + HepMC3::Reader* m_ioFrontend; +#else HepMC::IO_BaseClass* m_ioFrontend; +#endif }; - -/// I/O operators -////////////////////// - -/////////////////////////////////////////////////////////////////// -/// Inline methods: -/////////////////////////////////////////////////////////////////// - #endif //> MCPARTICLETOOLS_HEPMCREADERTOOL_H diff --git a/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcTupleWriterTool.cxx b/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcTupleWriterTool.cxx index 3320a816c0939a114c98bdbc5909499ad2d33bdb..e4c6a5a059d0f64c085538b2d263417e5680868e 100755 --- a/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcTupleWriterTool.cxx +++ b/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcTupleWriterTool.cxx @@ -8,10 +8,6 @@ // Author: S.Binet<binet@cern.ch> /////////////////////////////////////////////////////////////////// - -// STL includes -//#include <sstream> - // FrameWork includes #include "GaudiKernel/ITHistSvc.h" #include "Gaudi/Property.h" @@ -27,10 +23,6 @@ // McParticleTools includes #include "HepMcTupleWriterTool.h" -/////////////////////////////////////////////////////////////////// -/// Public methods: -/////////////////////////////////////////////////////////////////// - /// Constructors //////////////// HepMcTupleWriterTool::HepMcTupleWriterTool( const std::string& type, @@ -48,8 +40,7 @@ HepMcTupleWriterTool::HepMcTupleWriterTool( const std::string& type, m_outputFileName = "hepmc.root", "Name of the output file which will contain the HepMC tuple" "\nEx: hepmc.root" ); - m_outputFileName.declareUpdateHandler( &HepMcTupleWriterTool::setupBackend, - this ); + m_outputFileName.declareUpdateHandler( &HepMcTupleWriterTool::setupBackend, this ); declareProperty( "OutputStream", m_outputStreamName = "hepmc", @@ -107,10 +98,8 @@ StatusCode HepMcTupleWriterTool::execute() { // retrieve the McEventCollection const McEventCollection * mcEvts = 0; - if ( evtStore()->retrieve( mcEvts, m_mcEventsName ).isFailure() || - 0 == mcEvts ) { - ATH_MSG_ERROR("Could not retrieve a McEventCollection at [" - << m_mcEventsName << "] !!"); + if ( evtStore()->retrieve( mcEvts, m_mcEventsName ).isFailure() || 0 == mcEvts ) { + ATH_MSG_ERROR("Could not retrieve a McEventCollection at [" << m_mcEventsName << "] !!"); return StatusCode::FAILURE; } @@ -128,50 +117,39 @@ StatusCode HepMcTupleWriterTool::execute() return write(evt); } -/////////////////////////////////////////////////////////////////// -/// Const methods: -/////////////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////// /// Non-const methods: /////////////////////////////////////////////////////////////////// StatusCode HepMcTupleWriterTool::write( const HepMC::GenEvent* evt ) { - m_particles.m_nParticles = std::min<std::size_t>( s_nMax, - evt->particles_size() ); +#ifdef HEPMC3 + m_particles.m_nParticles = std::min<std::size_t>( s_nMax, evt->particles().size() ); +#else + m_particles.m_nParticles = std::min<std::size_t>( s_nMax, evt->particles_size() ); +#endif std::size_t i = 0; - for ( HepMC::GenEvent::particle_const_iterator - p = evt->particles_begin(), - pEnd = evt->particles_end(); - p != pEnd && i != static_cast<std::size_t>(m_particles.m_nParticles); - ++p,++i ) { - const HepMC::FourVector mom = (*p)->momentum(); + for (auto p: *evt) + { + if (i == static_cast<std::size_t>(m_particles.m_nParticles)) break; + i++; + const HepMC::FourVector mom = p->momentum(); m_particles.m_px [i] = mom.px(); m_particles.m_py [i] = mom.py(); m_particles.m_pz [i] = mom.pz(); m_particles.m_m [i] = mom.m(); m_particles.m_ene[i] = mom.e(); - m_particles.m_pdgId[i] = (*p)->pdg_id(); - m_particles.m_status[i] = (*p)->status(); - m_particles.m_barcode[i] = (*p)->barcode(); + m_particles.m_pdgId[i] = p->pdg_id(); + m_particles.m_status[i] = p->status(); + m_particles.m_barcode[i] = HepMC::barcode(p); } - // commit event m_tuple->Fill(); return StatusCode::SUCCESS; } -/////////////////////////////////////////////////////////////////// -/// Protected methods: -/////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////// -/// Const methods: -/////////////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////// /// Non-const methods: /////////////////////////////////////////////////////////////////// @@ -182,9 +160,7 @@ void HepMcTupleWriterTool::setupBackend( Gaudi::Details::PropertyBase& /*m_outpu IProperty * tSvc = 0; if ( !service( m_tupleSvc.name(), tSvc, createIf ).isSuccess() ) { ATH_MSG_ERROR("Could not retrieve THistSvc handle !!"); - throw GaudiException( "Could not retrieve THistSvc", - name(), - StatusCode::FAILURE ); + throw GaudiException( "Could not retrieve THistSvc", name(), StatusCode::FAILURE ); } const std::string streamName = m_outputStreamName.value(); @@ -196,16 +172,12 @@ void HepMcTupleWriterTool::setupBackend( Gaudi::Details::PropertyBase& /*m_outpu updatedProp.push_back ( streamName+" DATAFILE='"+m_outputFileName.value()+"' " "TYP='ROOT' " - "OPT='RECREATE'" - ); + "OPT='RECREATE'" ); outputFileName.set( updatedProp ); outputFileName.setName( propName ); if ( !tSvc->setProperty( outputFileName ).isSuccess() ) { - ATH_MSG_ERROR("Could not configure the THistSvc's output filename [" - << m_outputFileName.value() << "] !!"); - throw GaudiException( "Could not configure THistSvc output file !!", - name(), - StatusCode::FAILURE ); + ATH_MSG_ERROR("Could not configure the THistSvc's output filename ["<< m_outputFileName.value() << "] !!"); + throw GaudiException( "Could not configure THistSvc output file !!", name(), StatusCode::FAILURE ); } return; @@ -218,9 +190,7 @@ void HepMcTupleWriterTool::bookTuple() if ( !m_tupleSvc->regTree( "/" + streamName + "/hepmc", t ).isSuccess() ) { ATH_MSG_ERROR("Could not register HepMC validation tuple !!"); delete t; t = 0; - throw GaudiException( "Could not register HepMC validation tuple !!", - name(), - StatusCode::FAILURE ); + throw GaudiException( "Could not register HepMC validation tuple !!", name(), StatusCode::FAILURE ); } // booking branches diff --git a/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcTupleWriterTool.h b/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcTupleWriterTool.h index cb27cf1700ea88718091212064b811eef8adfc69..909eaa774351ddd8afec684deb0e7359f9bddc8b 100755 --- a/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcTupleWriterTool.h +++ b/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcTupleWriterTool.h @@ -1,5 +1,3 @@ -///////////////////////// -*- C++ -*- ///////////////////////////// - /* Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration */ @@ -29,8 +27,7 @@ class ITHistSvc; class TTree; #include "AtlasHepMC/GenEvent_fwd.h" -class HepMcTupleWriterTool : virtual public IIOHepMcTool, - public AthAlgTool +class HepMcTupleWriterTool : virtual public IIOHepMcTool, public AthAlgTool { /////////////////////////////////////////////////////////////////// @@ -41,9 +38,7 @@ class HepMcTupleWriterTool : virtual public IIOHepMcTool, // Copy constructor: /// Constructor with parameters: - HepMcTupleWriterTool( const std::string& type, - const std::string& name, - const IInterface* parent ); + HepMcTupleWriterTool( const std::string& type, const std::string& name, const IInterface* parent ); /// Destructor: virtual ~HepMcTupleWriterTool(); @@ -53,10 +48,6 @@ class HepMcTupleWriterTool : virtual public IIOHepMcTool, StatusCode execute(); StatusCode finalize(); - /////////////////////////////////////////////////////////////////// - // Const methods: - /////////////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////// // Non-const methods: /////////////////////////////////////////////////////////////////// @@ -126,11 +117,4 @@ class HepMcTupleWriterTool : virtual public IIOHepMcTool, TTree* m_tuple; }; -/// I/O operators -////////////////////// - -/////////////////////////////////////////////////////////////////// -/// Inline methods: -/////////////////////////////////////////////////////////////////// - #endif //> MCPARTICLETOOLS_HEPMCTUPLEWRITERTOOL_H diff --git a/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcWriterTool.cxx b/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcWriterTool.cxx index 685b361fac618637ea23b958c74a3e0769c1d078..b2c01a57e2e85d0da5af029692280d60d25529b7 100755 --- a/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcWriterTool.cxx +++ b/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcWriterTool.cxx @@ -8,14 +8,11 @@ // Author: S.Binet<binet@cern.ch> /////////////////////////////////////////////////////////////////// - // STL includes #include <algorithm> #include <cctype> -// FrameWork includes - -// CLHEP/HepMC includes +// HepMC includes #include "GeneratorObjects/McEventCollection.h" #include "HepPDT/ParticleDataTable.hh" #include "AtlasHepMC/IO_GenEvent.h" @@ -25,20 +22,10 @@ static const char * s_protocolSep = ":"; -struct ToLower -{ - char operator() (char c) const { return std::tolower(c); } -}; - -/////////////////////////////////////////////////////////////////// -/// Public methods: /////////////////////////////////////////////////////////////////// - /// Constructors //////////////// -HepMcWriterTool::HepMcWriterTool( const std::string& type, - const std::string& name, - const IInterface* parent ) : +HepMcWriterTool::HepMcWriterTool( const std::string& type, const std::string& name, const IInterface* parent ) : AthAlgTool( type, name, parent ), m_ioBackend( 0 ) { @@ -102,10 +89,8 @@ StatusCode HepMcWriterTool::execute() { // retrieve the McEventCollection const McEventCollection * mcEvts = 0; - if ( evtStore()->retrieve( mcEvts, m_mcEventsName ).isFailure() || - 0 == mcEvts ) { - ATH_MSG_ERROR("Could not retrieve a McEventCollection at [" - << m_mcEventsName << "] !!"); + if ( evtStore()->retrieve( mcEvts, m_mcEventsName ).isFailure() || 0 == mcEvts ) { + ATH_MSG_ERROR("Could not retrieve a McEventCollection at [" << m_mcEventsName << "] !!"); return StatusCode::FAILURE; } @@ -123,34 +108,20 @@ StatusCode HepMcWriterTool::execute() return write(evt); } -/////////////////////////////////////////////////////////////////// -/// Const methods: -/////////////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////// /// Non-const methods: /////////////////////////////////////////////////////////////////// StatusCode HepMcWriterTool::write( const HepMC::GenEvent* evt ) { - //m_ioBackend->write_comment( m_mcEventsName.value() ); +#ifdef HEPMC3 + m_ioBackend->write_event(*evt); +#else m_ioBackend->write_event(evt); - +#endif return StatusCode::SUCCESS; } -/////////////////////////////////////////////////////////////////// -/// Protected methods: -/////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////// -/// Const methods: -/////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////// -/// Non-const methods: -/////////////////////////////////////////////////////////////////// - void HepMcWriterTool::setupBackend( Gaudi::Details::PropertyBase& /*prop*/ ) { // defaults @@ -177,23 +148,26 @@ void HepMcWriterTool::setupBackend( Gaudi::Details::PropertyBase& /*prop*/ ) } // get the protocol name in lower cases - std::transform( protocol.begin(), protocol.end(), - protocol.begin(), - ToLower() ); - + std::transform( protocol.begin(), protocol.end(), protocol.begin(), [](unsigned char c){ return std::tolower(c); } ); +#ifdef HEPMC3 if ( "ascii" == protocol ) { - m_ioBackend = new HepMC::IO_GenEvent( fileName.c_str(), - std::ios::out | std::ios::trunc ); + m_ioBackend = new HepMC3::WriterAsciiHepMC2( fileName.c_str()); } else { - ATH_MSG_WARNING("UNKNOWN protocol [" << protocol << "] !!" << endmsg - << "Will use [ascii] instead..."); + ATH_MSG_WARNING("UNKNOWN protocol [" << protocol << "] !!" << endmsg << "Will use [ascii] instead..."); protocol = "ascii"; - m_ioBackend = new HepMC::IO_GenEvent( fileName.c_str(), - std::ios::out | std::ios::trunc ); + m_ioBackend = new HepMC3::WriterAsciiHepMC2( fileName.c_str()); } +#else + if ( "ascii" == protocol ) { + m_ioBackend = new HepMC::IO_GenEvent( fileName.c_str(), std::ios::out | std::ios::trunc ); - ATH_MSG_DEBUG("Using protocol [" << protocol << "] and write to [" - << fileName << "]"); + } else { + ATH_MSG_WARNING("UNKNOWN protocol [" << protocol << "] !!" << endmsg << "Will use [ascii] instead..."); + protocol = "ascii"; + m_ioBackend = new HepMC::IO_GenEvent( fileName.c_str(), std::ios::out | std::ios::trunc ); + } +#endif + ATH_MSG_DEBUG("Using protocol [" << protocol << "] and write to ["<< fileName << "]"); return; } diff --git a/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcWriterTool.h b/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcWriterTool.h index 1845fb6c6be3ccd748ad19b6a54e82cefdb41bb8..176419ac49f9c1cc9f1b097d2c64c239ecb337e4 100755 --- a/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcWriterTool.h +++ b/PhysicsAnalysis/TruthParticleID/McParticleTools/src/HepMcWriterTool.h @@ -1,5 +1,3 @@ -///////////////////////// -*- C++ -*- ///////////////////////////// - /* Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration */ @@ -23,10 +21,12 @@ // Forward declaration #include "AtlasHepMC/GenEvent_fwd.h" -#include "AtlasHepMC/IO_BaseClass_fwd.h" - -class HepMcWriterTool : virtual public IIOHepMcTool, - public AthAlgTool +#include "AtlasHepMC/IO_BaseClass.h" +#ifdef HEPMC3 +#include "HepMC3/Writer.h" +#include "HepMC3/WriterAsciiHepMC2.h" +#endif +class HepMcWriterTool : virtual public IIOHepMcTool, public AthAlgTool { /////////////////////////////////////////////////////////////////// @@ -37,9 +37,7 @@ class HepMcWriterTool : virtual public IIOHepMcTool, // Copy constructor: /// Constructor with parameters: - HepMcWriterTool( const std::string& type, - const std::string& name, - const IInterface* parent ); + HepMcWriterTool( const std::string& type, const std::string& name, const IInterface* parent ); /// Destructor: virtual ~HepMcWriterTool(); @@ -49,10 +47,6 @@ class HepMcWriterTool : virtual public IIOHepMcTool, StatusCode execute(); StatusCode finalize(); - /////////////////////////////////////////////////////////////////// - // Const methods: - /////////////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////// // Non-const methods: /////////////////////////////////////////////////////////////////// @@ -95,15 +89,11 @@ class HepMcWriterTool : virtual public IIOHepMcTool, /** Abstract base class for the back-end */ +#ifdef HEPMC3 + HepMC3::Writer* m_ioBackend; +#else HepMC::IO_BaseClass* m_ioBackend; +#endif }; - -/// I/O operators -////////////////////// - -/////////////////////////////////////////////////////////////////// -/// Inline methods: -/////////////////////////////////////////////////////////////////// - #endif //> MCPARTICLETOOLS_HEPMCWRITERTOOL_H