File indexing completed on 2025-01-18 10:06:26
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef Pythia8_LHEF3_H
0012 #define Pythia8_LHEF3_H
0013
0014 #include "Pythia8/PythiaStdlib.h"
0015 #include "Pythia8/Streams.h"
0016 #include <stdexcept>
0017
0018 namespace Pythia8 {
0019
0020
0021
0022
0023
0024
0025
0026
0027 struct XMLTag {
0028
0029
0030 typedef string::size_type pos_t;
0031
0032
0033 static const pos_t end;
0034
0035
0036 ~XMLTag() {
0037 for ( int i = 0, N = tags.size(); i < N; ++i )
0038 if (tags[i]) delete tags[i];
0039 }
0040
0041
0042 string name;
0043
0044
0045 map<string,string> attr;
0046
0047
0048 vector<XMLTag*> tags;
0049
0050
0051 string contents;
0052
0053
0054
0055 bool getattr(string n, double & v) const {
0056 map<string,string>::const_iterator it = attr.find(n);
0057 if ( it == attr.end() ) return false;
0058 v = atof(it->second.c_str());
0059 return true;
0060 }
0061
0062
0063
0064 bool getattr(string n, bool & v) const {
0065 map<string,string>::const_iterator it = attr.find(n);
0066 if ( it == attr.end() ) return false;
0067 if ( it->second == "yes" ) v = true;
0068 return true;
0069 }
0070
0071
0072
0073 bool getattr(string n, long & v) const {
0074 map<string,string>::const_iterator it = attr.find(n);
0075 if ( it == attr.end() ) return false;
0076 v = atoi(it->second.c_str());
0077 return true;
0078 }
0079
0080
0081
0082 bool getattr(string n, int & v) const {
0083 map<string,string>::const_iterator it = attr.find(n);
0084 if ( it == attr.end() ) return false;
0085 v = int(atoi(it->second.c_str()));
0086 return true;
0087 }
0088
0089
0090
0091 bool getattr(string n, string & v) const {
0092 map<string,string>::const_iterator it = attr.find(n);
0093 if ( it == attr.end() ) return false;
0094 v = it->second;
0095 return true;
0096 }
0097
0098
0099
0100 static vector<XMLTag*> findXMLTags(string str,
0101 string * leftover = 0) {
0102 vector<XMLTag*> tags;
0103 pos_t curr = 0;
0104
0105 while ( curr != end ) {
0106
0107
0108 pos_t begin = str.find("<", curr);
0109
0110
0111 pos_t lastbreak_before_begin = str.find_last_of("\n",begin);
0112 pos_t lastpound_before_begin = str.find_last_of("#",begin);
0113
0114
0115
0116
0117 if ( (lastbreak_before_begin < lastpound_before_begin
0118 || lastbreak_before_begin == end)
0119 && begin > lastpound_before_begin) {
0120 pos_t endcom = str.find_first_of("\n",begin);
0121 if ( endcom == end ) {
0122 if ( leftover ) *leftover += str.substr(curr);
0123 return tags;
0124 }
0125 if ( leftover ) *leftover += str.substr(curr, endcom - curr);
0126 curr = endcom;
0127 continue;
0128 }
0129
0130
0131 if ( begin != end && str.find("<!--", curr) == begin ) {
0132 pos_t endcom = str.find("-->", begin);
0133 if ( endcom == end ) {
0134 if ( leftover ) *leftover += str.substr(curr);
0135 return tags;
0136 }
0137 if ( leftover ) *leftover += str.substr(curr, endcom - curr);
0138 curr = endcom;
0139 continue;
0140 }
0141
0142
0143
0144
0145
0146
0147
0148 if ( str.find("<![CDATA[", curr) == begin ) {
0149 pos_t endcom = str.find("]]>", begin);
0150 if ( endcom == end ) {
0151 if ( leftover ) *leftover += str.substr(curr);
0152 return tags;
0153 }
0154 if ( leftover ) *leftover += str.substr(curr, endcom - curr);
0155 curr = endcom;
0156 continue;
0157 }
0158
0159 if ( leftover ) *leftover += str.substr(curr, begin - curr);
0160 if ( begin == end || begin > str.length() - 3 || str[begin + 1] == '/' )
0161 return tags;
0162
0163 pos_t close = str.find(">", curr);
0164 if ( close == end ) return tags;
0165
0166
0167 curr = str.find_first_of(" \t\n/>", begin);
0168 tags.push_back(new XMLTag());
0169 tags.back()->name = str.substr(begin + 1, curr - begin - 1);
0170
0171 while ( true ) {
0172
0173
0174 curr = str.find_first_not_of(" \t\n", curr);
0175 if ( curr == end || curr >= close ) break;
0176
0177 pos_t tend = str.find_first_of("= \t\n", curr);
0178 if ( tend == end || tend >= close ) break;
0179
0180 string name = str.substr(curr, tend - curr);
0181 curr = str.find("=", curr) + 1;
0182
0183
0184 curr = str.find("\"", curr);
0185 if ( curr == end || curr >= close ) break;
0186 pos_t bega = ++curr;
0187 curr = str.find("\"", curr);
0188 while ( curr != end && str[curr - 1] == '\\' )
0189 curr = str.find("\"", curr + 1);
0190
0191 string value = str.substr(bega, curr == end? end: curr - bega);
0192
0193 tags.back()->attr[name] = value;
0194
0195 ++curr;
0196
0197 }
0198
0199 curr = close + 1;
0200 if ( str[close - 1] == '/' ) continue;
0201
0202 pos_t endtag = str.find("</" + tags.back()->name + ">", curr);
0203 if ( endtag == end ) {
0204 tags.back()->contents = str.substr(curr);
0205 curr = endtag;
0206 } else {
0207 tags.back()->contents = str.substr(curr, endtag - curr);
0208 curr = endtag + tags.back()->name.length() + 3;
0209 }
0210
0211 string leftovers;
0212 tags.back()->tags = findXMLTags(tags.back()->contents, &leftovers);
0213 if ( leftovers.find_first_not_of(" \t\n") == end ) leftovers="";
0214 tags.back()->contents = leftovers;
0215
0216 }
0217
0218 return tags;
0219
0220 }
0221
0222
0223 void list(ostream & os) const {
0224 os << "<" << name;
0225 for ( map<string,string>::const_iterator it = attr.begin();
0226 it != attr.end(); ++it )
0227 os << " " << it->first << "=\"" << it->second << "\"";
0228 if ( contents.empty() && tags.empty() ) {
0229 os << "/>" << endl;
0230 return;
0231 }
0232 os << ">" << endl;
0233 for ( int i = 0, N = tags.size(); i < N; ++i )
0234 tags[i]->list(os);
0235
0236 os << "````" << contents << "''''</" << name << ">" << endl;
0237 }
0238
0239 };
0240
0241
0242
0243
0244
0245 struct LHAweights {
0246
0247
0248 LHAweights() {}
0249
0250
0251 LHAweights(const XMLTag & tag);
0252
0253
0254 void list(ostream & file) const;
0255
0256
0257 void clear() {
0258 contents="";
0259 weights.clear();
0260 attributes.clear();
0261 }
0262
0263
0264 vector<double> weights;
0265
0266
0267 map<string,string> attributes;
0268
0269
0270 string contents;
0271
0272
0273 int size() const { return int(weights.size()); }
0274
0275 };
0276
0277
0278
0279
0280
0281 struct LHAscales {
0282
0283
0284 LHAscales(double defscale = -1.0)
0285 : muf(defscale), mur(defscale), mups(defscale), SCALUP(defscale) {}
0286
0287
0288 LHAscales(const XMLTag & tag, double defscale = -1.0);
0289
0290
0291 void list(ostream & file) const;
0292
0293
0294 void clear() {
0295 contents="";
0296 muf=mur=mups=SCALUP;
0297 attributes.clear();
0298 }
0299
0300
0301 double muf;
0302
0303
0304 double mur;
0305
0306
0307
0308 double mups;
0309
0310
0311 map<string,double> attributes;
0312
0313
0314 double SCALUP;
0315
0316
0317 string contents;
0318
0319 };
0320
0321
0322
0323
0324
0325 struct LHAgenerator {
0326
0327
0328 LHAgenerator()
0329 : name(""), version(""), contents("") {}
0330
0331
0332 LHAgenerator(const XMLTag & tag, string defname = "");
0333
0334
0335 void list(ostream & file) const;
0336
0337
0338 void clear() {
0339 contents="";
0340 name="";
0341 version="";
0342 attributes.clear();
0343 }
0344
0345
0346 string name;
0347
0348
0349 string version;
0350
0351
0352 map<string,string> attributes;
0353
0354
0355 string contents;
0356
0357 };
0358
0359
0360
0361
0362
0363 struct LHAwgt {
0364
0365
0366 LHAwgt(double defwgt = 1.0)
0367 : id(""), contents(defwgt) {}
0368
0369
0370 LHAwgt(const XMLTag & tag, double defwgt = 1.0);
0371
0372
0373 void list(ostream & file) const;
0374
0375
0376 void clear() {
0377 contents=0.0;
0378 id="";
0379 attributes.clear();
0380 }
0381
0382
0383 string id;
0384
0385
0386 map<string,string> attributes;
0387
0388
0389 double contents;
0390
0391 };
0392
0393
0394
0395
0396
0397 struct LHAweight {
0398
0399
0400 LHAweight(string defname = "")
0401 : id(defname), contents(defname) {}
0402
0403
0404 LHAweight(const XMLTag & tag, string defname = "");
0405
0406
0407 void list(ostream & file) const;
0408
0409
0410 void clear() {
0411 contents="";
0412 id="";
0413 attributes.clear();
0414 }
0415
0416
0417 string id;
0418
0419
0420 map<string,string> attributes;
0421
0422
0423 string contents;
0424
0425 };
0426
0427
0428
0429
0430
0431 struct LHAweightgroup {
0432
0433
0434 LHAweightgroup() {}
0435
0436
0437
0438 LHAweightgroup(const XMLTag & tag);
0439
0440
0441 void list(ostream & file) const;
0442
0443
0444 void clear() {
0445 contents="";
0446 name="";
0447 weights.clear();
0448 attributes.clear();
0449 }
0450
0451
0452 string contents;
0453
0454
0455 string name;
0456
0457
0458 map<string, LHAweight> weights;
0459 vector<string> weightsKeys;
0460
0461
0462 map<string,string> attributes;
0463
0464
0465 int size() const { return int(weights.size()); }
0466
0467 };
0468
0469
0470
0471
0472
0473 struct LHArwgt {
0474
0475
0476 LHArwgt() {}
0477
0478
0479
0480 LHArwgt(const XMLTag & tag);
0481
0482
0483 void list(ostream & file) const;
0484
0485
0486 void clear() {
0487 contents="";
0488 wgts.clear();
0489 attributes.clear();
0490 }
0491
0492
0493 string contents;
0494
0495
0496 map<string, LHAwgt> wgts;
0497 vector<string> wgtsKeys;
0498
0499
0500 map<string,string> attributes;
0501
0502
0503 int size() const { return int(wgts.size()); }
0504
0505 };
0506
0507
0508
0509
0510
0511 struct LHAinitrwgt {
0512
0513
0514 LHAinitrwgt() {}
0515
0516
0517
0518 LHAinitrwgt(const XMLTag & tag);
0519
0520
0521 void list(ostream & file) const;
0522
0523
0524 void clear() {
0525 contents="";
0526 weights.clear();
0527 weightgroups.clear();
0528 attributes.clear();
0529 }
0530
0531
0532 string contents;
0533
0534
0535 map<string, LHAweight> weights;
0536 vector<string> weightsKeys;
0537
0538
0539 map<string, LHAweightgroup> weightgroups;
0540 vector<string> weightgroupsKeys;
0541
0542
0543 map<string,string> attributes;
0544
0545
0546 int size() const { return int(weights.size());}
0547
0548
0549 int sizeWeightGroups() const { return int(weightgroups.size()); }
0550
0551 };
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561
0562 class HEPRUP {
0563
0564 public:
0565
0566
0567 HEPRUP() : IDWTUP(0), NPRUP(0) {}
0568
0569
0570 HEPRUP & operator=(const HEPRUP & x) {
0571 IDBMUP = x.IDBMUP;
0572 EBMUP = x.EBMUP;
0573 PDFGUP = x.PDFGUP;
0574 PDFSUP = x.PDFSUP;
0575 IDWTUP = x.IDWTUP;
0576 NPRUP = x.NPRUP;
0577 XSECUP = x.XSECUP;
0578 XERRUP = x.XERRUP;
0579 XMAXUP = x.XMAXUP;
0580 LPRUP = x.LPRUP;
0581 initrwgt = x.initrwgt;
0582 generators = x.generators;
0583 weightgroups = x.weightgroups;
0584 weights = x.weights;
0585 return *this;
0586 }
0587
0588
0589 ~HEPRUP() {}
0590
0591
0592
0593
0594 void resize(int nrup) {
0595 NPRUP = nrup;
0596 resize();
0597 }
0598
0599
0600
0601
0602 void resize() {
0603 XSECUP.resize(NPRUP);
0604 XERRUP.resize(NPRUP);
0605 XMAXUP.resize(NPRUP);
0606 LPRUP.resize(NPRUP);
0607 }
0608
0609
0610 void clear();
0611
0612
0613 pair<long,long> IDBMUP;
0614
0615
0616 pair<double,double> EBMUP;
0617
0618
0619
0620 pair<int,int> PDFGUP;
0621
0622
0623
0624 pair<int,int> PDFSUP;
0625
0626
0627
0628
0629 int IDWTUP;
0630
0631
0632 int NPRUP;
0633
0634
0635 vector<double> XSECUP;
0636
0637
0638
0639 vector<double> XERRUP;
0640
0641
0642
0643 vector<double> XMAXUP;
0644
0645
0646 vector<int> LPRUP;
0647
0648
0649 LHAinitrwgt initrwgt;
0650
0651
0652 vector<LHAgenerator> generators;
0653
0654
0655 map<string,LHAweightgroup> weightgroups;
0656
0657
0658 map<string,LHAweight> weights;
0659
0660 };
0661
0662
0663
0664
0665
0666
0667
0668
0669
0670
0671 class HEPEUP {
0672
0673 public:
0674
0675
0676 HEPEUP()
0677 : NUP(0), IDPRUP(0), XWGTUP(0.0), XPDWUP(0.0, 0.0),
0678 SCALUP(0.0), AQEDUP(0.0), AQCDUP(0.0), heprup(0) {}
0679
0680
0681 HEPEUP(const HEPEUP & x) { operator=(x); }
0682
0683
0684 HEPEUP & setEvent(const HEPEUP & x);
0685
0686
0687 HEPEUP & operator=(const HEPEUP & x) {
0688 clear();
0689 setEvent(x);
0690 return *this;
0691 }
0692
0693
0694 ~HEPEUP() {
0695 clear();
0696 };
0697
0698
0699 void reset();
0700
0701
0702 void clear() {
0703 reset();
0704 }
0705
0706
0707
0708
0709 void resize(int nup) {
0710 NUP = nup;
0711 resize();
0712 }
0713
0714
0715 double weight() const {
0716 return XWGTUP;
0717 }
0718
0719
0720
0721
0722 void resize();
0723
0724
0725 int NUP;
0726
0727
0728 int IDPRUP;
0729
0730
0731 double XWGTUP;
0732
0733
0734
0735
0736
0737 pair<double,double> XPDWUP;
0738
0739
0740
0741 double SCALUP;
0742
0743
0744 double AQEDUP;
0745
0746
0747 double AQCDUP;
0748
0749
0750 vector<long> IDUP;
0751
0752
0753 vector<int> ISTUP;
0754
0755
0756
0757 vector< pair<int,int> > MOTHUP;
0758
0759
0760
0761 vector< pair<int,int> > ICOLUP;
0762
0763
0764
0765 vector< vector<double> > PUP;
0766
0767
0768
0769 vector<double> VTIMUP;
0770
0771
0772
0773
0774 vector<double> SPINUP;
0775
0776
0777 HEPRUP * heprup;
0778
0779
0780 map<string,double> weights_detailed;
0781
0782
0783 vector<double> weights_compressed;
0784
0785
0786 LHAscales scalesSave;
0787
0788
0789 LHAweights weightsSave;
0790
0791
0792 LHArwgt rwgtSave;
0793
0794
0795 map<string,string> attributes;
0796
0797 };
0798
0799
0800
0801
0802
0803
0804
0805
0806
0807
0808
0809
0810
0811
0812
0813
0814 class Reader {
0815
0816 public:
0817
0818
0819
0820
0821
0822
0823
0824
0825
0826
0827 Reader(string filenameIn)
0828 : filename(filenameIn), intstream(nullptr), file(nullptr), version() {
0829 intstream = new igzstream(filename.c_str());
0830 file = intstream;
0831 isGood = init();
0832 }
0833
0834 Reader(istream* is)
0835 : filename(""), intstream(nullptr), file(is), version() {
0836 isGood = init();
0837 }
0838
0839
0840 ~Reader() {
0841 if (intstream) delete intstream;
0842 }
0843
0844
0845
0846
0847
0848
0849
0850
0851
0852 bool setup(string filenameIn) {
0853 filename = filenameIn;
0854 if (intstream) delete intstream;
0855 intstream = new igzstream(filename.c_str());
0856 file = intstream;
0857 isGood = init();
0858 return isGood;
0859 }
0860
0861 private:
0862
0863
0864 bool init();
0865
0866 public:
0867
0868
0869
0870
0871 bool readEvent(HEPEUP * peup = 0);
0872
0873
0874 void clearEvent() {
0875 currentLine = "";
0876 hepeup.clear();
0877 eventComments = "";
0878 weights_detailed_vec.resize(0);
0879 weightnames_detailed_vec.resize(0);
0880 }
0881
0882 protected:
0883
0884
0885 bool getLine() {
0886 currentLine = "";
0887 if(!getline(*file, currentLine)) return false;
0888
0889 replace(currentLine.begin(),currentLine.end(),'\'','\"');
0890 return true;
0891 }
0892
0893 protected:
0894
0895
0896 string filename;
0897
0898
0899
0900 igzstream* intstream;
0901
0902
0903
0904 istream * file;
0905
0906
0907 string currentLine;
0908
0909 public:
0910
0911
0912 bool isGood;
0913
0914
0915 int version;
0916
0917
0918
0919 string outsideBlock;
0920
0921
0922 string headerBlock;
0923 string headerComments;
0924
0925
0926 HEPRUP heprup;
0927
0928
0929 string initComments;
0930
0931
0932 HEPEUP hepeup;
0933
0934
0935 string eventComments;
0936
0937
0938 vector<double> weights_detailed_vec;
0939 vector<double> weights_detailed_vector()
0940 { return weights_detailed_vec; }
0941 vector<string> weightnames_detailed_vec;
0942 vector<string> weightnames_detailed_vector()
0943 { return weightnames_detailed_vec; }
0944
0945 private:
0946
0947
0948 Reader();
0949
0950
0951 Reader(const Reader &);
0952
0953
0954 Reader & operator=(const Reader &);
0955
0956 };
0957
0958
0959
0960
0961
0962
0963
0964
0965
0966
0967
0968
0969
0970
0971
0972
0973
0974
0975
0976
0977
0978
0979
0980
0981
0982
0983
0984
0985
0986
0987
0988
0989
0990
0991
0992
0993
0994
0995
0996
0997
0998 class Writer {
0999
1000 public:
1001
1002
1003
1004 Writer(ostream & os)
1005 : file(os), version(3) {}
1006
1007
1008
1009 Writer(string filename)
1010 : intstream(filename.c_str()), file(intstream), version(3) {}
1011
1012
1013 ~Writer() {}
1014
1015
1016 ostream & headerBlock() {
1017 return headerStream;
1018 }
1019
1020
1021 ostream & initComments() {
1022 return initStream;
1023 }
1024
1025
1026 ostream & eventComments() {
1027 return eventStream;
1028 }
1029
1030
1031 void list_end_tag() {
1032 file << "</LesHouchesEvents>" << endl;
1033 }
1034
1035
1036
1037 void init();
1038
1039
1040
1041 bool writeEvent(HEPEUP * peup = 0, int pDigits = 15);
1042
1043 string getEventString(HEPEUP * peup = 0);
1044
1045 protected:
1046
1047
1048
1049 string hashline(string s, bool comment = false);
1050
1051 protected:
1052
1053
1054
1055 ofstream intstream;
1056
1057
1058
1059 ostream & file;
1060
1061 public:
1062
1063
1064 ostringstream headerStream;
1065
1066
1067 HEPRUP heprup;
1068
1069
1070 ostringstream initStream;
1071
1072
1073 HEPEUP hepeup;
1074
1075
1076 ostringstream eventStream;
1077
1078
1079 int version;
1080
1081 private:
1082
1083
1084 Writer();
1085
1086
1087 Writer(const Writer &);
1088
1089
1090 Writer & operator=(const Writer &);
1091
1092 };
1093
1094
1095
1096 }
1097
1098 #endif