Using Attributes
Attributes can be attached to GenEvent, GenParticle or GenVertex and they can have any format defined by the user (see Writing custom attributes). An attribute is accessed through a shared pointer and identified by its name.
Example of reading an attribute from the event:
shared_ptr<GenPdfInfo> pdf_info = event.attribute<GenPdfInfo>("GenPdfInfo");
if( pdf_info ) pdf_info->print();
Example of adding an attribute to the event:
shared_ptr<GenPdfInfo> pdf_info = make_shared<GenPdfInfo>();
evt.add_attribute("GenPdfInfo",pdf_info);
pdf_info->set(1,2,3.4,5.6,7.8,9.0,1.2,3,4);
Adding and getting attributes of a vertex or particle uses the same principles.
- Note
- An event (or particle or vertex) can have more than one attribute of the same type distinguished by different names. This might be useful in some applications, however, we strongly encourage to use just one instance named by its class name, as in these examples.
Writing custom attributes
Any class that derives from HepMC::Attribute class can be used as an attribute that can be attached to the event, vertex or particle.
User has to provide two abstract methods from HepMC::Attribute used to parse the class content from/to string.
Example:
#include "HepMC3/Attribute.h"
struct MyAttribute : public HepMC::Attribute {
double val1;
int val2;
public:
bool from_string(const string &att) {
val1 = stof( att );
val2 = stol( att.substr( att.find(' ')+1 ) );
return true;
}
bool to_string(string &att) const {
char buf[64];
sprintf(buf,"%.8e %i",val1,val2);
att = buf;
return true;
}
};
For other examples see attributes provided in the HepMC3 package.
Last update 27 Oct 2020