-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnaBase.cc
1390 lines (1267 loc) · 46.5 KB
/
AnaBase.cc
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "configana.h"
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <iterator>
#include <functional>
#include <numeric>
#include <string>
#include <climits>
#include <cassert>
#include <cstdlib>
#include <cmath>
#include <sstream>
#include "TROOT.h"
#include "TSystem.h"
#include "TChain.h"
#include "TChainElement.h"
#include "TClonesArray.h"
#include "TFile.h"
#include "TH1K.h"
#include "TH1.h"
#include "TH1F.h"
#include "TH2.h"
#include "TProfile.h"
#include "AnaBase.h"
#include "AnaUtil.h"
using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::vector;
using std::map;
using std::pair;
using std::abs;
using std::max;
using std::sqrt;
using std::sort;
using std::setprecision;
using std::setw;
using namespace vhtm;
// -----------
// Constructor
// -----------
AnaBase::AnaBase()
: chain_(new TChain("treeCreator/vhtree")),
histf_(0),
eventList_(new vector<vhtm::Event>()),
vertexList_(new vector<vhtm::Vertex>()),
genEventList_(new vector<vhtm::GenEvent>()),
tauList_(new vector<vhtm::Tau>()),
electronList_(new vector<vhtm::Electron>()),
muonList_(new vector<vhtm::Muon>()),
jetList_(new vector<vhtm::Jet>()),
metList_(new vector<vhtm::MET>()),
genParticleList_(new vector<vhtm::GenParticle>()),
genJetList_(new vector<vhtm::GenJet>()),
genMetList_(new vector<vhtm::GenMET>()),
triggerObjList_(new vector<vhtm::TriggerObject>()),
//trackList_(new vector<vhtm::Track>()),
l1physbits_(new vector<int>()),
l1techbits_(new vector<int>()),
hltpaths_(new vector<string>()),
hltresults_(new vector<int>()),
hltprescales_(new vector<int>()),
isMC_(false),
isSignal_(false),
readTrk_(false),
readTrigObject_(true),
logOption_(0),
useTrigger_(true),
usePUWt_(false),
histFile_("default.root"),
puHistFile_("./reweightFunctionFall11.root"),
useTrueNInt_(true),
logFile_("default.out"),
evFile_("default_event.out"),
maxEvt_(0)
{
cout << setiosflags(ios::fixed);
cout << "=== Start of Analysis === " << endl;
fileList_.clear();
brList_.clear();
puWtList_.clear();
trigPathList_.clear();
}
// ----------
// Destructor
// ----------
AnaBase::~AnaBase()
{
clearEvent();
delete eventList_;
delete vertexList_;
delete genEventList_;
delete tauList_;
delete electronList_;
delete muonList_;
delete jetList_;
delete metList_;
delete genParticleList_;
delete genJetList_;
delete genMetList_;
delete triggerObjList_;
//delete trackList_;
}
// ------------------------
// Clear the clones arrays
// ------------------------
void AnaBase::clearEvent()
{
if (eventList_) eventList_->clear();
if (genEventList_) genEventList_->clear();
if (vertexList_) vertexList_->clear();
if (tauList_) tauList_->clear();
if (electronList_) electronList_->clear();
if (muonList_) muonList_->clear();
if (jetList_) jetList_->clear();
if (metList_) metList_->clear();
if (genParticleList_) genParticleList_->clear();
if (genJetList_) genJetList_->clear();
if (genMetList_) genMetList_->clear();
if (triggerObjList_) triggerObjList_->clear();
//if (trackList_) trackList_->clear();
if (l1physbits_) l1physbits_->clear();
if (l1techbits_) l1techbits_->clear();
if (hltpaths_) hltpaths_->clear();
if (hltresults_) hltresults_->clear();
if (hltprescales_) hltprescales_->clear();
}
// -------------------------------------------------------
// Prepare for the run, do necessary initialisation etc.
// -------------------------------------------------------
bool AnaBase::beginJob()
{
if (isMC_ && usePUWt_ && !readPileUpHist()) return false;
// Open the output ROOT file
histf_ = TFile::Open(histFile_.c_str(), "RECREATE");
setAddresses();
nEvents_ = static_cast<int>(chain_->GetEntries());
if (nEvents_ <= 0) {
cerr << "******* nEvents = " << nEvents_ << ", returning!" << endl;
return false;
}
if (maxEvt_ > 0) nEvents_ = std::min(nEvents_, maxEvt_);
cout << " ===== # of events to analyse, nEvents = " << nEvents_ << endl;
openFiles();
return true;
}
// ------------------------------------
// Get Run number for the present event
// ------------------------------------
int AnaBase::getRunNumber() const
{
const Event& event = eventList_->at(0);
return event.run;
}
// ---------------------------------
// Add input Root files to the chain
// ---------------------------------
int AnaBase::setInputFile(const string& fname)
{
size_t found = fname.find("root:");
if (found == string::npos && gSystem->AccessPathName(fname.c_str())) {
cerr << ">>> Warning: File <<" << fname << ">> was not found!!" << endl;
return static_cast<int>(chain_->GetEntries());
}
chain_->AddFile(fname.c_str(), -1);
return static_cast<int>(chain_->GetEntries());
}
// ---------------------------------------
// Get total number of events in the chain
// --------------------------------------
int AnaBase::getEntries() const
{
return static_cast<int>(chain_->GetEntries());
}
// ------------------------------------------------------
// Open the output file with a global filehandle, C++ way
// ------------------------------------------------------
bool AnaBase::openFiles()
{
fLog_.open(logFile_.c_str(), ios::out);
if (!fLog_) {
cerr << "File: " << logFile_ << " could not be opened!" << endl;
return false;
}
fLog_ << setiosflags(ios::fixed);
evLog_.open(evFile_.c_str(), ios::out);
if (!evLog_) {
cerr << "File: " << evFile_ << " could not be opened!" << endl;
return false;
}
evLog_ << setiosflags(ios::fixed);
return true;
}
// ------------------------
// Close the output file
// ------------------------
void AnaBase::closeFiles()
{
if (fLog_) {
fLog_ << resetiosflags(ios::fixed);
fLog_.close();
}
if (evLog_) {
evLog_ << resetiosflags(ios::fixed);
evLog_.close();
}
}
void AnaBase::setAddresses()
{
if (branchFound("Event")) chain_->SetBranchAddress("Event", &eventList_);
if (branchFound("Vertex")) chain_->SetBranchAddress("Vertex", &vertexList_);
if (branchFound("Tau")) chain_->SetBranchAddress("Tau", &tauList_);
if (branchFound("Electron")) chain_->SetBranchAddress("Electron", &electronList_);
if (branchFound("Muon")) chain_->SetBranchAddress("Muon", &muonList_);
if (branchFound("Jet")) chain_->SetBranchAddress("Jet", &jetList_);
if (branchFound("MET")) chain_->SetBranchAddress("MET", &metList_);
if (readTrigObject_ && branchFound("TriggerObject"))
chain_->SetBranchAddress("TriggerObject", &triggerObjList_);
//if (readTrk_ && branchFound("Track"))
// chain_->SetBranchAddress("Track", &trackList_);
if (isMC_) {
if (branchFound("GenParticle")) chain_->SetBranchAddress("GenParticle", &genParticleList_);
if (branchFound("GenJet")) chain_->SetBranchAddress("GenJet", &genJetList_);
}
// Now the trigger variables
if (branchFound("l1physbits")) chain_->SetBranchAddress("l1physbits", &l1physbits_);
if (branchFound("l1techbits")) chain_->SetBranchAddress("l1techbits", &l1techbits_);
if (branchFound("hltresults")) chain_->SetBranchAddress("hltresults", &hltresults_);
if (branchFound("hltprescales")) chain_->SetBranchAddress("hltprescales", &hltprescales_);
if (branchFound("hltpaths")) chain_->SetBranchAddress("hltpaths", &hltpaths_);
}
bool AnaBase::branchFound(const string& b)
{
TBranch* branch = chain_->GetBranch(b.c_str()); // Get branch pointer
if (!branch) {
cout << ">>> SetBranchAddress: <" << b << "> not found!" << endl;
return false;
}
cout << ">>> SetBranchAddress: <" << b << "> found!" << endl;
brList_.push_back(b);
return true;
}
int AnaBase::getEntry(int lflag) const
{
int nbytes = 0;
for (auto it = brList_.begin(); it != brList_.end(); ++it) {
TBranch* branch = chain_->GetBranch((*it).c_str());
if (!branch) {
cout << ">>> Branch: " << (*it) << " not found!" << endl;
continue;
}
nbytes += branch->GetEntry(lflag);
}
return nbytes;
}
// not used yet
void AnaBase::enableBranches()
{
chain_->SetBranchStatus("*", kFALSE); // Disable all branches
for (auto it = brList_.begin(); it != brList_.end(); ++it) {
chain_->SetBranchStatus((*it).c_str(), kTRUE);
}
}
bool AnaBase::readJob(const string& jobFile, int& nFiles)
{
static const int BUF_SIZE = 256;
// Open the file containing the datacards
ifstream fin(jobFile.c_str(), ios::in);
if (!fin) {
cerr << "Input File: " << jobFile << " could not be opened!" << endl;
return false;
}
// note that you must use a pointer (reference!) to the cut map
// in order to avoid scope related issues
map<string, map<string, double>* > hmap;
hmap.insert(pair<string, map<string, double>* >("vtxCutList", &vtxCutMap_));
hmap.insert(pair<string, map<string, double>* >("electronCutList", &electronCutMap_));
hmap.insert(pair<string, map<string, double>* >("muonCutList", &muonCutMap_));
hmap.insert(pair<string, map<string, double>* >("tauCutList", &tauCutMap_));
hmap.insert(pair<string, map<string, double>* >("bjetCutList", &bjetCutMap_));
char buf[BUF_SIZE];
vector<string> tokens;
while (fin.getline(buf, BUF_SIZE, '\n')) { // Pops off the newline character
string line(buf);
if (line.empty() || line == "START") continue;
// enable '#' and '//' style comments
if (line.substr(0,1) == "#" || line.substr(0,2) == "//") continue;
if (line == "END") break;
// Split the line into words
AnaUtil::tokenize(line, tokens);
int vsize = tokens.size();
assert(vsize > 1);
string key = tokens.at(0);
string value = tokens.at(1);
if (key == "dataType") {
string vtmp(value);
std::transform(vtmp.begin(), vtmp.end(), vtmp.begin(), ::toupper);
vector<string> dt;
AnaUtil::tokenize(vtmp, dt, "#");
if (dt.size()) {
isMC_ = (dt.at(0) == "MC") ? true : false;
if (isMC_ && dt.size() > 1) {
isSignal_ = (dt.at(1) == "SIGNAL") ? true : false;
}
}
}
else if (key == "readTrk")
readTrk_ = (atoi(value.c_str()) > 0) ? true : false;
else if (key == "readTrigObject")
readTrigObject_ = (atoi(value.c_str()) > 0) ? true : false;
else if (key == "useTrigger")
useTrigger_ = (atoi(value.c_str()) > 0) ? true : false;
else if (key == "usePUWt")
usePUWt_ = (atoi(value.c_str()) > 0) ? true : false;
else if (key == "logFile")
logFile_ = value;
else if (key == "eventFile")
evFile_ = value;
else if (key == "logOption")
logOption_ = strtol(value.c_str(), NULL, 2);
else if (key == "maxEvent")
maxEvt_ = atoi(value.c_str());
else if (key == "histFile")
histFile_ = value;
else if (key == "puHistFile")
puHistFile_ = value;
else if (key == "useTrueNInt")
useTrueNInt_ = (atoi(value.c_str()) > 0) ? true : false;
else if (key == "trigPathList")
AnaUtil::buildList(tokens, trigPathList_);
else if (key == "inputFile")
AnaUtil::buildList(tokens, fileList_);
else if (key == "eventId" && tokens.size() == 4)
AnaUtil::buildMap(tokens, eventIdMap_);
else
AnaUtil::storeCuts(tokens, hmap);
tokens.clear();
}
if (!isMC_) usePUWt_ = false;
// Close the file
fin.close();
// Build the chain of root files
for (auto it = fileList_.begin(); it != fileList_.end(); ++it) {
string fname = *it;
cout << ">>> INFO. Adding input file " << fname << " to TChain " << endl;
++nFiles;
int nevt = setInputFile(fname.c_str());
if (maxEvt_ > 0 && nevt >= maxEvt_) break;
}
if (!nFiles) {
cerr << ">>> WARN. Input Root file list is empty! exiting ..." << endl;
return false;
}
useTCHE_ = (AnaUtil::cutValue(bjetCutMap_, "useTCHE") > 0) ? true : false;
return true;
}
void AnaBase::printJob(ostream& os) const
{
os << " datatype = " << ((isMC_) ? "mc" : "data") << endl
<< " logFile = " << logFile_ << endl
<< " eventFile = " << evFile_ << endl
<< " histFile = " << histFile_ << endl
<< " usePUWt = " << std::boolalpha << usePUWt_ << endl
<< " puHistFile = " << puHistFile_ << endl
<< " useTrueNInt = " << std::boolalpha << useTrueNInt_ << endl
<< " useTrigger = " << std::boolalpha << useTrigger_ << endl
<< " logOption = " << logOption_ << endl
<< " maxEvent = " << maxEvt_ << endl;
// Trigger Path List
if (useTrigger_)
AnaUtil::showList(trigPathList_, ">>> INFO. Trigger Paths used:", os);
// InputFiles
if (chain_) {
TObjArray *fileElements = chain_->GetListOfFiles();
os << ">>> INFO. nFiles: " << fileElements->GetEntries()
<< ", Files to analyse:"
<< endl;
TIter next(fileElements);
TChainElement *chEl = 0;
while (( chEl = dynamic_cast<TChainElement*>(next()) ))
os << chEl->GetTitle()
<< endl;
}
else
AnaUtil::showList(fileList_, ">>> INFO. inputFiles", os);
// EventID
AnaUtil::showMap<string, int>(eventIdMap_, "Event List:", os);
// Cuts
map<string, map<string, double> > hmap;
hmap.insert(pair<string, map<string, double> >("vtxCutList", vtxCutMap_));
hmap.insert(pair<string, map<string, double> >("electronCutList", electronCutMap_));
hmap.insert(pair<string, map<string, double> >("muonCutList", muonCutMap_));
hmap.insert(pair<string, map<string, double> >("tauCutList", tauCutMap_));
hmap.insert(pair<string, map<string, double> >("bjetCutList", bjetCutMap_));
AnaUtil::showCuts(hmap, os);
}
// Collect Object information
// Let's look at the event vertex
void AnaBase::findVtxInfo(vector<Vertex>& list, Options& op, ostream& os) {
if (nvertex() < 1) return;
if (op.verbose)
os << "=>> Vertices: " << nvertex() << endl
<< "indx ndf dxy z sumPt chi2 ntrks ntrkw05 sbit"
<< endl;
// already in correct order, no need to sort
int indx = 0;
for (auto it = vertexList_->begin(); it != vertexList_->end(); ++it,indx++) {
const Vertex& vtx = (*it);
double dxy = sqrt(pow(vtx.x, 2) + pow(vtx.y, 2));
int sbit = 0;
if (vtx.ndf <= AnaUtil::cutValue(vtxCutMap_, "ndf")) sbit |= (1 << 0);
if (dxy >= AnaUtil::cutValue(vtxCutMap_, "dxy")) sbit |= (1 << 1);
if (abs(vtx.z) >= AnaUtil::cutValue(vtxCutMap_, "z")) sbit |= (1 << 2);
if (op.verbose) {
bool pp = (op.printselected && sbit) ? false : true;
if (pp) {
os << setprecision(2)
<< setw(4) << indx
<< setw(8) << vtx.ndf
<< setw(8) << dxy
<< setw(8) << vtx.z
<< setw(8) << vtx.sumPt
<< setw(8) << vtx.chi2
//<< setw(8) << vtx.ntracks
<< setw(9) << vtx.ntracksw05;
AnaUtil::bit_print(sbit, 8, os);
}
}
if (op.usesbit && sbit) continue;
list.push_back(vtx);
}
}
// Let's look at the Muon collection
void AnaBase::findMuonInfo(vector<Muon>& list, double vz, Options& op, ostream& os) {
if (nmuon() < 1) return;
if (op.verbose)
os << "=>> Muons: " << nmuon() << endl
<< "indx Eta Phi Pt nChamber nMatch nStation"
<< " pixHits trkHits gChi2 D0 D0Err dB"
<< " vtxDistZ trkDz trkDzErr dz relIso sbit"
<< endl;
int indx = 0;
for (auto it = muonList_->begin(); it != muonList_->end(); ++it,indx++) {
const Muon& muon = (*it);
int sbit = 0;
if (abs(muon.eta) >= AnaUtil::cutValue(muonCutMap_, "eta")) sbit |= (1 << 0);
if (muon.pt < AnaUtil::cutValue(muonCutMap_, "pt")) sbit |= (1 << 1);
if (!muon.isTrackerMuon) sbit |= (1 << 2);
if (!muon.isGlobalMuonPromptTight) sbit |= (1 << 3);
if (muon.nChambers < AnaUtil::cutValue(muonCutMap_, "nChambers")) sbit |= (1 << 4);
if (muon.nMatches < AnaUtil::cutValue(muonCutMap_, "nMatches")) sbit |= (1 << 5);
if (muon.nMatchedStations < AnaUtil::cutValue(muonCutMap_, "nMatchedStations")) sbit |= (1 << 6);
if (muon.pixHits < AnaUtil::cutValue(muonCutMap_, "pixHits")) sbit |= (1 << 7);
if (muon.trkHits < AnaUtil::cutValue(muonCutMap_, "trkHits")) sbit |= (1 << 8);
if (muon.globalChi2 >= AnaUtil::cutValue(muonCutMap_,"globalChi2")) sbit |= (1 << 9);
if (abs(muon.trkD0) >= AnaUtil::cutValue(muonCutMap_,"trkD0")) sbit |= (1 << 10);
bool isGoodVtx;
TVector3 vmu = findLeptonVtx(muon.vtxIndex, isGoodVtx);
double dz = vmu.z() - vz;
//if (!isGoodVtx || abs(dz) >= AnaUtil::cutValue(muonCutMap_, "dz")) sbit |= (1 << 11);
//if (muon.relIso >= AnaUtil::cutValue(muonCutMap_, "pfRelIso")) sbit |= (1 << 12);
// not in baseline selection
#if 0
if (!muon.isAllArbitrated) sbit |= (1 << 13);
if (abs(muon.dB) >= AnaUtil::cutValue(muonCutMap_,"dB")) sbit |= (1 << 14);
if (abs(muon.vtxDistZ) >= AnaUtil::cutValue(muonCutMap_, "vtxDistZ")) sbit |= (1 << 15);
#endif
if (op.verbose) {
bool pp = (op.printselected && sbit) ? false : true;
if (pp) {
os << setprecision(2)
<< setw(4) << indx
<< setw(8) << muon.eta
<< setw(8) << muon.phi
<< setw(8) << muon.pt
<< setw(9) << muon.nChambers
<< setw(7) << muon.nMatches
<< setw(9) << muon.nMatchedStations
<< setprecision(1)
<< setw(8) << muon.pixHits
<< setw(8) << muon.trkHits
<< setw(8) << muon.globalChi2
<< setprecision(3)
<< setw(8) << muon.trkD0
//<< setw(8) << muon.trkD0Error
<< setw(8) << muon.dB
<< setw(9) << muon.vtxDistZ
<< setw(8) << muon.trkDz
//<< setw(9) << muon.trkDzError
<< setw(9) << dz;
//<< setw(9) << muon.relIso;
AnaUtil::bit_print(sbit, 15, os);
}
}
// Now apply cuts
if (op.usesbit && sbit) continue;
list.push_back(muon);
}
if (list.size() > 1)
sort(list.begin(), list.end(), PtComparator<Muon>());
}
// Let's look at the Electron collection now
void AnaBase::findElectronInfo(vector<Electron>& list, double vz, Options& op, ostream& os) {
if (nelectron() < 1) return;
if (op.verbose)
os << "=>> Electrons: " << nelectron() << endl
<< "indx Eta Phi Pt Energy"
<< " scEta dB eleId dz sbit"
<< endl;
int indx = 0;
for (auto it = electronList_->begin(); it != electronList_->end(); ++it,indx++) {
const Electron& elec = (*it);
bool quality_EB_loose = (abs(elec.eta) <= 1.4442
&& elec.sigmaEtaEta < 0.01
&& elec.deltaEtaTrkSC < 0.007
&& elec.deltaPhiTrkSC < 0.8
&& elec.hoe < 0.15);
bool quality_EE_loose = (abs(elec.eta) >= 1.566
&& elec.sigmaEtaEta < 0.03
&& elec.deltaEtaTrkSC < 0.01
&& elec.deltaPhiTrkSC < 0.7
&& elec.hoe < 0.07);
bool quality_loose = quality_EB_loose || quality_EE_loose;
int sbit = 0;
if (elec.pt <= AnaUtil::cutValue(electronCutMap_, "pt")) sbit |= (1 << 0);
if (abs(elec.eta) >= AnaUtil::cutValue(electronCutMap_, "eta")) sbit |= (1 << 1);
if (!quality_loose) sbit |= (1 << 2);
if (elec.missingHits > AnaUtil::cutValue(electronCutMap_, "missingHits")) sbit |= (1 << 3);
//if (elec.simpleEleId95cIso <= AnaUtil::cutValue(electronCutMap_, "eleId")) sbit |= (1 << 2);
//if (!elec.hasGsfTrack) sbit |= (1 << 3);
if (abs(elec.dB) >= AnaUtil::cutValue(electronCutMap_, "dB")) sbit |= (1 << 4);
if ( abs(elec.scEta) >= AnaUtil::cutValue(electronCutMap_, "scEtaLow")
&& abs(elec.scEta) <= AnaUtil::cutValue(electronCutMap_, "scEtaUp")) sbit |= (1 << 5);
bool isGoodVtx;
TVector3 vele = findLeptonVtx(elec.vtxIndex, isGoodVtx);
double dz = (isGoodVtx) ? (vele.z() - vz) : 999;
if (abs(dz) >= AnaUtil::cutValue(electronCutMap_, "dz")) sbit |= (1 << 6);
if (op.verbose) {
bool pp = (op.printselected && sbit) ? false : true;
if (pp) {
os << setprecision(2)
<< setw(4) << indx
<< setw(8) << elec.eta
<< setw(8) << elec.phi
<< setw(8) << elec.pt
<< setw(8) << elec.energy
<< setw(8) << elec.scEta
<< setw(8) << elec.dB
<< setw(8) << dz;
AnaUtil::bit_print(sbit, 8, os);
}
}
// Now apply cuts
if (op.usesbit && sbit) continue;
list.push_back(elec);
}
if (list.size() > 1)
sort(list.begin(), list.end(), PtComparator<Electron>());
}
// Let's look at the Tau collection
void AnaBase::findTauInfo(vector<Tau>& list, double vz, Options& op, ostream& os) {
if (ntau() < 1) return;
if (op.verbose)
os << "=>> Taus: " << ntau() << endl
<< "indx Eta Phi Pt dz"
<< " DMF LI MI aMT aEL aEM eMVA sbit"
<< endl;
int indx = 0;
for (auto it = tauList_->begin(); it != tauList_->end(); ++it,indx++) {
const Tau& tau = (*it);
// pre-selection
int sbit = 0;
if (abs(tau.eta) >= AnaUtil::cutValue(tauCutMap_, "eta")) sbit |= (1 << 0);
if (tau.pt < AnaUtil::cutValue(tauCutMap_, "pt")) sbit |= (1 << 1);
double dz = tau.zvertex - vz;
if (vz != -999 && abs(dz) >= AnaUtil::cutValue(tauCutMap_, "dz")) sbit |= (1 << 2);
if (tau.decayModeFinding <= 0.5) sbit |= (1 << 3);
if (tau.chargedIsoPtSum <= 2.0) sbit |= (1 << 4);
if (tau.againstMuonTight <= AnaUtil::cutValue(tauCutMap_, "muVeto")) sbit |= (1 << 5);
if (tau.againstElectronLoose <= AnaUtil::cutValue(tauCutMap_, "eleVeto")) sbit |= (1 << 6);
if (op.verbose) {
bool pp = (op.printselected && sbit) ? false : true;
if (pp) {
os << setprecision(2)
<< setw(4) << indx
<< setw(8) << tau.eta
<< setw(8) << tau.phi
<< setw(8) << tau.pt
<< setw(8) << dz
<< setprecision(1)
<< setw(4) << tau.decayModeFinding
//<< setw(4) << tau.byLooseCombinedIsolationDeltaBetaCorr
//<< setw(4) << tau.byMediumCombinedIsolationDeltaBetaCorr
<< setw(4) << tau.againstMuonTight
<< setw(4) << tau.againstElectronLoose
<< setw(4) << tau.againstElectronMedium
<< setw(5) << tau.againstElectronLooseMVA5;
AnaUtil::bit_print(sbit, 8, os);
}
}
if (op.usesbit && sbit) continue;
list.push_back(tau);
}
if (list.size() > 1)
sort(list.begin(), list.end(), PtComparator<Tau>());
}
// Let's look at the Jet collection
void AnaBase::findJetInfo(vector<Jet>& list, Options& op, ostream& os) {
if (njet() < 1) return;
if (op.verbose)
os << "=>> Jets: " << njet() << endl
<< "indx Eta Phi Pt Energy"
<< " TCHE TCHP SVHE SVHP JPBTag JBPBTag sbit"
<< endl;
int indx = 0;
for (auto it = jetList_->begin(); it != jetList_->end(); ++it,indx++) {
const Jet& jt = (*it);
int sbit = 0;
if (abs(jt.eta) >= AnaUtil::cutValue(bjetCutMap_, "eta")) sbit |= (1 << 0);
if (jt.pt <= AnaUtil::cutValue(bjetCutMap_, "pt")) sbit |= (1 << 1);
if (useTCHE_) {
//if (jt.trackCountingHighEffBTag <= AnaUtil::cutValue(bjetCutMap_, "TCHECut")) sbit |= (1 << 2);
}
else {
if (jt.combinedSecondaryVertexBTag <= AnaUtil::cutValue(bjetCutMap_, "CSVCut")) sbit |= (1 << 2);
}
if (op.verbose) {
bool pp = (op.printselected && sbit) ? false : true;
if (pp) {
os << setprecision(2)
<< setw(4) << indx
<< setw(8) << jt.eta
<< setw(8) << jt.phi
<< setw(8) << jt.pt
<< setw(8) << jt.energy;
//<< setprecision(1)
//<< setw(8) << jt.trackCountingHighEffBTag
//<< setw(8) << jt.trackCountingHighPurBTag
//<< setw(8) << jt.simpleSecondaryVertexHighEffBTag
//<< setw(8) << jt.simpleSecondaryVertexHighPurBTag
//<< setw(8) << jt.jetProbabilityBTag
//<< setw(8) << jt.jetBProbabilityBTag;
AnaUtil::bit_print(sbit, 8, os);
}
}
// Now apply cuts
if (op.usesbit && sbit) continue;
list.push_back(jt);
}
if (list.size() > 1)
sort(list.begin(), list.end(), PtComparator<Jet>());
}
void AnaBase::findTriggerObjectInfo(vector<TriggerObject>& list) {
for (auto it = triggerObjList_->begin(); it != triggerObjList_->end(); ++it) {
list.push_back(*it);
}
}
TVector3 AnaBase::findLeptonVtx(int index, bool& isGoodVtx) {
const Vertex& vtx = vertexList_->at(index);
isGoodVtx = false;
double dxy = sqrt(pow(vtx.x, 2) + pow(vtx.y, 2));
if (vtx.ndf > AnaUtil::cutValue(vtxCutMap_, "ndf") &&
dxy < AnaUtil::cutValue(vtxCutMap_, "dxy") &&
abs(vtx.z) < AnaUtil::cutValue(vtxCutMap_, "z")) isGoodVtx = true;
TVector3 v(vtx.x, vtx.y, vtx.z);
return v;
}
void AnaBase::dumpEvent(const char* optstr, ostream& os, bool ps) {
// Dump original content present in the tree
// Event
const Event& ev = eventList_->at(0);
os << "Event " << ev.event
<< " Lumis " << ev.lumis
<< " Run " << ev.run
<< endl;
int logOption = strtol(optstr, NULL, 2);
// Options common for all the objects
Options options;
options.usesbit = false;
options.printselected = ps;
vector<Vertex> vList;
options.verbose = (logOption & 0x1) ? true : false;
findVtxInfo(vList, options, os);
double vz = (vList.size() > 0) ? vList.at(0).z : -999;
vector<Tau> tList;
options.verbose = (logOption >> 1 & 0x1) ? true : false;
findTauInfo(tList, vz, options, os);
vector<Muon> mList;
options.verbose = (logOption >> 2 & 0x1) ? true : false;
findMuonInfo(mList, vz, options, os);
vector<Electron> eList;
options.verbose = (logOption >> 3 & 0x1) ? true : false;
findElectronInfo(eList, vz, options, os);
vector<Jet> bList;
options.verbose = (logOption >> 4 & 0x1) ? true : false;
findJetInfo(bList, options, os);
if (isMC_ && (logOption >> 5 & 0x1)) dumpGenInfo(os);
}
void AnaBase::findGenInfo(int leptonId, vector<GenParticle>& genLepList, vector<GenParticle>& genTauList) {
// Generator level information
if (!ngenparticle()) return;
for (auto jt = genParticleList_->begin(); jt != genParticleList_->end(); ++jt) {
const GenParticle& gp = (*jt);
int pdgid = fabs(gp.pdgId);
if (pdgid != fabs(leptonId) && pdgid != 15) continue;
int status = gp.status;
// looking for a Hadronically decaying Tau whose mother is Higgs
if (pdgid == 15 && status == 2) {
int mmid = -1;
int index = getMotherId(gp, mmid);
if (index < 0 || fabs(mmid) != 25) continue; // Assert Higgs
vector<int> d = gp.daughtIndices;
int ndau = 0;
for (auto it = d.begin(); it != d.end(); ++it) {
int di = (*it);
if (di >= ngenparticle()) continue;
const GenParticle& dgp = genParticleList_->at(di);
int pid = fabs(dgp.pdgId);
if (pid == 16) continue;
//if (pid != 11 && pid != 13 && pid != 12 && pid != 14 && pid != 22) ++ndau;
if (pid > 100 || pid == 24) ++ndau;
}
if (ndau) genTauList.push_back(gp);
}
// Looking for Lep whose mother is W boson
else if (pdgid == fabs(leptonId) && status == 1) {
int mmid = -1;
int index = getMotherId(gp, mmid);
if (index >= 0 && fabs(mmid) == 24) {
const GenParticle& mgp = genParticleList_->at(index);
int jmid = -1;
int jndex = getMotherId(mgp, jmid);
if (jndex >= 0 && fabs(jmid) != 6) genLepList.push_back(gp); // ensure W does not come from t
}
}
}
if (genLepList.size() > 1)
sort(genLepList.begin(), genLepList.end(), PtComparator<GenParticle>());
if (genTauList.size() > 1)
sort(genTauList.begin(), genTauList.end(), PtComparator<GenParticle>());
}
void AnaBase::findLLTGenInfo( vector<GenParticle>& genMuList, vector<GenParticle>& genElList,
vector<GenParticle>& genTauList){
emt = false;
eet = false;
mmt = false;
// Generator level information
if (!ngenparticle()) return;
for (auto jt = genParticleList_->begin(); jt != genParticleList_->end(); ++jt) {
const GenParticle& gp = (*jt);
int pdgid = fabs(gp.pdgId);
if (pdgid != 11 && pdgid != 15 && pdgid != 13) continue;
int status = gp.status;
// looking for a Hadronically decaying Tau whose mother is Higgs
if (pdgid == 15 && status == 2) {
int mmid = -1;
int index = getMotherId(gp, mmid);
if (index < 0 || fabs(mmid) != 25) continue; // Assert Higgs
vector<int> d = gp.daughtIndices;
int ndau = 0;
for (auto it = d.begin(); it != d.end(); ++it) {
int di = (*it);
if (di >= ngenparticle()) continue;
const GenParticle& dgp = genParticleList_->at(di);
int pid = fabs(dgp.pdgId);
if (pid == 16) continue;
//if (pid != 11 && pid != 13 && pid != 12 && pid != 14 && pid != 22) ++ndau;
if (pid > 100 || pid == 24) ++ndau;
}
if (ndau) genTauList.push_back(gp);
}
//looking for lepton
if ((pdgid == 11 || pdgid == 13) && status == 1) {
int mmid = -1;
int index = getMotherId(gp, mmid);
if(index < 0 || (fabs(mmid) != 15 && fabs(mmid) != 24)) continue;
const GenParticle& mgp = genParticleList_->at(index);
int jmid = -1;
int jndex = getMotherId(mgp, jmid);
if(jndex <0 ) continue;
if(fabs(mmid) ==15 && fabs(jmid) == 25){
if(pdgid == 11) genElList.push_back(gp);
if(pdgid == 13) genMuList.push_back(gp);
}
else if(jndex >= 0 && fabs(jmid) != 6 && fabs(mmid)!=15){ // ensure W does not come from t
if(pdgid == 11) genElList.push_back(gp);
if(pdgid == 13) genMuList.push_back(gp);
}
}
}
if(genTauList.size() < 1) return;
if(genElList.size() == 1 && genMuList.size() == 1)
emt = true;
if(genElList.size() == 2 && genMuList.size() == 0)
eet = true;
if(genElList.size() == 0 && genMuList.size() == 2)
mmt = true;
}
int AnaBase::getMotherId(const GenParticle& gp, int& mmid) const {
int pdgid = gp.pdgId;
vector<int> m = gp.motherIndices;
if (m.size() < 1) return -1;
int indx = m.at(0);
GenParticle& mgp = genParticleList_->at(indx);
mmid = mgp.pdgId;
while (mmid == pdgid) {
m = mgp.motherIndices;
indx = m.at(0);
mgp = genParticleList_->at(indx);
mmid = mgp.pdgId;
}
return indx;
}
void AnaBase::dumpGenInfo(ostream& os) const {
if (!ngenparticle()) return;
os << setprecision(2);
os << "indx status pdgId eta phi pt energy moIndx"
<< " moID daughterID"
<< endl;
int indx = 0;
for (auto jt = genParticleList_->begin(); jt != genParticleList_->end(); ++jt,indx++) {
const GenParticle& gp = (*jt);
std::ostringstream mID;
vector<int> m = gp.motherIndices;
for (auto it = m.begin(); it != m.end(); ++it) {
int mi = (*it);
if (mi >= ngenparticle()) continue;
const GenParticle& mgp = genParticleList_->at(mi);
mID << " " << mgp.pdgId;
}
string ms = mID.str();
if (!ms.length()) ms = " -";
std::ostringstream dID;
vector<int> d = gp.daughtIndices;
for (auto it = d.begin(); it != d.end(); ++it) {
int di = (*it);
if (di >= ngenparticle()) continue;
const GenParticle& dgp = genParticleList_->at(di);
double energy = dgp.energy;
int pdgid = dgp.pdgId;
if (abs(pdgid) == 21 && energy <= 10) continue;
dID << " " << dgp.pdgId;
}
string ds = dID.str();
if (!ds.length()) ds = " -";
os << setw(4) << indx
<< setw(8) << gp.status
<< setw(10) << gp.pdgId
<< setw(10) << gp.eta
<< setw(9) << gp.phi
<< setw(9) << gp.pt
<< setw(9) << gp.energy
<< setw(8) << gp.motherIndex
<< setw(10) << ms
<< ds
<< endl;
}
}
bool AnaBase::readPileUpHist() {
size_t found = puHistFile_.find(".root");
if (found == string::npos) {
cerr << ">>> Warning: <<" << puHistFile_ << ">> does not have .root extension!!" << endl;
return false;
}
const char* fname = gSystem->ExpandPathName(puHistFile_.c_str());
if (gSystem->AccessPathName(fname)) {
cerr << ">>> Warning: File <<" << puHistFile_ << ">> was not found!!" << endl;
return false;
}
TFile file(fname);
TH1D *h = dynamic_cast<TH1D*>(file.Get("plot_data_div_MC"));
int nx = h->GetXaxis()->GetNbins();
for (int i = 0; i < nx; ++i) {
double wt = h->GetBinContent(i);
puWtList_.push_back(wt);
}
return true;
}
double AnaBase::wtPileUp(int& nPU) const {
nPU = 0;
const Event& evt = eventList_->at(0);
auto list = (useTrueNInt_) ? evt.trueNInt : evt.nPU;
if (!list.size()) return 1.0;
int nbins = puWtList_.size();
nPU = list.at(0);
if (nPU < 0) nPU = 0;
if (nPU >= nbins) nPU = nbins - 1;
return puWtList_.at(nPU);
}
bool AnaBase::isTriggered(bool check_prescale, bool verbose) const {
bool flag = false;
for (size_t i = 0; i < hltpaths_->size(); ++i) {
string str = (*hltpaths_).at(i);
bool found = false;
for (auto it = trigPathList_.begin(); it != trigPathList_.end(); ++it) {
if (str.find(*it) != string::npos) {
found = true;
break;
}
}
if (!found) continue;
int prescl = (check_prescale) ? (*hltprescales_).at(i) : 1;
int result = (*hltresults_).at(i);
if (verbose) cout << ">>> HLT Path = " << str
<< ", fired=" << result
<< ", prescale=" << prescl
<< endl;
if (result == 1 && prescl == 1) {
flag = true;
break;
}
}
return flag;
}
void AnaBase::dumpTriggerPaths(ostream& os, bool check_prescale) const