-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_all.c
More file actions
73 lines (62 loc) · 2.67 KB
/
merge_all.c
File metadata and controls
73 lines (62 loc) · 2.67 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
#include <map>
#include <iostream>
#include <fstream>
#include "TSystemDirectory.h"
#include "TFile.h"
#include "TNtupleD.h"
#include "TRegexp.h"
using namespace std;
/**
* Merge all files in folder path with names matching the regular expression filematch into trees in a ROOT file.
*
* @param filematch Merge files whose names match this regular expression (default: read all)
* @param path Merge files in this folder (default: current folder)
*/
void merge_all(const char *filematch = "[0-9]+[A-Za-z]+.out", const char *path = ".")
{
TFile *outfile = new TFile("out.root","RECREATE"); // create ROOT file
ifstream infile;
TSystemDirectory dir(path,path); // open given folder
TList *files = dir.GetListOfFiles(); // get all files in that folder
TRegexp re(filematch); // create regular expression from given parameter
outfile->cd(); // switch current directory to ROOT file
Int_t n = 0;
for (Int_t i = 0; ; i++){ // for loop incrementing index i
TObject *f = files->At(i); // get file from folder with index i
if (f){ // if next file was found
TString filename = f->GetName(); // get filename
TString fn = filename;
fn.Resize(filename.Length() - 4); // shorten filename by extension ".out"
ULong64_t jobnumber;
char logtype[64];
double data[1024];
if ((re.Index(filename, &n) == 0) && (sscanf(fn.Data(), "%Ld%s", &jobnumber, logtype) == 2)){ // if filename matches regular expression and contains jobnumber
infile.open(filename.Data()); // open file
TNtupleD *tree = (TNtupleD*)outfile->Get(logtype); // get corresponding tree from file
if (!tree) { // if tree does not yet exist
TString bdescriptor;
bdescriptor.ReadLine(infile); // read branch descriptor from file header
bdescriptor.ReplaceAll(" ",":"); // format branch descriptor for root ("x y z" -> "x:y:z")
tree = new TNtupleD(logtype, logtype, bdescriptor.Data()); // create new tree with name logtype from filename
printf("%ss have %i columns\n", logtype, tree->GetNvar());
}
else
infile.ignore(9999, '\n'); // if tree already exists skip file header
n = tree->GetNvar(); // get number of file columns
cout << "Adding " << filename << '\n';
while (1){
for (int j = 0; j < n; j++) infile >> data[j]; // read values into data array
if (!infile) break; // if something happened during reading: stop
tree->Fill(data); // fill data into tree
}
infile.close(); // close file
cout << "Entries: " << tree->GetEntries() << '\n';
}
}
else break;
}
cout << "Done!" << endl;
outfile->Write();
outfile->Close(); // close ROOT file
delete outfile;
}