HepMC3 event record library
LHEF_example_cat.cc

Basic example of use of LHEF for reading and writing LHE files

/**
* @example LHEF_example_cat.cc
* @brief Basic example of use of LHEF for reading and writing LHE files
*/
#include <iomanip>
using namespace HepMC3;
int main(int /*argc*/, char ** /*argv*/) {
// Create Reader to read the example LHE file.
LHEF::Reader reader("LHEF_example.lhe");
// Create a HEPRUP attribute and initialize it from the reader.
std::shared_ptr<HEPRUPAttribute> hepr = std::make_shared<HEPRUPAttribute>();
hepr->heprup = reader.heprup;
// There may be some XML tags in the LHE file which are
// non-standard, but we can save them as well.
// Nowwe want to create a GenRunInfo object for the HepMC file, and
// we add the LHEF attribute to that.
std::shared_ptr<GenRunInfo> runinfo = std::make_shared<GenRunInfo>();
runinfo->add_attribute("HEPRUP", hepr);
// This is just a test to make sure we can add other attributes as
// well.
runinfo->add_attribute("NPRUP",
std::make_shared<FloatAttribute>(hepr->heprup.NPRUP));
// We want to be able to convey the different event weights to
// HepMC. In particular we need to add the names of the weights to
// the GenRunInfo object.
std::vector<std::string> weightnames;
weightnames.push_back("0"); // The first weight is always the
// default weight with name "0".
for ( int i = 0, N = hepr->heprup.weightinfo.size(); i < N; ++i )
weightnames.push_back(hepr->heprup.weightNameHepMC(i));
runinfo->set_weight_names(weightnames);
// We also want to convey the information about which generators was
// used to HepMC.
for ( int i = 0, N = hepr->heprup.generators.size(); i < N; ++i ) {
tool.name = hepr->heprup.generators[i].name;
tool.version = hepr->heprup.generators[i].version;
tool.description = hepr->heprup.generators[i].contents;
runinfo->tools().push_back(tool);
}
// Now we want to start reading events from the LHE file and
// translate them to HepMC.
WriterAscii output("LHEF_example.hepmc3", runinfo);
int neve = 0;
while ( reader.readEvent() ) {
++neve;
// To each GenEvent we want to add an attribute corresponding to
// the HEPEUP. Also here there may be additional non-standard
// information outside the LHEF <event> tags, which we may want to
// add.
std::shared_ptr<HEPEUPAttribute> hepe = std::make_shared<HEPEUPAttribute>();
if ( reader.outsideBlock.length() )
hepe->hepeup = reader.hepeup;
GenEvent ev(runinfo, Units::GEV, Units::MM);
ev.set_event_number(neve);
// This is just a text to check that we can add additional
// attributes to each event.
ev.add_attribute("HEPEUP", hepe);
ev.add_attribute("AlphaQCD",
std:: make_shared<DoubleAttribute>(hepe->hepeup.AQCDUP));
ev.add_attribute("AlphaEM",
std::make_shared<DoubleAttribute>(hepe->hepeup.AQEDUP));
ev.add_attribute("NUP",
std::make_shared<IntAttribute>(hepe->hepeup.NUP));
ev.add_attribute("IDPRUP",
std::make_shared<LongAttribute>(hepe->hepeup.IDPRUP));
// Now add the Particles from the LHE event to HepMC
GenParticlePtr p1 = std::make_shared<GenParticle>(hepe->momentum(0),
hepe->hepeup.IDUP[0],
hepe->hepeup.ISTUP[0]);
GenParticlePtr p2 = std::make_shared<GenParticle>(hepe->momentum(1),
hepe->hepeup.IDUP[1],
hepe->hepeup.ISTUP[1]);
GenVertexPtr vx = std::make_shared<GenVertex>();
vx->add_particle_in(p1);
vx->add_particle_in(p2);
for ( int i = 2; i < hepe->hepeup.NUP; ++i )
vx->add_particle_out(std::make_shared<GenParticle>
(hepe->momentum(i),
hepe->hepeup.IDUP[i],
hepe->hepeup.ISTUP[i]));
ev.add_vertex(vx);
// And we also want to add the weights.
std::vector<double> wts;
for ( int i = 0, N = hepe->hepeup.weights.size(); i < N; ++i )
wts.push_back(hepe->hepeup.weights[i].first);
ev.weights() = wts;
// Let's see if we can associate p1 and p2.
ev.add_attribute("OtherIncoming",
std::make_shared<AssociatedParticle>(p2), p1->id());
// And then we are done and can write out the GenEvent.
output.write_event(ev);
}
output.close();
// Now we wnat to make sure we can read in the HepMC file and
// recreate the same info. To check this we try to reconstruct the
// LHC file we read in above.
ReaderAscii input("LHEF_example.hepmc3");
LHEF::Writer writer("LHEF_example_out.lhe");
hepr = std::shared_ptr<HEPRUPAttribute>();
// The loop over all events in the HepMC file.
while ( true ) {
// Read in the next event.
GenEvent ev(Units::GEV, Units::MM);
if ( !input.read_event(ev) || ev.event_number() == 0 ) break;
// Check that the first incoming particle still refers to the second.
std::shared_ptr<AssociatedParticle> assoc =
ev.attribute<AssociatedParticle>("OtherIncoming", 1);
if ( !assoc || !assoc->associated() ||
assoc->associated() != ev.particles()[1] ) return 3;
// Make sure the weight names are the same.
if ( input.run_info()->weight_names() != weightnames ) return 2;
// For the first event we also go in and reconstruct the HEPRUP
// information, and write it out to the new LHE file.
if ( !hepr ) {
hepr = ev.attribute<HEPRUPAttribute>("HEPRUP");
// Here we also keep track of the additional non-standard info
// we found in the original LHE file.
for ( int i = 0, N = hepr->tags.size(); i < N; ++i )
if ( hepr->tags[i]->name != "init" )
hepr->tags[i]->print(writer.headerBlock());
// This is just a test that we can access other attributes
// included in the GenRunInfo.
hepr->heprup.NPRUP =
int(input.run_info()->
attribute<FloatAttribute>("NPRUP")->value());
// Then we write out the HEPRUP object.
writer.heprup = hepr->heprup;
if ( writer.heprup.eventfiles.size() >= 2 ) {
writer.heprup.eventfiles[0].filename = "LHEF_example_1_out.plhe";
writer.heprup.eventfiles[1].filename = "LHEF_example_2_out.plhe";
}
writer.init();
}
// Now we can access the HEPEUP attribute of the current event.
std::shared_ptr<HEPEUPAttribute> hepe =
ev.attribute<HEPEUPAttribute>("HEPEUP");
// Again, there may be addisional non-standard information we want
// to keep.
for ( int i = 0, N = hepe->tags.size(); i < N; ++i )
if ( hepe->tags[i]->name != "event" &&
hepe->tags[i]->name != "eventgroup" )
hepe->tags[i]->print(writer.eventComments());
// This is just a test that we can access other attributes
// included in the GenRunInfo.
hepe->hepeup.AQCDUP =
ev.attribute<DoubleAttribute>("AlphaQCD")->value();
hepe->hepeup.AQEDUP =
ev.attribute<DoubleAttribute>("AlphaEM")->value();
hepe->hepeup.NUP =
ev.attribute<IntAttribute>("NUP")->value();
hepe->hepeup.IDPRUP =
ev.attribute<LongAttribute>("IDPRUP")->value();
// And then we can write out the HEPEUP object.
writer.hepeup = hepe->hepeup;
writer.hepeup.heprup = &writer.heprup;
writer.writeEvent();
}
}
LHEF::Writer::eventComments
std::ostream & eventComments()
Definition: LHEF.h:3145
GenEvent.h
Definition of class GenEvent.
HepMC3::HEPEUPAttribute
Class for storing data for LHEF run information.
Definition: LHEFAttributes.h:68
HepMC3::GenRunInfo::tools
const std::vector< ToolInfo > & tools() const
The vector of tools used to produce this run.
Definition: GenRunInfo.h:63
HepMC3::GenEvent::event_number
int event_number() const
Get event number.
Definition: GenEvent.h:135
HepMC3::GenRunInfo::ToolInfo::version
std::string version
The version of the tool.
Definition: GenRunInfo.h:44
LHEF::Writer::init
void init()
Definition: LHEF.h:3173
LHEF::Writer
Definition: LHEF.h:3090
HepMC3::WriterAscii
GenEvent I/O serialization for structured text files.
Definition: WriterAscii.h:25
HepMC3::GenEvent
Stores event-related information.
Definition: GenEvent.h:41
LHEF::Reader::outsideBlock
std::string outsideBlock
Definition: LHEF.h:3002
HepMC3::GenEvent::add_vertex
void add_vertex(GenVertexPtr v)
Add vertex.
Definition: GenEvent.cc:97
GenVertex.h
Definition of class GenVertex.
LHEF::HEPRUP::generators
std::vector< Generator > generators
Definition: LHEF.h:2026
HepMC3::GenEvent::add_attribute
void add_attribute(const std::string &name, const std::shared_ptr< Attribute > &att, const int &id=0)
Add event attribute to event.
Definition: GenEvent.h:208
HepMC3
HepMC3 main namespace.
Definition: AnalysisExample.h:19
HepMC3::HEPRUPAttribute::heprup
LHEF::HEPRUP heprup
The actual HEPRUP object.
Definition: LHEFAttributes.h:57
LHEFAttributes.h
Definition of class HEPRUPAttribute and class HEPEUAttribute.
HepMC3::IntAttribute
Attribute that holds an Integer implemented as an int.
Definition: Attribute.h:159
GenParticle.h
Definition of class GenParticle.
HepMC3::LongAttribute
Attribute that holds an Integer implemented as an int.
Definition: Attribute.h:200
LHEF::HEPRUP::NPRUP
int NPRUP
Definition: LHEF.h:1967
LHEF::Reader::hepeup
HEPEUP hepeup
Definition: LHEF.h:3022
HepMC3::ReaderAscii
GenEvent I/O parsing for structured text files.
Definition: ReaderAscii.h:29
HepMC3::GenEvent::set_event_number
void set_event_number(const int &num)
Set event number.
Definition: GenEvent.h:137
HepMC3::GenRunInfo::ToolInfo::name
std::string name
The name of the tool.
Definition: GenRunInfo.h:41
HepMC3::GenEvent::weights
const std::vector< double > & weights() const
Get event weight values as a vector.
Definition: GenEvent.h:86
LHEF::Reader::heprup
HEPRUP heprup
Definition: LHEF.h:3012
HepMC3::HEPEUPAttribute::tags
std::vector< LHEF::XMLTag * > tags
The parsed XML-tags.
Definition: LHEFAttributes.h:116
LHEF::Reader::initComments
std::string initComments
Definition: LHEF.h:3017
LHEF::Writer::writeEvent
void writeEvent()
Definition: LHEF.h:3243
HepMC3::AssociatedParticle::associated
ConstGenParticlePtr associated() const
get a pointer to the associated particle.
Definition: AssociatedParticle.h:57
WriterAscii.h
Definition of class WriterAscii.
HepMC3::AssociatedParticle
Attribute class allowing eg. a GenParticle to refer to another GenParticle.
Definition: AssociatedParticle.h:32
LHEF::HEPEUP::NUP
int NUP
Definition: LHEF.h:2550
LHEF::HEPRUP::eventfiles
std::vector< EventFile > eventfiles
Definition: LHEF.h:2000
LHEF::HEPEUP::IDPRUP
int IDPRUP
Definition: LHEF.h:2555
HepMC3::DoubleAttribute
Attribute that holds a real number as a double.
Definition: Attribute.h:243
LHEF::Reader::headerBlock
std::string headerBlock
Definition: LHEF.h:3007
HepMC3::HEPRUPAttribute
Class for storing data for LHEF run information.
Definition: LHEFAttributes.h:26
HepMC3::GenRunInfo::set_weight_names
void set_weight_names(const std::vector< std::string > &names)
Set the names of the weights in this run.
Definition: GenRunInfo.cc:18
HepMC3::Reader::run_info
std::shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Definition: Reader.h:44
LHEF::HEPEUP::IDUP
std::vector< long > IDUP
Definition: LHEF.h:2589
LHEF::Reader
Definition: LHEF.h:2742
LHEF::HEPRUP::weightinfo
std::vector< WeightInfo > weightinfo
Definition: LHEF.h:2031
LHEF::HEPEUP::ISTUP
std::vector< int > ISTUP
Definition: LHEF.h:2594
LHEF::HEPEUP::heprup
HEPRUP * heprup
Definition: LHEF.h:2630
LHEF::XMLTag::findXMLTags
static std::vector< XMLTag * > findXMLTags(std::string str, std::string *leftover=0)
Definition: LHEF.h:198
AssociatedParticle.h
Definition of class AssociatedParticle,.
HepMC3::HEPEUPAttribute::hepeup
LHEF::HEPEUP hepeup
The actual HEPEUP object.
Definition: LHEFAttributes.h:113
HepMC3::GenRunInfo::ToolInfo
Interrnal struct for keeping track of tools.
Definition: GenRunInfo.h:38
LHEF::HEPEUP::AQEDUP
double AQEDUP
Definition: LHEF.h:2579
ReaderAscii.h
Definition of class ReaderAscii.
LHEF::HEPEUP::AQCDUP
double AQCDUP
Definition: LHEF.h:2584
HepMC3::GenRunInfo::add_attribute
void add_attribute(const std::string &name, const std::shared_ptr< Attribute > &att)
add an attribute This will overwrite existing attribute if an attribute with the same name is present
Definition: GenRunInfo.h:97
LHEF::HEPRUP::weightNameHepMC
std::string weightNameHepMC(int i) const
Definition: LHEF.h:1751
HepMC3::ReaderAscii::read_event
bool read_event(GenEvent &evt) override
Load event from file.
Definition: ReaderAscii.cc:63
HepMC3::GenEvent::particles
const std::vector< ConstGenParticlePtr > & particles() const
Get list of particles (const)
Definition: GenEvent.cc:39
HepMC3::HEPRUPAttribute::tags
std::vector< LHEF::XMLTag * > tags
The parsed XML-tags.
Definition: LHEFAttributes.h:60
main
int main(int argc, char **argv)
Definition: rootIOTree_example_read.cc:23
LHEF::Writer::heprup
HEPRUP heprup
Definition: LHEF.h:3306
LHEF::HEPEUP::weights
std::vector< std::pair< double, const WeightInfo * > > weights
Definition: LHEF.h:2645
HepMC3::GenRunInfo::ToolInfo::description
std::string description
Other information about how the tool was used in the run.
Definition: GenRunInfo.h:48
HepMC3::HEPEUPAttribute::momentum
FourVector momentum(int i) const
Get momentum.
Definition: LHEFAttributes.h:104
HepMC3::WriterAscii::write_event
void write_event(const GenEvent &evt) override
Write event to file.
Definition: WriterAscii.cc:63
LHEF::Writer::hepeup
HEPEUP hepeup
Definition: LHEF.h:3312
HepMC3::GenEvent::attribute
std::shared_ptr< T > attribute(const std::string &name, const int &id=0) const
Get attribute of type T.
Definition: GenEvent.h:389
LHEF::Writer::headerBlock
std::ostream & headerBlock()
Definition: LHEF.h:3131
LHEF::Reader::readEvent
bool readEvent()
Definition: LHEF.h:2868
HepMC3::WriterAscii::close
void close() override
Close file stream.
Definition: WriterAscii.cc:354