11 #ifndef Pythia8_LHEF3_H
12 #define Pythia8_LHEF3_H
14 #include "Pythia8/PythiaStdlib.h"
15 #include "Pythia8/Streams.h"
30 typedef string::size_type pos_t;
33 static const pos_t end;
37 for (
int i = 0, N = tags.size(); i < N; ++i )
38 if (tags[i])
delete tags[i];
45 map<string,string> attr;
55 bool getattr(
string n,
double & v)
const {
56 map<string,string>::const_iterator it = attr.find(n);
57 if ( it == attr.end() )
return false;
58 v = atof(it->second.c_str());
64 bool getattr(
string n,
bool & v)
const {
65 map<string,string>::const_iterator it = attr.find(n);
66 if ( it == attr.end() )
return false;
67 if ( it->second ==
"yes" ) v =
true;
73 bool getattr(
string n,
long & v)
const {
74 map<string,string>::const_iterator it = attr.find(n);
75 if ( it == attr.end() )
return false;
76 v = atoi(it->second.c_str());
82 bool getattr(
string n,
int & v)
const {
83 map<string,string>::const_iterator it = attr.find(n);
84 if ( it == attr.end() )
return false;
85 v = int(atoi(it->second.c_str()));
91 bool getattr(
string n,
string & v)
const {
92 map<string,string>::const_iterator it = attr.find(n);
93 if ( it == attr.end() )
return false;
100 static vector<XMLTag*> findXMLTags(
string str,
101 string * leftover = 0) {
102 vector<XMLTag*> tags;
105 while ( curr != end ) {
108 pos_t begin = str.find(
"<", curr);
111 pos_t lastbreak_before_begin = str.find_last_of(
"\n",begin);
112 pos_t lastpound_before_begin = str.find_last_of(
"#",begin);
117 if ( (lastbreak_before_begin < lastpound_before_begin
118 || lastbreak_before_begin == end)
119 && begin > lastpound_before_begin) {
120 pos_t endcom = str.find_first_of(
"\n",begin);
121 if ( endcom == end ) {
122 if ( leftover ) *leftover += str.substr(curr);
125 if ( leftover ) *leftover += str.substr(curr, endcom - curr);
131 if ( begin != end && str.find(
"<!--", curr) == begin ) {
132 pos_t endcom = str.find(
"-->", begin);
133 if ( endcom == end ) {
134 if ( leftover ) *leftover += str.substr(curr);
137 if ( leftover ) *leftover += str.substr(curr, endcom - curr);
148 if ( str.find(
"<![CDATA[", curr) == begin ) {
149 pos_t endcom = str.find(
"]]>", begin);
150 if ( endcom == end ) {
151 if ( leftover ) *leftover += str.substr(curr);
154 if ( leftover ) *leftover += str.substr(curr, endcom - curr);
159 if ( leftover ) *leftover += str.substr(curr, begin - curr);
160 if ( begin == end || begin > str.length() - 3 || str[begin + 1] ==
'/' )
163 pos_t close = str.find(
">", curr);
164 if ( close == end )
return tags;
167 curr = str.find_first_of(
" \t\n/>", begin);
168 tags.push_back(
new XMLTag());
169 tags.back()->name = str.substr(begin + 1, curr - begin - 1);
174 curr = str.find_first_not_of(
" \t\n", curr);
175 if ( curr == end || curr >= close )
break;
177 pos_t tend = str.find_first_of(
"= \t\n", curr);
178 if ( tend == end || tend >= close )
break;
180 string name = str.substr(curr, tend - curr);
181 curr = str.find(
"=", curr) + 1;
184 curr = str.find(
"\"", curr);
185 if ( curr == end || curr >= close )
break;
187 curr = str.find(
"\"", curr);
188 while ( curr != end && str[curr - 1] ==
'\\' )
189 curr = str.find(
"\"", curr + 1);
191 string value = str.substr(bega, curr == end? end: curr - bega);
193 tags.back()->attr[name] = value;
200 if ( str[close - 1] ==
'/' )
continue;
202 pos_t endtag = str.find(
"</" + tags.back()->name +
">", curr);
203 if ( endtag == end ) {
204 tags.back()->contents = str.substr(curr);
207 tags.back()->contents = str.substr(curr, endtag - curr);
208 curr = endtag + tags.back()->name.length() + 3;
212 tags.back()->tags = findXMLTags(tags.back()->contents, &leftovers);
213 if ( leftovers.find_first_not_of(
" \t\n") == end ) leftovers=
"";
214 tags.back()->contents = leftovers;
223 void list(ostream & os)
const {
225 for ( map<string,string>::const_iterator it = attr.begin();
226 it != attr.end(); ++it )
227 os <<
" " << it->first <<
"=\"" << it->second <<
"\"";
228 if ( contents.empty() && tags.empty() ) {
233 for (
int i = 0, N = tags.size(); i < N; ++i )
236 os <<
"````" << contents <<
"''''</" << name <<
">" << endl;
251 LHAweights(
const XMLTag & tag);
254 void list(ostream & file)
const;
264 vector<double> weights;
273 int size()
const {
return int(weights.size()); }
284 LHAscales(
double defscale = -1.0)
285 : muf(defscale), mur(defscale), mups(defscale), SCALUP(defscale) {}
288 LHAscales(
const XMLTag & tag,
double defscale = -1.0);
291 void list(ostream & file)
const;
325 struct LHAgenerator {
329 : name(
""), version(
""), contents(
"") {}
332 LHAgenerator(
const XMLTag & tag,
string defname =
"");
335 void list(ostream & file)
const;
366 LHAwgt(
double defwgt = 1.0)
367 : id(
""), contents(defwgt) {}
370 LHAwgt(
const XMLTag & tag,
double defwgt = 1.0);
373 void list(ostream & file)
const;
400 LHAweight(
string defname =
"")
401 : id(defname), contents(defname) {}
404 LHAweight(
const XMLTag & tag,
string defname =
"");
407 void list(ostream & file)
const;
431 struct LHAweightgroup {
438 LHAweightgroup(
const XMLTag & tag);
441 void list(ostream & file)
const;
458 map<string, LHAweight> weights;
459 vector<string> weightsKeys;
465 int size()
const {
return int(weights.size()); }
480 LHArwgt(
const XMLTag & tag);
483 void list(ostream & file)
const;
496 map<string, LHAwgt> wgts;
497 vector<string> wgtsKeys;
503 int size()
const {
return int(wgts.size()); }
518 LHAinitrwgt(
const XMLTag & tag);
521 void list(ostream & file)
const;
527 weightgroups.clear();
535 map<string, LHAweight> weights;
536 vector<string> weightsKeys;
539 map<string, LHAweightgroup> weightgroups;
540 vector<string> weightgroupsKeys;
546 int size()
const {
return int(weights.size());}
549 int sizeWeightGroups()
const {
return int(weightgroups.size()); }
567 HEPRUP() : IDWTUP(0), NPRUP(0) {}
570 HEPRUP & operator=(
const HEPRUP & x) {
581 initrwgt = x.initrwgt;
582 generators = x.generators;
583 weightgroups = x.weightgroups;
594 void resize(
int nrup) {
603 XSECUP.resize(NPRUP);
604 XERRUP.resize(NPRUP);
605 XMAXUP.resize(NPRUP);
613 pair<long,long> IDBMUP;
616 pair<double,double> EBMUP;
620 pair<int,int> PDFGUP;
624 pair<int,int> PDFSUP;
635 vector<double> XSECUP;
639 vector<double> XERRUP;
643 vector<double> XMAXUP;
649 LHAinitrwgt initrwgt;
652 vector<LHAgenerator> generators;
655 map<string,LHAweightgroup> weightgroups;
658 map<string,LHAweight> weights;
677 : NUP(0), IDPRUP(0), XWGTUP(0.0), XPDWUP(0.0, 0.0),
678 SCALUP(0.0), AQEDUP(0.0), AQCDUP(0.0), heprup(0) {}
681 HEPEUP(
const HEPEUP & x) { operator=(x); }
684 HEPEUP & setEvent(
const HEPEUP & x);
687 HEPEUP & operator=(
const HEPEUP & x) {
709 void resize(
int nup) {
715 double weight()
const {
737 pair<double,double> XPDWUP;
757 vector< pair<int,int> > MOTHUP;
761 vector< pair<int,int> > ICOLUP;
765 vector< vector<double> > PUP;
769 vector<double> VTIMUP;
774 vector<double> SPINUP;
780 map<string,double> weights_detailed;
783 vector<double> weights_compressed;
786 LHAscales scalesSave;
789 LHAweights weightsSave;
827 Reader(
string filenameIn)
828 : filename(filenameIn), intstream(NULL), file(NULL), version() {
829 intstream =
new igzstream(filename.c_str());
835 : filename(
""), intstream(NULL), file(is), version() {
841 if (intstream)
delete intstream;
852 bool setup(
string filenameIn) {
853 filename = filenameIn;
854 if (intstream)
delete intstream;
855 intstream =
new igzstream(filename.c_str());
871 bool readEvent(HEPEUP * peup = 0);
878 weights_detailed_vec.resize(0);
879 weightnames_detailed_vec.resize(0);
887 if(!getline(*file, currentLine))
return false;
889 replace(currentLine.begin(),currentLine.end(),
'\'',
'\"');
900 igzstream* intstream;
923 string headerComments;
935 string eventComments;
938 vector<double> weights_detailed_vec;
939 vector<double> weights_detailed_vector()
940 {
return weights_detailed_vec; }
941 vector<string> weightnames_detailed_vec;
942 vector<string> weightnames_detailed_vector()
943 {
return weightnames_detailed_vec; }
951 Reader(
const Reader &);
954 Reader & operator=(
const Reader &);
1004 Writer(ostream & os)
1005 : file(os), version(3) {}
1009 Writer(
string filename)
1010 : intstream(filename.c_str()), file(intstream), version(3) {}
1016 ostream & headerBlock() {
1017 return headerStream;
1021 ostream & initComments() {
1026 ostream & eventComments() {
1031 void list_end_tag() {
1032 file <<
"</LesHouchesEvents>" << endl;
1041 bool writeEvent(HEPEUP * peup = 0,
int pDigits = 15);
1043 string getEventString(HEPEUP * peup = 0);
1049 string hashline(
string s,
bool comment =
false);
1064 ostringstream headerStream;
1070 ostringstream initStream;
1076 ostringstream eventStream;
1087 Writer(
const Writer &);
1090 Writer & operator=(
const Writer &);
1098 #endif // end Pythia8_LHEF3_H