MiniTree.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #ifndef minitree_cxx
  2. #define minitree_cxx
  3. #include <algorithm>
  4. #include <functional>
  5. #include <cstdio>
  6. #include <string>
  7. #include <vector>
  8. #include <map>
  9. #include <TStyle.h>
  10. #include "MiniTree.h"
  11. #include "HistCollection.h"
  12. using namespace std;
  13. HistCollection* build_histograms(const char* sample_name, const char* filename){
  14. HistCollection *hc = new HistCollection(sample_name);
  15. TFile *f = TFile::Open(filename);
  16. TTree *tree = (TTree*) f->Get("tree");
  17. MiniTree minitree(tree);
  18. minitree.Loop(hc);
  19. delete tree;
  20. f->Close();
  21. delete f;
  22. printf("Finished filling histograms for file %s\n", filename);
  23. return hc;
  24. }
  25. void set_branch_statuses(MiniTree *minitree){
  26. auto on = [minitree](const char* bname){
  27. minitree->fChain->SetBranchStatus(bname, true);
  28. };
  29. auto off = [minitree](const char* bname){
  30. minitree->fChain->SetBranchStatus(bname, false);
  31. };
  32. off("*");
  33. on("nLepGood");
  34. on("LepGood_pt");
  35. on("LepGood_eta");
  36. on("LepGood_phi");
  37. on("LepGood_mcPt");
  38. on("LepGood_charge");
  39. on("nJet");
  40. on("Jet_btagCMVA");
  41. on("Jet_pt");
  42. on("nGenTop");
  43. }
  44. double delta_r2(double eta1, double eta2, double phi1, double phi2){
  45. return pow(eta1 - eta2, 2) + pow(phi1 - phi2, 2);
  46. }
  47. double isolation(int n, int i, function<double(int, int)> delta_r2_calculator){
  48. double min_iso = -1;
  49. for (int j=0; j<n; j++){
  50. if (i == j) continue;
  51. double curr_iso = delta_r2_calculator(i, j);
  52. if (curr_iso < min_iso || min_iso == -1){
  53. min_iso = curr_iso;
  54. }
  55. }
  56. if (min_iso == -1)
  57. return min_iso;
  58. else
  59. return sqrt(min_iso);
  60. }
  61. void MiniTree::Loop(HistCollection* hc){
  62. /* Define first a few little subroutines that will be used later in the
  63. * main loop.
  64. */
  65. auto lepton_delta_r2 = [this](int i, int j){
  66. return delta_r2(this->LepGood_eta[i], this->LepGood_eta[j],
  67. this->LepGood_phi[i], this->LepGood_phi[j]);
  68. };
  69. auto lepton_isolation = [this, lepton_delta_r2](int i){
  70. return isolation(this->nLepGood, i, function<double(int, int)>(lepton_delta_r2));
  71. };
  72. auto mini_isolation_cut = [this, lepton_isolation](int i){
  73. /* Implement the mini-isolation cut which utilizes a dynamic isolation cone size
  74. * based on lepton pt
  75. */
  76. double r0 = 0.5;
  77. double pt0 = 7.5; // GeV
  78. double iso_cut = min(r0, pt0/this->LepGood_pt[i]);
  79. double isolation = lepton_isolation(i);
  80. return isolation > iso_cut;
  81. };
  82. if (fChain == 0) return;
  83. /* Main analysis code begins here
  84. *
  85. */
  86. set_branch_statuses(this);
  87. Long64_t nentries = fChain->GetEntriesFast();
  88. Long64_t nbytes = 0, nb = 0;
  89. for (Long64_t jentry=0; jentry<nentries;jentry++) {
  90. Long64_t ientry = LoadTree(jentry);
  91. if (ientry < 0) break;
  92. nb = fChain->GetEntry(jentry); nbytes += nb;
  93. /* Main analysis code begins here
  94. *
  95. */
  96. hc->lepton_count->Fill(nLepGood);
  97. hc->top_quark_count->Fill(nGenTop);
  98. for(int i=0; i<nLepGood; i++){
  99. double delta_pt_val = LepGood_pt[i] - LepGood_mcPt[i];
  100. hc->delta_pt->Fill(delta_pt_val);
  101. hc->lepton_count_vs_delta_pt->Fill(nLepGood, delta_pt_val);
  102. }
  103. hc->jet_count->Fill(nJet);
  104. int b_jet_count = 0;
  105. for(int i=0; i<nJet; i++){
  106. hc->b_jet_discriminator->Fill(Jet_btagCMVA[i]);
  107. b_jet_count += (Jet_btagCMVA[i] > 0);
  108. }
  109. hc->b_jet_count->Fill(b_jet_count);
  110. for(int i=0; i<nJet; i++){
  111. hc->b_jet_pt_all->Fill(Jet_pt[i]);
  112. if (b_jet_count >= 3){
  113. hc->b_jet_pt_3_or_more->Fill(Jet_pt[i]);
  114. }
  115. }
  116. int lepton_count_pass_miniiso= 0;
  117. for(int i=0; i<nLepGood; i++){
  118. lepton_count_pass_miniiso += mini_isolation_cut(i);
  119. }
  120. hc->lepton_count_pass_miniiso->Fill(lepton_count_pass_miniiso);
  121. if (nLepGood == 3){
  122. hc->jet_count_trilepton->Fill(nJet);
  123. }
  124. if (nLepGood == 2 && (LepGood_charge[0] * LepGood_charge[1]) == 1){
  125. hc->jet_count_ss_dilepton->Fill(nJet);
  126. }
  127. if (nLepGood == 2 && (LepGood_charge[0] * LepGood_charge[1]) == -1){
  128. hc->jet_count_os_dilepton->Fill(nJet);
  129. }
  130. if (nLepGood == 3){
  131. vector<double> isos = {lepton_isolation(0),
  132. lepton_isolation(1),
  133. lepton_isolation(2)};
  134. sort(isos.begin(), isos.end()); //Sorts ascending by default
  135. hc->trilepton_iso_1->Fill(isos[2]);
  136. hc->trilepton_iso_2->Fill(isos[1]);
  137. hc->trilepton_iso_3->Fill(isos[0]);
  138. }
  139. }
  140. }
  141. #endif // tree_cxx