ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/FGolf/tcmet/doAnalysis.C
Revision: 1.1
Committed: Sat Dec 5 02:59:03 2009 UTC (15 years, 5 months ago) by fgolf
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Error occurred while calculating annotation data.
Log Message:
looper for studying tcMET in CMSSW_3_1_2

File Contents

# Content
1 //now make the source file
2 #include <algorithm>
3 #include <iostream>
4 #include <fstream>
5 #include <vector>
6 #include "TChain.h"
7 #include "TChainElement.h"
8 #include "TFile.h"
9 #include "TH1.h"
10 #include "TH2.h"
11 #include "Math/LorentzVector.h"
12 #include "TMath.h"
13 #include "TStopwatch.h"
14 #include <algorithm>
15 #include <set>
16 #include "TCanvas.h"
17 #include "TRegexp.h"
18 #include "TLorentzVector.h"
19 #include "RooDataSet.h"
20 #include "RooRealVar.h"
21 #include "RooCategory.h"
22
23
24 using namespace std;
25
26 #ifndef __CINT__
27 #include "../CORE/CMS2.cc"
28 #include "../CORE/utilities.cc"
29 #include "../CORE/selections.cc"
30 #include "../Tools/tools.cc"
31 #endif
32
33
34 TH1F*hypos_total;
35 TH1F* hypos_total_weighted;
36
37 enum Sample {WW, WZ, ZZ, Wjets, DY, DYee, DYmm, DYtt, ttbar, tW, qcd, LM0x, LM1x, LM2x, LM3x, LM4x, LM5x, LM6x, LM7x, LM8x, LM9x}; // signal samples
38 enum Hypothesis {MM, EM, EE, ALL}; // hypothesis types (em and me counted as same) and all
39
40 // filter events by process
41 bool filterByProcess( enum Sample sample ) {
42 switch (sample) {
43 case DYee:
44 return isDYee();
45 case DYmm:
46 return isDYmm();
47 case DYtt:
48 return isDYtt();
49 case WW:
50 return isWW();
51 case WZ:
52 return isWZ();
53 case ZZ:
54 return isZZ();
55 default:
56 return true;
57 }
58 }
59
60 bool isIdentified( enum Sample sample ) {
61 switch (sample) {
62 case DYee:
63 case DYmm:
64 case DYtt:
65 return getDrellYanType()!=999;
66 case WW:
67 case WZ:
68 case ZZ:
69 return getVVType()!=999;
70 default:
71 return true;
72 }
73 }
74
75 // filter candidates by hypothesis
76 Hypothesis filterByHypothesis( int candidate ) {
77 switch (candidate) {
78 case 0:
79 return MM;
80 case 1: case 2:
81 return EM;
82 case 3:
83 return EE;
84 }
85 cout << "Unknown type: " << candidate << "Abort" << endl;
86 assert(0);
87 return MM;
88 }
89
90 // Book histograms...
91 // Naming Convention:
92 // Prefix comes from the sample and it is passed to the scanning function
93 // Suffix is "ee" "em" "em" "all" which depends on the final state
94 // For example: histogram named tt_hnJet_ee would be the Njet distribution
95 // for the ee final state in the ttbar sample.
96
97 // MAKE SURE TO CAL SUMW2 FOR EACH 1D HISTOGRAM BEFORE FILLING!!!!!!
98
99
100 TTree *outTree_ ;
101 // AY set up variables to put in flat ntuple
102
103 Int_t event_;
104 Float_t weight_;
105 Float_t tcMET_;
106 Float_t blMET_;
107
108
109 Float_t eta_ ;
110 Float_t phi_ ;
111 Float_t pt_ ;
112 Float_t pterr_ ;
113 Float_t d0_ ;
114 Float_t d0err_ ;
115 Float_t z0_ ;
116 Float_t z0err_ ;
117 Float_t chi2_ ;
118
119 Float_t etag_ ;
120 Float_t phig_ ;
121 Float_t ptg_ ;
122 Float_t d0g_ ;
123
124 Int_t algog_ ;
125 Int_t valdhg_ ;
126 Int_t highpg_ ;
127
128 Int_t algo_ ;
129 Int_t losth_ ;
130 Int_t valdh_ ;
131 Int_t qmask_ ;
132 Int_t ndof_ ;
133
134 Int_t loose_ ;
135 Int_t tight_ ;
136 Int_t highp_ ;
137
138
139 vector<ROOT::Math::LorentzVector<ROOT::Math::PxPyPzE4D<double> > > calo_jetsp4;
140
141 struct hypo_monitor{
142 std::vector<std::pair<std::string,unsigned int> > counters;
143 void count(unsigned int index, const char* name){
144 unsigned int current_size = counters.size();
145 for ( unsigned int i=current_size; i<=index; ++i )
146 counters.push_back( std::pair<std::string,unsigned int>("",0) );
147 counters[index].first = name;
148 counters[index].second++;
149 }
150 void print(){
151 for ( unsigned int i=0; i<counters.size(); ++i )
152 std::cout << counters[i].first << "\t" << counters[i].second << std::endl;
153 }
154 };
155
156 hypo_monitor monitor;
157
158 void hypo (int i_hyp, double kFactor, RooDataSet* dataset = 0)
159 {
160
161 unsigned int icounter(0);
162 monitor.count(icounter++,"Total number of hypothesis: ");
163
164 // Require Trigger:
165 // if ( ! passTriggersMu9orLisoE15( cms2.hyp_type()[i_hyp] ) ) return;
166 // if (! GoodSusyTrigger( cms2.hyp_type()[i_hyp] ) ) return;
167 monitor.count(icounter++,"Total number of hypothesis after trigger requirements: ");
168
169
170
171
172 monitor.count(icounter++,"Total number of hypothesis after adding jet cuts: ");
173
174 // if ( additionalZvetoSUSY09(i_hyp)) return;
175
176 monitor.count(icounter++,"Total number of hypothesis after adding additionalZvetoSUSY09 cut: ");
177
178
179 // -------------------------------------------------------------------//
180 // If we made it to here, we passed all cuts and we are ready to fill //
181 // -------------------------------------------------------------------//
182
183 // Fill the distribution
184
185
186
187 }//end of void hypo
188
189 RooDataSet* MakeNewDataset(const char* name)
190 {
191 RooRealVar set_iso("iso","iso",0.,1.);
192 RooRealVar set_event("event","event",0);
193 RooRealVar set_run("run","run",0);
194 RooRealVar set_lumi("lumi","lumi",0);
195 RooRealVar set_weight("weight","weight",0);
196 RooCategory set_selected("selected","Passed final WW selection requirements");
197 set_selected.defineType("true",1);
198 set_selected.defineType("false",0);
199
200 RooCategory set_hyp_type("hyp_type","Hypothesis type");
201 set_hyp_type.defineType("ee",0);
202 set_hyp_type.defineType("mm",1);
203 set_hyp_type.defineType("em",2);
204
205 RooCategory set_fake_type("fake_type","Define type of lepton for which isolation is extracted");
206 set_fake_type.defineType("electron",0);
207 set_fake_type.defineType("muon",1);
208
209 RooCategory set_sample_type("sample_type","Sample type");
210 set_sample_type.defineType("data_relaxed_iso",0); // full sample with final selection
211 set_sample_type.defineType("control_sample_signal_iso",1);
212
213 RooDataSet* dataset = new RooDataSet(name, name,
214 RooArgSet(set_event,set_run,set_lumi,
215 set_iso,set_selected,set_weight,
216 set_hyp_type,set_fake_type,set_sample_type) );
217 dataset->setWeightVar(set_weight);
218 return dataset;
219 }
220
221 void AddIsoSignalControlSample( int i_hyp, double kFactor, RooDataSet* dataset = 0 ) {
222 if ( !dataset ) return;
223 // The event weight including the kFactor (scaled to 1 fb-1)
224 float weight = cms2.evt_scale1fb() * kFactor;
225 // Cut on lepton Pt
226 if (cms2.hyp_lt_p4()[i_hyp].pt() < 20.0) return;
227 if (cms2.hyp_ll_p4()[i_hyp].pt() < 20.0) return;
228 // Require opposite sign
229 if ( cms2.hyp_lt_id()[i_hyp] * cms2.hyp_ll_id()[i_hyp] > 0 ) return;
230 // Z mass veto using hyp_leptons for ee and mumu final states
231 if ( cms2.hyp_type()[i_hyp] == 1 || cms2.hyp_type()[i_hyp] == 2 ) return;
232 if (! inZmassWindow(cms2.hyp_p4()[i_hyp].mass())) return;
233 RooArgSet set( *(dataset->get()) );
234 set.setCatIndex("selected",0);
235 set.setRealValue("event",cms2.evt_event());
236 set.setRealValue("run",cms2.evt_run());
237 set.setRealValue("lumi",cms2.evt_lumiBlock());
238 set.setCatLabel("sample_type","control_sample_signal_iso");
239
240 if ( cms2.hyp_type()[i_hyp] == 3 ){
241 set.setCatLabel("hyp_type","ee");
242 set.setCatLabel("fake_type","electron");
243 if ( goodElectronIsolated(cms2.hyp_lt_index()[i_hyp],true) &&
244 goodElectronWithoutIsolation(cms2.hyp_ll_index()[i_hyp]) ){
245 set.setRealValue("iso",el_rel_iso(cms2.hyp_ll_index()[i_hyp],true));
246 dataset->add(set,weight);
247 }
248 if ( goodElectronIsolated(cms2.hyp_ll_index()[i_hyp],true) &&
249 goodElectronWithoutIsolation(cms2.hyp_lt_index()[i_hyp]) ){
250 set.setRealValue("iso",el_rel_iso(cms2.hyp_lt_index()[i_hyp],true));
251 dataset->add(set,weight);
252 }
253 } else {
254 set.setCatLabel("hyp_type","mm");
255 set.setCatLabel("fake_type","muon");
256 if ( goodMuonIsolated(cms2.hyp_lt_index()[i_hyp]) &&
257 goodMuonWithoutIsolation(cms2.hyp_ll_index()[i_hyp]) ){
258 set.setRealValue("iso",mu_rel_iso(cms2.hyp_ll_index()[i_hyp]));
259 dataset->add(set,weight);
260 }
261 if ( goodMuonIsolated(cms2.hyp_ll_index()[i_hyp]) &&
262 goodMuonWithoutIsolation(cms2.hyp_lt_index()[i_hyp]) ){
263 set.setRealValue("iso",mu_rel_iso(cms2.hyp_lt_index()[i_hyp]));
264 dataset->add(set,weight);
265 }
266 }
267 }
268
269 RooDataSet* ScanChain( TChain* chain, enum Sample sample, bool identifyEvents ) {
270
271
272 unsigned int nEventsChain = chain->GetEntries(); // number of entries in chain --> number of events from all files
273 unsigned int nEventsTotal = 0;
274
275 // const unsigned int numHypTypes = 4; // number of hypotheses: MM, EM, EE, ALL
276
277 // declare and create array of histograms
278 // const char sample_names[][1024] = { "ww", "wz", "zz", "wjets", "dyee", "dymm", "dytt", "ttbar", "tw", "lm0x", "lm1x", "lm2x", "lm3x", "lm4x", "lm5x", "lm6x", "lm7x", "lm8x", "lm9x" };
279 const char sample_names[][1024] = { "ww", "wz", "zz", "wjets", "dy", "dyee", "dymm", "dytt", "ttbar", "tw", "qcd", "lm0x", "lm1x", "lm2x", "lm3x", "lm4x", "lm5x", "lm6x", "lm7x", "lm8x", "lm9x"};
280 const char *prefix = sample_names[sample];
281 RooDataSet* dataset = MakeNewDataset(sample_names[sample]);
282 double kFactor = 1; // 1fb-1
283
284 // double kFactor = .1; // 1fb-1
285 // switch (sample) {
286 // case WW:
287 // evt_scale1fb = 0.1538;
288 // break;
289 // default:
290 // break;
291
292 char *jetbins[5] = {"0", "1", "2", "3", "#geq 4"};
293 char *suffix[3];
294 suffix[0] = "ee";
295 suffix[1] = "mm";
296 suffix[2] = "em";
297 suffix[3] = "all";
298
299
300
301 // set up flat ntuple file:
302 std::string prefixStr = prefix;
303 std::string fileName = prefixStr + "_FlatTree.root";
304 TFile *outFile_ = new TFile(fileName.c_str(), "RECREATE");
305 outFile_->cd();
306
307 // set up tree
308 outTree_ = new TTree("T1", "Tree");
309
310 // book the branches
311 outTree_->Branch("event", &event_, "event/I");
312
313 outTree_->Branch("algo", &algo_, "algo/I");
314 outTree_->Branch("losth", &losth_, "losth/I");
315 outTree_->Branch("valdh", &valdh_, "valdh/I");
316 outTree_->Branch("qmask", &qmask_, "qmask/I");
317 outTree_->Branch("ndof", &ndof_, "ndof/I");
318
319 outTree_->Branch("qls", &loose_, "qls/I");
320 outTree_->Branch("qtt", &tight_, "qtt/I");
321 outTree_->Branch("qhp", &highp_, "qhp/I");
322
323 outTree_->Branch("wgt", &weight_, "wgt/F");
324 outTree_->Branch("tcmet", &tcMET_, "tcmet/F");
325 outTree_->Branch("blmet", &blMET_, "blmet/F");
326
327 outTree_->Branch("eta", &eta_, "eta/F");
328 outTree_->Branch("phi", &phi_, "phi/F");
329 outTree_->Branch("pt", &pt_, "pt/F");
330 outTree_->Branch("d0", &d0_, "d0/F");
331 outTree_->Branch("z0", &z0_, "z0/F");
332 outTree_->Branch("chi2", &chi2_, "chi2/F");
333 outTree_->Branch("d0err", &d0err_, "d0err/F");
334 outTree_->Branch("pterr", &pterr_, "pterr/F");
335 outTree_->Branch("z0err", &z0err_, "z0err/F");
336
337 outTree_->Branch("algog", &algog_, "algog/I");
338 outTree_->Branch("etag", &etag_, "etag/F");
339 outTree_->Branch("phig", &phig_, "phig/F");
340 outTree_->Branch("ptg", &ptg_, "ptg/F");
341 outTree_->Branch("d0g", &d0g_, "d0g/F");
342 outTree_->Branch("valdhg", &valdhg_, "valdhg/I");
343 outTree_->Branch("qhpg", &highpg_, "qhpg/I");
344
345
346 // The statement below should work but does not work due to bug in root when TH2 are also used
347 // Rene Brun promised a fix.
348 //TH1::SetDefaultSumw2(kTRUE); // do errors properly based on weights
349
350 // clear list of duplicates
351 already_seen.clear();
352 int duplicates_total_n = 0;
353 double duplicates_total_weight = 0;
354 int nFailedIdentification = 0;
355 int nFilteredOut = 0;
356 int i_permille_old = 0;
357 // file loop
358 TObjArray *listOfFiles = chain->GetListOfFiles();
359 TIter fileIter(listOfFiles);
360 monitor.counters.clear();
361
362 while (TChainElement *currentFile = (TChainElement*)fileIter.Next()) {
363 // need to call TFile::Open(), since the file is not
364 // necessarily a plain TFile (TNetFile, TDcacheFile, etc)
365 // printf("current file: %s (%s), %s\n", currentFile->GetName(),
366 // currentFile->GetTitle(), currentFile->IsA()->GetName());
367
368 TFile *f = TFile::Open(currentFile->GetTitle());
369 assert(f);
370 TTree *tree = (TTree*)f->Get("Events");
371
372 cms2.Init(tree); // set branch addresses for TTree tree
373
374 TStopwatch t;
375 //Event Loop
376
377 unsigned int nEvents = tree->GetEntries();
378 for( unsigned int event = 0; event < nEvents; ++event) {
379 cms2.GetEntry(event); // get entries for Event number event from branches of TTree tree
380 ++nEventsTotal;
381 // Loops
382 if (cms2.trks_d0().size() == 0)
383 continue;
384
385 DorkyEventIdentifier id(cms2);
386
387 if (is_duplicate(id)) {
388 duplicates_total_n++;
389 duplicates_total_weight += cms2.evt_scale1fb();
390 // cout << "Duplicate event found. Run: " << cms2.evt_run() << ", Event:" << cms2.evt_event() << ", Lumi: " << cms2.evt_lumiBlock() << endl;
391 continue;
392 }
393 // Jmu counter:
394
395 int i_permille = (int)floor(1000 * nEventsTotal / float(nEventsChain));
396 if (i_permille != i_permille_old) {
397 // xterm magic from L. Vacavant and A. Cerri
398 printf("\015\033[32m ---> \033[1m\033[31m%4.1f%%"
399 "\033[0m\033[32m <---\033[0m\015", i_permille/10.);
400 fflush(stdout);
401 i_permille_old = i_permille;
402 }
403
404 if ( identifyEvents ){
405 // check if we know what we are looking at
406 if ( ! isIdentified(sample) ) nFailedIdentification++;
407
408 // filter by process
409 if ( ! filterByProcess(sample) ) {
410 nFilteredOut++;
411 continue;
412 }
413 }
414
415 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
416 // CRAP
417 // if (event != 5159 ) continue;
418 //|| event != 29156 || event != 50028) continue;
419 // cout << "top of loop " << event << endl;
420 if (event > 100000) continue ;
421 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
422
423 // The event weight including the kFactor (scaled to 1 fb-1)
424 float weight = cms2.evt_scale1fb() * kFactor / 10; //AY set to a 100 picoBarn
425
426 // filling of flat ntuple entries...
427
428 for (int i = 0; i < cms2.trks_trk_p4().size(); ++i)
429 {
430
431 eta_ = cms2.trks_trk_p4()[i].Eta() ;
432 phi_ = cms2.trks_trk_p4()[i].Phi() ;
433 pt_ = cms2.trks_trk_p4()[i].Pt();
434 pterr_ = cms2.trks_ptErr()[i];
435 d0_ = cms2.trks_d0corr()[i] ;
436 d0err_ = cms2.trks_d0Err()[i] ;
437 z0_ = cms2.trks_z0()[i] ;
438 z0err_ = cms2.trks_z0Err()[i] ;
439 chi2_ = cms2.trks_chi2()[i] / cms2.trks_ndof()[i] ;
440
441
442 algo_ = cms2.trks_algo()[i]-4 ;
443 losth_ = cms2.trks_lostHits()[i] ;
444 valdh_ = cms2.trks_validHits()[i] ;
445 qmask_ = cms2.trks_qualityMask()[i] ;
446 ndof_ = cms2.trks_ndof()[i] ;
447 // qual cuts:
448 loose_ = isTrackQuality( i, (1<<loose) );
449 tight_ = isTrackQuality( i, (1<<tight) );
450 highp_ = isTrackQuality( i, (1<<highPurity) );
451
452 weight_ = weight;
453 event_ = event;
454 tcMET_ = cms2.evt_tcmet();
455 blMET_ = cms2.evt_metMuonJESCorr();
456
457
458
459 // look for gohst:
460 if (cms2.trks_trk_p4()[i].Pt() < 2.0) continue; // dont bother with tracks bellow 2 GeV
461 float detamin = 0.05;
462 float dphimin = 0.05 ;
463 int indtkmin = -1;
464 for (int j = i+1; j<cms2.trks_trk_p4().size() ; ++j)
465 {
466 float dpt = fabs(cms2.trks_trk_p4()[i].Pt() - cms2.trks_trk_p4()[j].Pt()) ;
467 float deta = fabs(cms2.trks_trk_p4()[i].Eta() - cms2.trks_trk_p4()[j].Eta()) ;
468 float dphi = acos(cos( cms2.trks_trk_p4()[i].Phi()- cms2.trks_trk_p4()[j].Phi())) ;
469 if ( dpt/cms2.trks_trk_p4()[i].Pt() > 0.2) continue; // dpt/pt<0.2
470 if (deta > detamin) continue;
471 if (dphi < dphimin) {
472 dphimin = dphi;
473 indtkmin = j;
474 /*
475 cout << " found a gohst track " << event << endl;
476 cout << " pt="<< cms2.trks_trk_p4()[i].Pt() << " deta " << deta << " dphi " << dphi << " dpt " << dpt << endl;
477 cout << "valid hits " << valdh_ << " gohst hits " << cms2.trks_validHits()[j] << endl ;
478 cout << "trk algo " << algo_ << " gohst algo " << cms2.trks_algo()[j]-4 << endl ;
479 */
480 }
481 }
482
483 if (indtkmin>=0) {
484 etag_ = cms2.trks_trk_p4()[indtkmin].Eta() ;
485 phig_ = cms2.trks_trk_p4()[indtkmin].Phi() ;
486 ptg_ = cms2.trks_trk_p4()[indtkmin].Pt();
487 algog_ = cms2.trks_algo()[indtkmin]-4 ;
488 valdhg_ = cms2.trks_validHits()[indtkmin] ;
489 d0g_ = cms2.trks_d0corr()[indtkmin] ;
490 highpg_ = isTrackQuality( indtkmin, (1<<highPurity) );
491 } else {
492 valdhg_ = 0;
493 etag_ = 0;
494 phig_ = 0;
495 ptg_ = 0;
496 }
497
498 // Fill Flat tree:
499 outTree_->Fill();
500 } // end of loopp over tracks
501
502
503 }//end event loop
504
505
506
507 }
508
509
510 monitor.print();
511 if ( nEventsChain != nEventsTotal ) {
512 printf("ERROR: number of events from files (%d) is not equal to total number"
513 " of events (%d)\n", nEventsChain, nEventsTotal);
514 }
515 /*
516 printf("Total number of skipped events due to bad identification: %d (%0.0f %%)\n",
517 nFailedIdentification, nFailedIdentification*100.0/(nEventsChain+1e-5));
518 printf("Total number of filtered out events: %d (%0.0f %%)\n",
519 nFilteredOut, nFilteredOut*100.0/(nEventsChain+1e-5));
520
521 printf("Total candidate count (ee mm em all): %.0f %.0f %.0f %0.f.\n",
522 hypos_total->GetBinContent(1), hypos_total->GetBinContent(2),
523 hypos_total->GetBinContent(3), hypos_total->GetBinContent(4));
524
525 printf("Total weighted candidate yield (ee mm em all): %f %f %f %f\n",
526 hypos_total_weighted->GetBinContent(1), hypos_total_weighted->GetBinContent(2),
527 hypos_total_weighted->GetBinContent(3), hypos_total_weighted->GetBinContent(4));
528 */
529 // Save tree
530 outFile_->cd();
531 outTree_->Write();
532 outFile_->Close();
533 delete outFile_;
534
535 // ofstream outf;
536 // outf.open("susy_eventcounts.txt", ios::app);
537 // outf<<"|" << prefix << "|" << nEventsTotal << endl;
538 // outf.close();
539
540 return dataset;
541 }
542