-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmergeLheFiles.cpp
More file actions
171 lines (139 loc) · 5.08 KB
/
mergeLheFiles.cpp
File metadata and controls
171 lines (139 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// g++ -Wall -o mergeLheFiles mergeLheFiles.cpp
#include <iostream>
#include <fstream>
#include <vector>
int main(int argc, char** argv)
{
if(argc < 3)
{
std::cout << ">>>mergeLheFile.cpp::Usage: " << argv[0] << " initialFile.lhe fileToAdd1.lhe fileToAdd2.lhe ..." << std::endl;
return -1;
}
char* initialFileName = argv[1];
std::cout << "initialFileName = " << initialFileName << std::endl;
std::vector<char*> fileToAddNames;
for(int fileIt = 0; fileIt < argc-2; ++fileIt)
{
fileToAddNames.push_back( argv[2+fileIt] );
std::cout << "fileToAddName = " << fileToAddNames.at(fileIt) << std::endl;
}
// open lhe file
std::ifstream initialFile(initialFileName, std::ios::in);
std::ofstream outFile("out.lhe", std::ios::out);
std::ofstream weightIDFile("weightID.txt", std::ios::out);
std::string line;
std::string line2;
bool writeEvent = false;
bool changeWeightline = false;
int eventIt = 0;
int weightIDIt = 0;
std::vector<std::string> weightID;
// loop over initial file to write all the information contained in
// comments, header and init block
while(!initialFile.eof())
{
getline(initialFile, line);
//std::cout << line << std::endl;
if( !initialFile.good() ) break;
// readout the weightID !!!string of four digits!!! from <header> block
//if( line.find("<weight id=") != std::string::npos )
if( line.find("<weight id=") == 0)
{
//std::cout << "Position: " << line.find("<weight id=") << std::endl;
std::string token = line.substr(line.find("\'")+1, 4);
//std::cout << "ID: " << token << std::endl;
weightIDFile << token << std::endl;
weightID.push_back(token);
}
if( line != "</LesHouchesEvents>" )
{
// start of the event's <weights> block
if( line == "<weights>" )
{
outFile << "<rwgt>" << std::endl;
//std::cout << "<rwgt>" << std::endl;
changeWeightline = true;
weightIDIt = 0;
continue;
}
// end of <weights> block
else if( line == "</weights>" )
{
outFile << "</rwgt>" << std::endl;
//std::cout << "</rwgt>" << std::endl;
weightIDIt = 0;
changeWeightline = false;
}
// change the line with weight
else if( changeWeightline == true )
{
outFile << "<wgt id=\'" << weightID.at(weightIDIt) << "\'>" << line << "</wgt>" << std::endl;
//std::cout << "<wgt id=\'" << weightID.at(weightIDIt) << "\'>" << line << "</wgt>" << std::endl;
weightIDIt++;
}
// all other lines write normally
//~ if( line != "<weights>" && line != "</weights>" && changeWeightline == false )
else
{
outFile << line << std::endl;
//std::cout << line << std::endl;
}
}
// end of the lhe file content
if( line == "</LesHouchesEvents>" )
{
// loop over all other lhe files
for(int fileIt = 0; fileIt < argc-2; ++fileIt)
{
std::ifstream fileToAdd(fileToAddNames.at(fileIt), std::ios::in);
while(!fileToAdd.eof())
{
getline(fileToAdd, line2);
// decide whether to skip event or not
if( line2 == "<event>" )
{
++eventIt;
writeEvent = true;
}
// write line to outFile
if(writeEvent == true)
{
// start of the event's <weights> block
if( line2 == "<weights>" )
{
outFile << "<rwgt>" << std::endl;
changeWeightline = true;
weightIDIt =0;
continue;
}
// end of <weights> block
else if( line2 == "</weights>" )
{
outFile << "</rwgt>" << std::endl;
weightIDIt = 0;
changeWeightline = false;
}
// change the line with weight
else if( changeWeightline == true )
{
outFile << "<wgt id=\'" << weightID.at(weightIDIt) << "\'>" << line2 << "</wgt>" << std::endl;
weightIDIt++;
}
// all other lines write normally
//~ if( line2 != "<weights>" && line2 != "</weights>" && changeWeightline == false )
else
{
outFile << line2 << std::endl;
}
}
// end of event
if( line2 == "</event>" )
writeEvent = false;
}
}
outFile << "</LesHouchesEvents>" << std::endl;
}
}
std::cout << "Added " << eventIt << " events from " << argc-2 << " files to file " << initialFileName << std::endl;
return 0;
}