-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsplitPandaExpress.C
More file actions
59 lines (56 loc) · 2.92 KB
/
splitPandaExpress.C
File metadata and controls
59 lines (56 loc) · 2.92 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
#include "../interface/splitPandaExpress.h"
void splitPandaExpress(std::string inputFile, Long64_t nEvtsPerFile) {
printf("Opening file \"%s\" and splitting it into files with %lld events...\n", inputFile.c_str(), nEvtsPerFile);
TFile *f = TFile::Open(inputFile.c_str(),"update"); assert(f);
TTree *events=0, *treebuffer;
// Drop extra cycle numbers so rooteventselector can do its job
printf("\tLooking for extra cycle numbers to drop...\n");
TIter keyList(f->GetListOfKeys());
std::vector<short> extraCycleNumbers; short highestCycleNumber=-1;
TKey *key;
while ((key = (TKey*)keyList())) {
TClass *cl = gROOT->GetClass(key->GetClassName());
if (!cl->InheritsFrom("TTree")) continue;
treebuffer=(TTree*)key->ReadObj();
if(strcmp(treebuffer->GetName(),"events")!=0) continue;
short cycleNumber=key->GetCycle();
printf("\t%s;%d\n",treebuffer->GetName(),cycleNumber);
if(cycleNumber > highestCycleNumber) {
// replace the highest cycle number, putting the previous highest in the list of extras if real
if(highestCycleNumber>0) extraCycleNumbers.push_back(highestCycleNumber);
highestCycleNumber=cycleNumber;
} else {
// this is an extra cycle number
extraCycleNumbers.push_back(cycleNumber);
}
}
std::cout << "Extra cycle numbers: ";
for (std::vector<short>::const_iterator i = extraCycleNumbers.begin(); i != extraCycleNumbers.end(); ++i)
std::cout << *i << ", ";
std::cout << std::endl;
for (std::vector<short>::const_iterator i = extraCycleNumbers.begin(); i != extraCycleNumbers.end(); ++i)
f->Delete(Form("events;%d",*i));
// Done dropping extra cycle numbers
events = (TTree*)f->Get(Form("events;%d",highestCycleNumber)); assert(events);
Long64_t nEntries = events->GetEntries();
unsigned nSplit = ceil( float(nEntries) / nEvtsPerFile);
f->Close();
printf("The file had %lld entries, splitting into %d subfiles\n", nEntries, nSplit);
size_t lastDot = inputFile.find_last_of(".");
size_t lastSlash = inputFile.find_last_of("/");
std::string splitDir = inputFile.substr(0, lastSlash) + std::string("/split/");
std::string rawName = splitDir + inputFile.substr(lastSlash+1, lastDot-lastSlash-1);
system(Form("mkdir -p %s",splitDir.c_str()));
for(unsigned i=0; i<nSplit; i++) {
std::string splitName;
if(lastDot!=std::string::npos) splitName = rawName + std::string(Form("_%d.root", i));
else splitName = rawName + std::string(Form("_%d", i));
Long64_t firstEvt = i*nEvtsPerFile;
Long64_t lastEvt = (i+1)*nEvtsPerFile - 1;
const char * command = Form("rooteventselector --recreate -f %lld -l %lld %s:events %s", firstEvt, lastEvt, inputFile.c_str(), splitName.c_str());
printf("\tSubfile #%d: writing event # %lld-%lld to \"%s\"\n", i+1, firstEvt, lastEvt, splitName.c_str());
system(command);
//printf("\t%s\n",command);
}
printf("Done splitting file \"%s\"\n", inputFile.c_str());
}