babymaker  e95a6a9342d4604277fe7cc6149b6b5b24447d89
event_tools.cc
Go to the documentation of this file.
1 
4 // System include files
5 #include <algorithm>
6 #include <iostream>
7 #include <fstream>
8 
9 // FW include files
10 #include "FWCore/Framework/interface/ESHandle.h"
11 #include "DataFormats/Math/interface/deltaR.h"
12 
13 // User include files
16 
17 using namespace std;
18 using namespace utilities;
19 
20 bool event_tools::isInJSON(string type, int run, int lumiblock){
21  //if(type=="golden") return inJSON(VRunLumi2015golden, run, lumiblock);
22  if(type=="golden") return true; // Applying this in bmaker_*_cfg.py
23  if(type=="nonblind") return inJSON(VRunLumi2015nonblind, run, lumiblock);
24  if(type=="json2p6") return inJSON(VRunLumi2016json2p6, run, lumiblock);
25 
26  return true;
27 }
28 
29 bool event_tools::hasGoodPV(edm::Handle<reco::VertexCollection> vtx){
30  bool one_good_pv(false);
31  for(unsigned ipv(0); ipv < vtx->size(); ipv++){
32  const double pv_rho(sqrt(pow(vtx->at(ipv).x(),2) + pow(vtx->at(ipv).y(),2)));
33  if(vtx->at(ipv).ndof()>4 && fabs(vtx->at(ipv).z())<24. && pv_rho<2.0 && vtx->at(ipv).isFake()==0){
34  one_good_pv = true;
35  break;
36  }
37  } // Loop over vertices
38  return one_good_pv;
39 }
40 
41 bool event_tools::passBeamHalo(int run, int event){
42  if(!doBeamHalo) return true;
43  if(badBeamHaloEvents.find(run) == badBeamHaloEvents.end()) return true;
44  if(badBeamHaloEvents[run].find(event) == badBeamHaloEvents[run].end()) return true;
45  return false;
46 }
47 
50 bool event_tools::passFSMET(edm::Handle<pat::JetCollection> alljets, edm::Handle<edm::View <reco::GenJet> > genjets){
51  for (size_t ijet(0); ijet < alljets->size(); ijet++) {
52  const pat::Jet &jet = (*alljets)[ijet];
53  if(fabs(jet.eta()) >= 2.5 || jet.pt()<=20 || jet.chargedHadronEnergyFraction()>=0.1) continue;
54 
55  bool matched = false;
56  for (size_t gjet(0); gjet < genjets->size(); gjet++) {
57  const reco::GenJet &genjet = (*genjets)[gjet];
58  double dr(deltaR(jet, genjet));
59  if(dr < 0.3) {
60  matched = true;
61  break;
62  }
63  } // Loop over genjets
64  if(!matched) {
65  // cout<<endl<<endl<<"GENJETs"<<endl;
66  // for (size_t gjet(0); gjet < genjets->size(); gjet++) {
67  // const pat::Jet &genjet = (*genjets)[gjet];
68  // cout<<"("<<genjet.pt()<<", "<<genjet.eta()<<", "<<genjet.phi()<<")"<<endl;
69  // }
70  // cout<<"Jet unmatched ("<<jet.pt()<<", "<<jet.eta()<<", "<<jet.phi()<<")"<<endl;
71  return false;
72  }
73  } // Loop over jets
74 
75  return true;
76 }
77 
78 void event_tools::fillBeamHaloMap(string eventList){
79  cout<<"BABYMAKER::event_tools: Reading CSC Beam Halo filter file "<<eventList.c_str()<<endl;
80  ifstream file(eventList.c_str());
81  TString run_s, event_s;
82  int nlines(0), run, event;
83  while(file){
84  nlines++;
85  file >> run_s;
86  if(run_s.CountChar(':') < 2) break;
87  event_s = run_s;
88  run_s.Remove(run_s.Index(':'), run_s.Length());
89  event_s.Remove(0, event_s.Index(':')+1); event_s.Remove(0, event_s.Index(':')+1);
90  run = run_s.Atoi(); event = event_s.Atoi();
91  if(badBeamHaloEvents.find(run) == badBeamHaloEvents.end()) badBeamHaloEvents[run] = set<int>(); // New run
92  if(badBeamHaloEvents[run].find(event) == badBeamHaloEvents[run].end()) badBeamHaloEvents[run].insert(event);// New event
93  //cout<<"Pushed run "<<run<<" and event "<<event<<endl;
94  }
95 
96  for(map<int, set<int> >::const_iterator it = badBeamHaloEvents.begin(); it != badBeamHaloEvents.end(); ++it) {
97  run = it->first;
98  //cout << run << " "<<endl;
99  }
100 
101 }
102 
103 
104 int event_tools::type(const string &name){
105  int sample = -999, category = -9, bin = -99;
106  if(contains(name, "Run201")){ sample = 0;
107  if(contains(name, "SingleElectron")){ category = 0;
108  }else if(contains(name, "SingleMuon")){ category = 1;
109  }else if(contains(name, "DoubleEG")){ category = 2;
110  }else if(contains(name, "DoubleMuon")){ category = 3;
111  }else if(contains(name, "MET")){ category = 4;
112  }else if(contains(name, "HTMHT")){ category = 5;
113  }else if(contains(name, "JetHT")){ category = 6;
114  }
115  auto pos = name.find("Run201")+7;
116  if(pos < name.size()
117  && isalpha(name.at(pos))
118  && pos-1 < name.size()
119  && isdigit(name.at(pos-1))){
120  int run = toupper(name.at(pos))-65;
121  int year = name.at(pos-1)-48;
122  bin = (year-5)*26+run+1;
123  //2015A=1, ..., 2015D=4, ..., 2016A=27, 2016B=28, 2016C=29, ...
124  if(bin > 99) bin = 0;//Sanity check
125  }
126  }else if((contains(name, "TTJets") || contains(name, "TT_")) && !contains(name, "TTTT_")){ sample = 1;
127  if(contains(name, "TTJets_Tune")){ category = 0; bin = 0;
128  }else if(contains(name, "SingleLept")){ category = 1; bin = 0;
129  if(contains(name, "genMET-150")) bin = 1;
130  }else if(contains(name, "DiLept")){ category = 2; bin = 0;
131  if(contains(name, "genMET-150")) bin = 1;
132  }else if(contains(name, "TTJets_HT")){ category = 3;
133  if(contains(name, "HT-600to800")){ bin = 0;
134  }else if(contains(name, "HT-800to1200")){ bin = 1;
135  }else if(contains(name, "HT-1200to2500")){ bin = 2;
136  }else if(contains(name, "HT-2500toInf")){ bin = 3;
137  }
138  }else if(contains(name, "TT_")){ category = 4; bin = 0;
139  }else if(contains(name, "TTJets_Mtt")){ category = 5; bin = 0;
140  }
141  }else if(contains(name, "WJets") && !contains(name, "TTWJets")){ sample = 2;
142  if(contains(name, "WJetsToLNu_Tune")){ category = 0; bin = 0;
143  }else if(contains(name, "WJetsToLNu_HT")){ category = 1;
144  if(contains(name, "HT-70To100")){ bin = 0;
145  }else if(contains(name, "HT-100To200")){ bin = 1;
146  }else if(contains(name, "HT-200To400")){ bin = 2;
147  }else if(contains(name, "HT-400To600")){ bin = 3;
148  }else if(contains(name, "HT-600To800")){ bin = 4;
149  }else if(contains(name, "HT-800To1200")){ bin = 5;
150  }else if(contains(name, "HT-1200To2500")){ bin = 6;
151  }else if(contains(name, "HT-2500ToInf")){ bin = 7;
152  }else if(contains(name, "HT-600ToInf")){ bin = 10;
153  }
154  }else if(contains(name, "WJetsToQQ_HT")){ category = 2;
155  if(contains(name, "HT-600ToInf")){ bin = 0;
156  }
157  }
158  }else if(contains(name, "ST_")){ sample = 3;
159  if(contains(name, "ST_s-channel")){ category = 0; bin = 0;
160  }else if(contains(name, "ST_t-channel_top")){ category = 1; bin = 0;
161  }else if(contains(name, "ST_t-channel_antitop")){ category = 2; bin = 0;
162  }else if(contains(name, "ST_tW_top")){ category = 3; bin = 0;
163  }else if(contains(name, "ST_tW_antitop")){ category = 4; bin = 0;
164  }
165  }else if(contains(name, "TTWJets")){ sample = 4;
166  if(contains(name, "TTWJetsToLNu")){ category = 0; bin = 0;
167  }else if(contains(name, "TTWJetsToQQ")){ category = 1; bin = 0;
168  }
169  }else if(contains(name, "TTZ")){ sample = 5;
170  if(contains(name, "TTZToLLNuNu")){ category = 0; bin = 0;
171  }else if(contains(name, "TTZToQQ")){ category = 1; bin = 0;
172  }
173  }else if(contains(name, "DYJetsToLL")){ sample = 6;
174  if(contains(name, "DYJetsToLL_M-50_Tune") && contains(name, "madgraphMLM")){ category = 0; bin = 0;
175  }else if(contains(name, "DYJetsToLL_M-50_HT")){ category = 1;
176  if(contains(name, "HT-70to100")){ bin = 0;
177  }else if(contains(name, "HT-100to200")){ bin = 1;
178  }else if(contains(name, "HT-200to400")){ bin = 2;
179  }else if(contains(name, "HT-400to600")){ bin = 3;
180  }else if(contains(name, "HT-600to800")){ bin = 4;
181  }else if(contains(name, "HT-800to1200")){ bin = 5;
182  }else if(contains(name, "HT-1200to2500")){ bin = 6;
183  }else if(contains(name, "HT-2500toInf")){ bin = 7;
184  }else if(contains(name, "HT-600toInf")){ bin = 10;
185  }
186  } else if(contains(name, "DYJetsToLL") && contains(name, "amcatnloFXFX")){ category = 2;
187  if(contains(name, "M-50_Tune")){ bin = 0;
188  }else if(contains(name, "Pt-50To100")){ bin = 1;
189  }else if(contains(name, "Pt-100To250")){ bin = 2;
190  }else if(contains(name, "Pt-250To400")){ bin = 3;
191  }else if(contains(name, "Pt-400To650")){ bin = 4;
192  }else if(contains(name, "Pt-650ToInf")){ bin = 5;
193  }
194  }
195 
196  }else if(contains(name, "QCD")){ sample = 7;
197  if(contains(name, "QCD_HT")){ category = 0;
198  if(contains(name, "HT50to100")){ bin = 0;
199  }else if(contains(name, "HT100to200")){ bin = 1;
200  }else if(contains(name, "HT200to300")){ bin = 2;
201  }else if(contains(name, "HT300to500")){ bin = 3;
202  }else if(contains(name, "HT500to700")){ bin = 4;
203  }else if(contains(name, "HT700to1000")){ bin = 5;
204  }else if(contains(name, "HT1000to1500")){ bin = 6;
205  }else if(contains(name, "HT1500to2000")){ bin = 7;
206  }else if(contains(name, "HT2000toInf")){ bin = 8;
207  }
208  }else if(contains(name, "QCD_Pt")){ category = 1;
209  if(contains(name, "5to10")){ bin = 0;
210  }else if(contains(name, "10to15")){ bin = 1;
211  }else if(contains(name, "15to30")){ bin = 2;
212  }else if(contains(name, "30to50")){ bin = 3;
213  }else if(contains(name, "50to80")){ bin = 4;
214  }else if(contains(name, "80to120")){ bin = 5;
215  }else if(contains(name, "120to170")){ bin = 6;
216  }else if(contains(name, "170to300")){ bin = 7;
217  }else if(contains(name, "300to470")){ bin = 8;
218  }else if(contains(name, "470to600")){ bin = 9;
219  }else if(contains(name, "600to800")){ bin = 10;
220  }else if(contains(name, "800to1000")){ bin = 11;
221  }else if(contains(name, "1000to1400")){ bin = 12;
222  }else if(contains(name, "1400to1800")){ bin = 13;
223  }else if(contains(name, "1800to2400")){ bin = 14;
224  }else if(contains(name, "2400to3200")){ bin = 15;
225  }else if(contains(name, "3200toInf")){ bin = 16;
226  }
227  }
228  }else if(contains(name, "ZJets")){ sample = 8;
229  if(contains(name, "ZJetsToNuNu")){ category = 0;
230  if(contains(name, "HT-70To100")){ bin = 0;
231  }else if(contains(name, "HT-100To200")){ bin = 1;
232  }else if(contains(name, "HT-200To400")){ bin = 2;
233  }else if(contains(name, "HT-400To600")){ bin = 3;
234  }else if(contains(name, "HT-600To800")){ bin = 4;
235  }else if(contains(name, "HT-800To1200")){ bin = 5;
236  }else if(contains(name, "HT-1200To2500")){ bin = 6;
237  }else if(contains(name, "HT-2500ToInf")){ bin = 7;
238  }else if(contains(name, "HT-600ToInf")){ bin = 10;
239  }
240  }else if(contains(name, "ZJetsToQQ")){ category = 1;
241  if(contains(name, "HT600toInf")){ bin = 0;
242  }
243  }
244  }else if(contains(name, "ttH")){ sample = 9;
245  if(contains(name, "ttHJetTobb")){ category = 0; bin = 0;
246  }
247  }else if(contains(name, "TTGJets")){ sample = 10;
248  if(contains(name, "TTGJets_Tune")){ category = 0; bin = 0;
249  }
250  }else if(contains(name, "TTTT")){ sample = 11;
251  if(contains(name, "TTTT_Tune")){ category = 0; bin = 0;
252  }
253  }else if(contains(name, "WH_") && !contains(name,"TChiWH")){ sample = 12;
254  if(contains(name, "WH_HToBB_WToLNu")){ category = 0; bin = 0;
255  }
256  }else if(contains(name, "ZH_")){ sample = 13;
257  if(contains(name, "ZH_HToBB_ZToNuNu")){ category = 0; bin = 0;
258  }
259  }else if(contains(name, "WW") && !contains(name,"TChiHH")){ sample = 14;
260  if(contains(name, "WWToLNuQQ")){ category = 0; bin = 0;
261  }else if(contains(name, "WWTo2L2Nu")){ category = 1; bin = 0;
262  }
263  }else if(contains(name, "WZ") && !contains(name,"TChiHH")){ sample = 15;
264  if(contains(name, "WZTo1L3Nu")){ category = 0; bin = 0;
265  }else if(contains(name, "WZTo1L1Nu2Q")){ category = 1; bin = 0;
266  }else if(contains(name, "WZTo2L2Q")){ category = 2; bin = 0;
267  }else if(contains(name, "WZTo3LNu")){ category = 3; bin = 0;
268  }
269  }else if(contains(name, "ZZ") && !contains(name,"TChiHH")){ sample = 16;
270  if(contains(name, "ZZ_Tune")){ category = 0; bin = 0;
271  }
272  }else if(contains(name, "T1tttt")){ sample = 100; category = 0; bin = 0;
273  }else if(contains(name, "T2tt")){ sample = 101; category = 0; bin = 0;
274  }else if(contains(name, "T1bbbb")){ sample = 102; category = 0; bin = 0;
275  }else if(contains(name, "T2bb")){ sample = 103; category = 0; bin = 0;
276  }else if(contains(name, "T1qqqq")){ sample = 104; category = 0; bin = 0;
277  }else if(contains(name, "RPV")){ sample = 105; category = 0; bin = 0;
278  }else if(contains(name, "TChiHH")){ sample = 106; category = 0; bin = 0;
279  }else if(contains(name, "TChiWH")){ sample = 107; category = 0; bin = 0;
280  }
281 
282  if(sample < 0 || category < 0 || bin < 0
283  || category > 9 || bin > 99){
284  DBG("Could not find type code for " << name << ": sample=" << sample << ", category=" << category << ", bin=" << bin);
285  int code = -(1000*abs(sample)+100*abs(category)+abs(bin));
286  if(code >= 0) code = -99999;
287  return code;
288  }else{
289  int code = 1000*sample+100*category+bin;
290  if(code < 0 || code > 107000){
291  DBG("Type code out of range for " << name << ": sample=" << sample << ", category=" << category << ", bin=" << bin);
292  }
293  return code;
294  }
295 }
296 
298 
299  doBeamHalo = outname.Contains("Run2015");
300  if(doBeamHalo) {
301  string command("printf ${CMSSW_BASE}/src/babymaker/data/csc_beamhalo_filter/csc2015_ee4sc_Jan13.txt");
302  string eventList = execute(command.c_str());
303  fillBeamHaloMap(eventList);
304  }
305 }
306 // const std::vector<std::vector<int> > VRunLumi2015golden(MakeVRunLumi("golden"));
307 
309 }
bool passBeamHalo(int run, int event)
Definition: event_tools.cc:41
bool isInJSON(std::string type, int run, int lumiblock)
Definition: event_tools.cc:20
void fillBeamHaloMap(std::string eventList)
Definition: event_tools.cc:78
bool inJSON(std::vector< std::vector< int > > VRunLumi, int Run, int LS)
Definition: in_json.cc:75
STL namespace.
#define DBG(x)
Definition: utilities.hh:19
bool hasGoodPV(edm::Handle< reco::VertexCollection > vtx)
Definition: event_tools.cc:29
tuple run
Parsing run from file name.
bool contains(const std::string &s, const std::string &pat)
event_tools(TString outname)
Definition: event_tools.cc:297
bool passFSMET(edm::Handle< pat::JetCollection > alljets, edm::Handle< edm::View< reco::GenJet > > genjets)
Definition: event_tools.cc:50
static int type(const std::string &name)
Definition: event_tools.cc:104
std::string execute(const std::string &cmd)