checkmate is hosted by Hepforge, IPPP Durham
AnalysisBase.h
Go to the documentation of this file.
1 #ifndef _ANALYSISBASE
2 #define _ANALYSISBASE
3 
4 #include <iostream>
5 #include <fstream>
6 #include <stdio.h>
7 #include <map>
8 #include <math.h>
9 
10 #include <TCanvas.h>
11 #include <TChain.h>
12 #include <TClonesArray.h>
13 #include <TH1.h>
14 #include <TObject.h>
15 #include <TROOT.h>
16 #include <TStyle.h>
17 #include <TSystem.h>
18 
19 #include "classes/DelphesClasses.h"
20 #include "external/ExRootAnalysis/ExRootTreeReader.h"
21 #include "external/ExRootAnalysis/ExRootResult.h"
22 
23 #include "ETMiss.h"
24 #include "FinalStateObject.h"
25 #include "Units.h"
26 
27 #include "mt2_bisect.h"
28 #include "mctlib.h"
29 #include "mt2bl_bisect.h"
30 
42 
47 class AnalysisBase {
48  public:
50 
54  AnalysisBase(std::string inFile, std::string outFol, std::string outPre, double xs, double xserr, std::map<std::string, std::string> branches, std::map<std::string, std::vector<int> > flags);
56  ~AnalysisBase();
58 
65  void loopOverEvents();
66 
67  protected:
69 
75 
80  virtual void initialize() {};
82  virtual void analyze() {};
84  virtual void finalize() {};
88 
101  std::vector<Electron*> electrons;
102  std::vector<Electron*> electronsLoose;
103  std::vector<Electron*> electronsMedium;
104  std::vector<Electron*> electronsTight;
105  std::vector<Muon*> muons;
106  std::vector<Muon*> muonsCombinedPlus;
107  std::vector<Muon*> muonsCombined;
108  std::vector<Jet*> jets;
109  std::vector<Jet*> jets2;
110  std::vector<Photon*> photons;
111  std::vector<Track*> tracks;
112  std::vector<Tower*> towers;
114 
117 
126 
136  template <class T>
137  std::vector<T*> filterPhaseSpace(std::vector<T*> unfiltered, double pTmin = 0., double etamin = -100, double etamax = 100, bool exclude_overlap = false) {
138  std::vector<T*> filtered;
139  for (int i = 0; i < unfiltered.size(); i++) {
140  T* cand = unfiltered[i];
141  if((cand->PT > pTmin) && (cand->Eta > etamin) && (cand->Eta < etamax)) {
142  if(!exclude_overlap)
143  filtered.push_back(cand);
144  else if( (fabs(cand->Eta) < 1.37) || (fabs(cand->Eta) > 1.52) )
145  filtered.push_back(cand);
146  }
147  }
148  return filtered;
149  }
150 
152 
160  template <class X, class Y>
161  std::vector<X*> overlapRemoval(std::vector<X*> candidates, std::vector<Y*> neighbours, double dR) {
162  // If neighbours are empty, return candidates
163  if(neighbours.size() == 0)
164  return candidates;
165  std::vector<X*> passed_candidates;
166  // Loop over candidates
167  for(int i = 0; i < candidates.size(); i++) {
168  bool overlap = false;
169  // If a neighbour is too close, declare overlap, break and don't save candidate
170  for(int j = 0; j < neighbours.size(); j++) {
171  if (candidates[i]->P4().DeltaR(neighbours[j]->P4()) < dR) {
172  overlap = true;
173  break;
174  }
175  }
176  if (!overlap)
177  passed_candidates.push_back(candidates[i]);
178  }
179  return passed_candidates;
180  }
181 
183 
190  template <class X>
191  std::vector<X*> overlapRemoval(std::vector<X*> candidates, double dR) {
192  // Same as above for the special case that candidates = neighbours. In that case, the removal
193  // can be formulated more effectively as the sum only has to run half as many times
194  if(candidates.size() == 0)
195  return candidates;
196  std::vector<X*> passed_candidates;
197  // Loop over candidates
198  for(int i = 0; i < candidates.size(); i++) {
199  bool overlap = false;
200  // If one of the other, still untested, candidates is too close: remove
201  // Since the list is order w.r.t pt, this will always remove the softer object
202  for(int j = 0; j < i; j++) {
203  if (candidates[i]->P4().DeltaR(candidates[j]->P4()) < dR) {
204  overlap = true;
205  break;
206  }
207  }
208  if (!overlap)
209  passed_candidates.push_back(candidates[i]);
210  }
211  return passed_candidates;
212  }
213 
215 
227  std::vector<Electron*> filterIsolation(std::vector<Electron*> unfiltered, std::vector<int> relative_flags = std::vector<int>()) {
228  // Translate the relative isolation number of the analysis in the absolute number within all analyses
229  std::vector<int> absolute_flags;
230  for(int i = 0; i < relative_flags.size(); i++) {
231  if (relative_flags[i] >= electronIsolationFlags.size()) {
232  std::cerr << "Error: There is no electron isolation " << relative_flags[i] << std::endl;
233  std::cerr << "Exiting... "<< std::endl;
234  exit(1);
235  }
236  absolute_flags.push_back(electronIsolationFlags[relative_flags[i]]);
237  }
238  // if no flags are given, use all
239  if (absolute_flags.size() == 0)
240  absolute_flags = electronIsolationFlags;
241 
242  return filterFlags(unfiltered, "isolation", absolute_flags);
243  }
244 
246  std::vector<Electron*> filterIsolation(std::vector<Electron*> unfiltered, int relative_flag) {
247  std::vector<int> absolute_flags;
248  if (relative_flag >= electronIsolationFlags.size()) {
249  std::cerr << "Error: There is no electron isolation " << relative_flag << std::endl;
250  std::cerr << "Exiting... "<< std::endl;
251  exit(1);
252  }
253  absolute_flags.push_back(electronIsolationFlags[relative_flag]);
254  return filterFlags(unfiltered, "isolation", absolute_flags);
255  }
256 
258  std::vector<Muon*> filterIsolation(std::vector<Muon*> unfiltered, std::vector<int> relative_flags = std::vector<int>()) {
259  // Translate the relative isolation number of the analysis in the absolute number within all analyses
260  std::vector<int> absolute_flags;
261  for(int i = 0; i < relative_flags.size(); i++) {
262  if (relative_flags[i] >= muonIsolationFlags.size()) {
263  std::cerr << "Error: There is no muon isolation " << relative_flags[i] << std::endl;
264  std::cerr << "Exiting... "<< std::endl;
265  exit(1);
266  }
267  absolute_flags.push_back(muonIsolationFlags[relative_flags[i]]);
268  }
269  // if no flags are given, use all
270  if (absolute_flags.size() == 0)
271  absolute_flags = muonIsolationFlags;
272 
273  return filterFlags(unfiltered, "isolation", absolute_flags);
274  }
275 
277  std::vector<Muon*> filterIsolation(std::vector<Muon*> unfiltered, int relative_flag) {
278  std::vector<int> absolute_flags;
279  if (relative_flag >= muonIsolationFlags.size()) {
280  std::cerr << "Error: There is no muon isolation " << relative_flag << std::endl;
281  std::cerr << "Exiting... "<< std::endl;
282  exit(1);
283  }
284  absolute_flags.push_back(muonIsolationFlags[relative_flag]);
285 
286 
287  return filterFlags(unfiltered, "isolation", absolute_flags);
288  }
289 
291  std::vector<Photon*> filterIsolation(std::vector<Photon*> unfiltered, std::vector<int> relative_flags = std::vector<int>()) {
292  // Translate the relative isolation number of the analysis in the absolute number within all analyses
293  std::vector<int> absolute_flags;
294  for(int i = 0; i < relative_flags.size(); i++) {
295  if (relative_flags[i] >= photonIsolationFlags.size()) {
296  std::cerr << "Error: There is no photon isolation " << relative_flags[i] << std::endl;
297  std::cerr << "Exiting... "<< std::endl;
298  exit(1);
299  }
300  absolute_flags.push_back(photonIsolationFlags[relative_flags[i]]);
301  }
302  // if no flags are given, use all
303  if (absolute_flags.size() == 0)
304  absolute_flags = photonIsolationFlags;
305 
306  return filterFlags(unfiltered, "isolation", absolute_flags);
307  }
308 
310  std::vector<Photon*> filterIsolation(std::vector<Photon*> unfiltered, int relative_flag) {
311  std::vector<int> absolute_flags;
312  if (relative_flag >= photonIsolationFlags.size()) {
313  std::cerr << "Error: There is no photon isolation " << relative_flag << std::endl;
314  std::cerr << "Exiting... "<< std::endl;
315  exit(1);
316  }
317  absolute_flags.push_back(photonIsolationFlags[relative_flag]);
318 
319 
320  return filterFlags(unfiltered, "isolation", absolute_flags);
321  }
322 
324 
331  bool checkTauTag(Jet* candidate, std::string efficiency);
332 
334 
348  bool checkBTag(Jet* candidate, int relative_flag = 0, std::string option = "");
352 
357 
369  double mT(const TLorentzVector & vis, const TLorentzVector & invis);
370 
372 
376  double mT2(const TLorentzVector & vis1, const TLorentzVector & vis2, double m_inv, const TLorentzVector & invis = TLorentzVector(0., 0., 0., 0.));
377 
379 
383  double mCT(const TLorentzVector & v1, const TLorentzVector & v2);
384 
386 
390  double mCTcorr(const TLorentzVector & v1, const TLorentzVector & v2, const TLorentzVector & vds, const TLorentzVector & invis, const double ecm = 8000.0, const double mxlo = 0.0);
391 
393 
397  double mCTy(const TLorentzVector & v1, const TLorentzVector & v2, const TLorentzVector & vds, const TLorentzVector & invis);
398 
400 
405  double mT2_bl(const TLorentzVector & pl_in, const TLorentzVector & pb1_in, const TLorentzVector & pb2_in, const TLorentzVector & invis = TLorentzVector(0., 0., 0., 0.));
406 
408 
412  double alphaT(const std::vector<Jet*> & jets, const double thresh_ET = 0.);
413 
415 
419  std::vector<double> razor(const std::vector<TLorentzVector> & obj, const TLorentzVector & invis = TLorentzVector(0., 0., 0., 0.));
423 
454  void bookSignalRegions(std::string listOfRegions);
455  void bookControlRegions(std::string listOfRegions);
456  void bookCutflowRegions(std::string listOfRegions);
457 
459 
464  inline void countSignalEvent(std::string region) {
465  signalRegions[region] += weight;
466  signalRegions2[region] += weight*weight;
467  }
469  inline void countControlEvent(std::string region) {
470  controlRegions[region] += weight;
471  controlRegions2[region] += weight*weight;
472  }
474  inline void countCutflowEvent(std::string region) {
475  cutflowRegions[region] += weight;
476  cutflowRegions2[region] += weight*weight;
477  }
481 
495  // File streams for generalized output files
496  std::vector<std::ofstream*> fStreams;
497  std::vector<std::string> fNames;
498 
500 
510  int bookFile(std::string name, bool noheader = false);
511 
512 
514 
520 
524  inline void setInformation(std::string s) {
525  information = s;
526  };
527 
529 
530  inline void setLuminosity(double l) {
531  luminosity = l;
532  };
533 
535 
536  inline void setAnalysisName(std::string name) {
537  analysis = name;
538  };
539 
541 
547  inline double normalize(double x) {
548  return x*xsect*luminosity/sumOfWeights;
549  };
550 
552  ExRootResult *result;
553 
555 
561  void ignore(std::string ignore_what);
562  double weight;
563 
568  private:
569  // Overall reader of the ROOT tree
570  ExRootTreeReader *treeReader;
571  TChain *chain;
572 
573  // Objects for all the branches in the ROOT file
574  TClonesArray *branchElectron;
575  TClonesArray *branchMuon;
576  TClonesArray *branchJet;
577  TClonesArray *branchJet2;
578  TClonesArray *branchPhoton;
579  TClonesArray *branchMissingET;
580  TClonesArray *branchEvent;
581  TClonesArray *branchTrack;
582  TClonesArray *branchTower;
583 
584  // Information on files
585  std::string outputFolder;
586  std::string outputPrefix;
587  std::string inputFile;
588 
589  // Information about the analysis to be printed at the start of each output file
590  std::string analysis;
591  std::string information;
592 
593  // Global parameters
594  Long64_t nEvents;
595  double sumOfWeights;
596  double sumOfWeights2;
597  double xsect;
598  double xsecterr;
599  double luminosity;
600 
601  // Sums up weights (and weights^2) that fall into control, signal or cutflow regions
602  std::map<std::string, double> controlRegions;
603  std::map<std::string, double> signalRegions;
604  std::map<std::string, double> cutflowRegions;
605  std::map<std::string, double> controlRegions2;
606  std::map<std::string, double> signalRegions2;
607  std::map<std::string, double> cutflowRegions2;
608 
609  // Absolute flag values for the isolation criteria of the individual analysis
610  // [e.g. if there are 2 analyses with 2 isolation criteria each, analysis 1 gets (1, 2)
611  // whereas analysis 2 gets (3, 4) ]
612  std::vector<int> electronIsolationFlags;
613  std::vector<int> muonIsolationFlags;
614  std::vector<int> photonIsolationFlags;
615  std::vector<int> jetBTagFlags;
616  std::vector<int> jet2BTagFlags;
617 
618  // If these flags are set, the corresponding containers won't be set up (to save computing time)
619  bool ignoreElectrons;
620  bool ignoreElectrons_LooseIsolation;
621  bool ignoreElectrons_MediumEfficiency;
622  bool ignoreElectrons_TightEfficiency;
623  bool ignoreMuons;
624  bool ignoreMuons_LooseIsolation;
625  bool ignoreMuons_CombinedPlusEfficiency;
626  bool ignoreMuons_CombinedEfficiency;
627  bool ignorePhotons;
628  bool ignorePhotons_LooseIsolation;
629  bool ignoreTracks;
630  bool ignoreTowers;
631 
632  // A given set of objects (Electrons, Jets, ...) can be filtered w.r.t given flag values
633  template <class T>
634  std::vector<T*> filterFlags(std::vector<T*> unfiltered, std::string whichFlag, std::vector<int> flags) {
635  std::vector<T*> filtered;
636  for (int i = 0; i < unfiltered.size(); i++) {
637  T* cand = unfiltered[i];
638  // First, decode the member's isoflag into a vector of valid flags
639  int code = 0;
640  if(whichFlag == "isolation") {
641  code = cand->IsolationFlags;
642  }
643  else if(whichFlag == "efficiency")
644  code = cand->EfficiencyFlags;
645 
646  std::vector<int> candidates_flags = code_to_flags(code);
647 
648  // Then, check if iso_flags are all within the member's flags
649  bool passes = true;
650  for (int iso = 0; iso < flags.size(); iso++) {
651  if(std::find(candidates_flags.begin(), candidates_flags.end(), flags[iso]) == candidates_flags.end()) {
652  passes = false;
653  break;
654  }
655  }
656 
657  // Only if memebr passed it should be saved in filtered list
658  if(passes)
659  filtered.push_back(cand);
660  }
661  return filtered;
662  }
663 
664  // The flagnumber, read in binary, tells which flags have been set to true
665  std::vector<int> code_to_flags(int code) {
666  std::vector<int> flags;
667  int flag = 0;
668  while(code > 0) {
669  if(code % 2 == 1)
670  flags.push_back(flag);
671  code -= code % 2;
672  code /= 2;
673  flag++;
674  }
675  return flags;
676  }
677 
678  // Used by alphaT code
679  struct fabs_less {
680  bool operator()(const double x, const double y) const {
681  return fabs(x) < fabs(y);
682  }
683  };
684 
685 };
686 
687 
688 #endif
std::vector< X * > overlapRemoval(std::vector< X * > candidates, std::vector< Y * > neighbours, double dR)
Remove objects if they are to close.
Definition: AnalysisBase.h:161
std::vector< std::ofstream * > fStreams
Container of output file streams booked via bookFile()
Definition: AnalysisBase.h:496
double mT2(const TLorentzVector &vis1, const TLorentzVector &vis2, double m_inv, const TLorentzVector &invis=TLorentzVector(0., 0., 0., 0.))
Evaluates .
void bookSignalRegions(std::string listOfRegions)
Function to book signal regions.
Definition: AnalysisBase.cc:56
std::vector< Photon * > filterIsolation(std::vector< Photon * > unfiltered, std::vector< int > relative_flags=std::vector< int >())
Remove photons that are not isolated.
Definition: AnalysisBase.h:291
double alphaT(const std::vector< Jet * > &jets, const double thresh_ET=0.)
Evaluates .
std::vector< Muon * > muonsCombined
Container of 'muonsCombinedPlus' objects that pass 'CB' efficiency.
Definition: AnalysisBase.h:107
std::vector< Photon * > photons
Container of all truth photons after detector smearing and loose isolation condition.
Definition: AnalysisBase.h:110
double weight
Current event weight usable for e.g. histograms.
Definition: AnalysisBase.h:562
std::vector< X * > overlapRemoval(std::vector< X * > candidates, double dR)
Remove objects if they are to close to any other object in the same list.
Definition: AnalysisBase.h:191
double normalize(double x)
Normalises number to luminosity and cross section.
Definition: AnalysisBase.h:547
virtual void initialize()
Function to prepare the analysis.
Definition: AnalysisBase.h:80
double mT(const TLorentzVector &vis, const TLorentzVector &invis)
Evaluates the transverse mass .
std::vector< Electron * > electronsLoose
Container of 'electrons' objects that pass loose isolation condition.
Definition: AnalysisBase.h:102
std::vector< Electron * > filterIsolation(std::vector< Electron * > unfiltered, int relative_flag)
Remove electrons that are not isolated (simplified function for exactly one condition given as one in...
Definition: AnalysisBase.h:246
void setLuminosity(double l)
Sets the luminosity.
Definition: AnalysisBase.h:530
std::vector< Jet * > jets2
Container of all reconstructed 'second type' jets if defined in analysis settings.
Definition: AnalysisBase.h:109
std::vector< Muon * > filterIsolation(std::vector< Muon * > unfiltered, int relative_flag)
Remove muons that are not isolated (simplified function for exactly one condition given as one intege...
Definition: AnalysisBase.h:277
~AnalysisBase()
Destructor function to free pointers and close opened file streams.
Definition: AnalysisBase.cc:44
void countControlEvent(std::string region)
Function to count a given event for a control region.
Definition: AnalysisBase.h:469
double mCT(const TLorentzVector &v1, const TLorentzVector &v2)
Evaluates normal .
void countCutflowEvent(std::string region)
Function to count a given event for a cutflow region.
Definition: AnalysisBase.h:474
void bookCutflowRegions(std::string listOfRegions)
Function to book cutflow regions.
Definition: AnalysisBase.cc:89
double mT2_bl(const TLorentzVector &pl_in, const TLorentzVector &pb1_in, const TLorentzVector &pb2_in, const TLorentzVector &invis=TLorentzVector(0., 0., 0., 0.))
Evaluates .
std::vector< Photon * > filterIsolation(std::vector< Photon * > unfiltered, int relative_flag)
Remove photons that are not isolated (simplified function for exactly one condition given as one inte...
Definition: AnalysisBase.h:310
ETMiss * missingET
Reconstructed missingET vector excluding muons.
Definition: AnalysisBase.h:113
std::vector< Electron * > filterIsolation(std::vector< Electron * > unfiltered, std::vector< int > relative_flags=std::vector< int >())
Remove electrons that are not isolated.
Definition: AnalysisBase.h:227
std::vector< Electron * > electrons
Container of all truth electrons after detector smearing in acceptance range.
Definition: AnalysisBase.h:101
std::vector< Muon * > filterIsolation(std::vector< Muon * > unfiltered, std::vector< int > relative_flags=std::vector< int >())
Remove muons that are not isolated.
Definition: AnalysisBase.h:258
ExRootResult * result
Object to ExRootAnalysis for internal studies.
Definition: AnalysisBase.h:549
std::vector< Electron * > electronsTight
Container of 'electronsLoose' objects that pass 'medium' efficiency cut.
Definition: AnalysisBase.h:104
virtual void finalize()
Function to finish the analyis.
Definition: AnalysisBase.h:84
virtual void analyze()
Function containing the event wise analysis code.
Definition: AnalysisBase.h:82
void countSignalEvent(std::string region)
Function to count a given event for a signal region.
Definition: AnalysisBase.h:464
void loopOverEvents()
Internal function that starts the event loop in main().
void setAnalysisName(std::string name)
Sets the analysis name to denote the name of the output files.
Definition: AnalysisBase.h:536
std::vector< Muon * > muons
Container of all truth muons after detector smearing in acceptance range.
Definition: AnalysisBase.h:105
A class to parametrise missingET.
Definition: ETMiss.h:19
std::vector< Jet * > jets
Container of all reconstructed jets.
Definition: AnalysisBase.h:108
std::vector< Tower * > towers
Container of all calorimeter towers.
Definition: AnalysisBase.h:112
AnalysisBase(std::string inFile, std::string outFol, std::string outPre, double xs, double xserr, std::map< std::string, std::string > branches, std::map< std::string, std::vector< int > > flags)
Constructor function to load the Delphes ROOT file.
Definition: AnalysisBase.cc:3
bool checkBTag(Jet *candidate, int relative_flag=0, std::string option="")
Checks if candidate jet fulfills given b-jet identification.
void setInformation(std::string s)
Sets the header that is printed on top of all output files.
Definition: AnalysisBase.h:524
int bookFile(std::string name, bool noheader=false)
Function to book file streams accessible via fStreams and fNames.
std::vector< Electron * > electronsMedium
Container of 'electronsLoose' objects that pass 'medium' efficiency cut.
Definition: AnalysisBase.h:103
std::vector< double > razor(const std::vector< TLorentzVector > &obj, const TLorentzVector &invis=TLorentzVector(0., 0., 0., 0.))
Evaluates razor.
bool checkTauTag(Jet *candidate, std::string efficiency)
Checks if candidate jet fulfills given tau identification cut.
std::vector< T * > filterPhaseSpace(std::vector< T * > unfiltered, double pTmin=0., double etamin=-100, double etamax=100, bool exclude_overlap=false)
Require objects to have a certain ptmin and lie within a certain eta range.
Definition: AnalysisBase.h:137
double mCTcorr(const TLorentzVector &v1, const TLorentzVector &v2, const TLorentzVector &vds, const TLorentzVector &invis, const double ecm=8000.0, const double mxlo=0.0)
Evaluates boost corrected .
The base class which defines the structure and functionality of all CheckMATE analyses.
Definition: AnalysisBase.h:47
std::vector< Muon * > muonsCombinedPlus
Container of objects that pass loose isolation condition and 'CBplusST' efficiency.
Definition: AnalysisBase.h:106
void ignore(std::string ignore_what)
Does not read out unneeded ROOT information.
void bookControlRegions(std::string listOfRegions)
Function to book control regions.
Definition: AnalysisBase.cc:73
double mCTy(const TLorentzVector &v1, const TLorentzVector &v2, const TLorentzVector &vds, const TLorentzVector &invis)
Evaluates transverse.
std::vector< Track * > tracks
Container of all reconstructed tracks.
Definition: AnalysisBase.h:111
std::vector< std::string > fNames
Container of output file names booked via bookFile()
Definition: AnalysisBase.h:497