-
Notifications
You must be signed in to change notification settings - Fork 1
/
eventMapper.C
105 lines (96 loc) · 4.51 KB
/
eventMapper.C
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
#define eventMapper_cxx
#include "eventMapper.h"
#include <TFile.h>
#include <TTree.h>
void eventMapper::Loop(std::string triggerName)
{
// In a ROOT session, you can do:
// root> .L eventMapper.C
// root> eventMapper t
// root> t.GetEntry(12); // Fill t data members with entry number 12
// root> t.Show(); // Show values of entry 12
// root> t.Show(16); // Read and show values of entry 16
// root> t.Loop(); // Loop on all entries
//
// This is the loop skeleton where:
// jentry is the global entry number in the chain
// ientry is the entry number in the current Tree
// Note that the argument to GetEntry must be:
// jentry for TChain::GetEntry
// ientry for TTree::GetEntry and TBranch::GetEntry
//
// To read only selected branches, Insert statements like:
// METHOD1:
cout << "about to set branch addresses" << endl;
fChain->SetBranchStatus("*",0); // disable all branches
fChain->SetBranchStatus("EVENT_event",1); // activate branchname
fChain->SetBranchStatus("EVENT_lumiBlock",1); // activate branchname
fChain->SetBranchStatus("EVENT_run",1); // activate branchname
fChain->SetBranchStatus( "HLT_isFired" , 1 ); // activate select branches
cout << "done setting branch addresses" << endl;
// METHOD2: replace line
// fChain->GetEntry(jentry); //read all branches
//by b_branchname->GetEntry(ientry); //read only this branch
if (fChain == 0) return;
Long64_t nentries = fChain->GetEntries();
cout << "nentries is " << nentries << endl;
Long64_t nbytes = 0, nb = 0;
for (Long64_t jentry=0; jentry<nentries;jentry++) {
Long64_t ientry = LoadTree(jentry);
if (ientry < 0) break;
nb = fChain->GetEntry(jentry); nbytes += nb;
// if (Cut(ientry) < 0) continue;
for(map<string,bool>::iterator it = HLT_isFired->begin(); it != HLT_isFired->end(); ++it) {
if (it->first.find(triggerName + "_") != std::string::npos ) {
if (1==it->second) addEvent((unsigned int)EVENT_run, (unsigned int)EVENT_lumiBlock, (unsigned long long)EVENT_event);
}
}
if (jentry%25000==0) {
cout.flush();
cout << fixed << setw(4) << setprecision(2) << (float(jentry)/float(nentries))*100 << "% done: Scanned " << jentry << " events." << '\r';
}
}
TFile* outFile = new TFile((std::string("eventMap_") + triggerName + std::string(".root") ).c_str(), "RECREATE");
TTree* mapTree = new TTree("eventMap", "eventMap");
mapTree->Branch("eventMap", &eventMap);
mapTree->Fill();
mapTree->Write();
outFile->Close();
cout.flush();
cout << "100% done: Scanned " << nentries << " events." << endl;
}
unsigned short eventMapper::findEvent(unsigned int run, unsigned int lumiBlock, unsigned long long event) {
std::unordered_map<unsigned int, std::unordered_map<unsigned int, std::vector<unsigned long long> > >::iterator runIt = eventMap.find(run);
if (runIt != eventMap.end()) {
std::unordered_map<unsigned int, std::vector<unsigned long long> >::iterator lumiIt = eventMap.at(run).find(lumiBlock);
if (lumiIt != eventMap.at(run).end()) {
if (std::find(eventMap.at(run).at(lumiBlock).begin(), eventMap.at(run).at(lumiBlock).end(), event) != eventMap.at(run).at(lumiBlock).end()) {
return 0; // found the event
}
else return 1; // found the run and lumiblock, but the event wasn't there
}
else return 2; // found the run, but lumiblock wasn't there
}
else return 3; // didn't find the run
}
void eventMapper::addEvent(unsigned int run, unsigned int lumiBlock, unsigned long long event) {
unsigned short searchResult = findEvent(run, lumiBlock, event);
if (searchResult==0) {
std::cout << "Error! Trying to add an event that is already present!" << endl;
}
else if (searchResult == 1) {
eventMap.at(run).at(lumiBlock).push_back(event);
}
else if (searchResult == 2) {
std::vector<unsigned long long> newLumiBlock;
newLumiBlock.push_back(event);
eventMap.at(run).insert(std::pair<unsigned int, std::vector<unsigned long long> >(lumiBlock, newLumiBlock));
}
else if (searchResult == 3) {
std::unordered_map<unsigned int, std::vector<unsigned long long> > newRun;
std::vector<unsigned long long> newLumiBlock;
newLumiBlock.push_back(event);
newRun.insert(std::pair<unsigned int, std::vector<unsigned long long> >(lumiBlock, newLumiBlock));
eventMap.insert(std::pair<unsigned int, std::unordered_map<unsigned int, std::vector<unsigned long long> > >(run, newRun));
}
}