-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmergeLheFiles.cpp
106 lines (83 loc) · 2.73 KB
/
mergeLheFiles.cpp
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
// g++ -Wall -o mergeLheFiles mergeLheFiles.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <string.h>
#include <stdio.h>
int main(int argc, char** argv)
{
std::string line;
std::string line2;
/* if(argc < 3)
{
std::cout << ">>>splitLheFile.cpp::Usage: " << argv[0] << " initialFile.lhe fileToAdd1.lhe fileToAdd2.lhe ..." << std::endl;
return -1;
}*/
char* fileListName = argv[1];
std::ifstream fileList(fileListName, std::ios::in);
int fileIt = 0;
char* initialFileName;
std::vector<char*> fileToAddNames;
while(!fileList.eof()) {
getline(fileList, line);
if( !fileList.good() ) break;
char *cline = new char[line.length() + 1];
strcpy(cline, line.c_str());
if (fileIt==0) {
initialFileName = cline;
fileToAddNames.push_back( cline );
std::cout << "initialFileName = " << initialFileName << " add = "<< fileToAddNames.at(fileIt) <<std::endl;
} else {
fileToAddNames.push_back( cline );
std::cout << "fileToAddName = " << fileToAddNames.at(fileIt) << std::endl;
}
fileIt++;
}
std::cout << "Merging " << fileIt << " LHE files" << std::endl;
/* 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);
bool writeEvent = false;
int eventIt = 0;
while(!initialFile.eof())
{
getline(initialFile, line);
if( !initialFile.good() ) break;
if( line == "</LesHouchesEvents>" )
{
for(int fileIt2 = 0; fileIt2 < fileIt-1; ++fileIt2)
{
std::ifstream fileToAdd(fileToAddNames.at(fileIt2), 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)
outFile << line2 << std::endl;
// end of event
if( line2 == "</event>" )
writeEvent = false;
}
}
break;
}
else outFile << line << std::endl;
}
outFile << "</LesHouchesEvents>" << std::endl;
std::cout << "Added " << eventIt << " events from " << fileIt-1 << " files to file " << initialFileName << std::endl;
return 0;
}