CMS 3D CMS Logo

ReadTree.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <cstring>
3 #include <fstream>
4 #include <cmath>
5 #include <TFile.h>
6 #include <TH1F.h>
7 #include <TMath.h>
8 #include <TTree.h>
9 #include "Utilities.h"
10 
11 using namespace std;
12 
13 int main(int argc, char **argv) {
15  c1.parse(argc, argv);
16 
17  std::string TreeFilename = c1.getValue<string>("TreeFilename");
18  std::string HistoFilename = c1.getValue<string>("HistoFilename");
19  bool UseRatioForResponse = c1.getValue<bool>("UseRatioForResponse", true);
20  bool IsPFJet = c1.getValue<bool>("IsPFJet", false);
21  int NJETS_MAX = c1.getValue<int>("NJETS_MAX", 2);
22  double JetPT_MIN = c1.getValue<double>("JetPT_MIN", 1.0);
23  double DR_MIN = c1.getValue<double>("DR_MIN", 0.25);
24  std::vector<double> pt_vec = c1.getVector<double>("RefPtBoundaries");
25  std::vector<double> eta_vec = c1.getVector<double>("EtaBoundaries");
26  if (!c1.check())
27  return 0;
28  c1.print();
30  const int MAX_NETA = 83;
31  const int MAX_NPT = 30;
32  TH1F *hPtRef[MAX_NPT][MAX_NETA];
33  TH1F *hPtJet[MAX_NPT][MAX_NETA];
34  TH1F *hPtRefBarrel[MAX_NPT];
35  TH1F *hPtJetBarrel[MAX_NPT];
36  TH1F *hResponseBarrel[MAX_NPT];
37  TH1F *hResponse[MAX_NPT][MAX_NETA];
38  int NPT = pt_vec.size() - 1;
39  int NETA = eta_vec.size() - 1;
40  char name[1024];
41  bool cut_ptmin, cut_dR, cut_njets;
42  float ptJet, ptGen, etaJet, emfJet, chfJet, etaGen, phiJet, phiGen, dR, resp;
43  int rank;
44  int i, j, ind_pt, ind_eta, responseBins(2200);
45  double responseLow(-1000.), responseHigh(100.);
46  unsigned int entry;
47  if (UseRatioForResponse) {
48  responseBins = 200;
49  responseLow = 0.;
50  responseHigh = 2;
51  }
52  std::vector<std::string> HistoNamesList;
53  TFile *inf = new TFile(TreeFilename.c_str(), "R");
54  if (inf->IsZombie())
55  return 0;
56  TFile *outf = new TFile(HistoFilename.c_str(), "RECREATE");
57 
58  TTree *tr = (TTree *)inf->Get("mcTruthTree");
59  TBranch *br_ptJet = (TBranch *)tr->GetBranch("ptJet");
60  br_ptJet->SetAddress(&ptJet);
61  TBranch *br_ptGen = (TBranch *)tr->GetBranch("ptGen");
62  br_ptGen->SetAddress(&ptGen);
63  TBranch *br_emfJet, *br_chfJet;
64  if (!IsPFJet) {
65  br_emfJet = (TBranch *)tr->GetBranch("emfJet");
66  br_emfJet->SetAddress(&emfJet);
67  } else {
68  br_chfJet = (TBranch *)tr->GetBranch("chfJet");
69  br_chfJet->SetAddress(&chfJet);
70  }
71  TBranch *br_etaJet = (TBranch *)tr->GetBranch("etaJet");
72  br_etaJet->SetAddress(&etaJet);
73  TBranch *br_etaGen = (TBranch *)tr->GetBranch("etaGen");
74  br_etaGen->SetAddress(&etaGen);
75  TBranch *br_phiJet = (TBranch *)tr->GetBranch("phiJet");
76  br_phiJet->SetAddress(&phiJet);
77  TBranch *br_phiGen = (TBranch *)tr->GetBranch("phiGen");
78  br_phiGen->SetAddress(&phiGen);
79  TBranch *br_dR = (TBranch *)tr->GetBranch("dR");
80  br_dR->SetAddress(&dR);
81  TBranch *br_rank = (TBranch *)tr->GetBranch("rank");
82  br_rank->SetAddress(&rank);
83  std::cout << "Total entries:" << tr->GetEntries() << std::endl;
84  if (NETA > 1) {
85  for (i = 0; i < NPT; i++) {
86  sprintf(name, "ptRef_RefPt%d_Barrel", i);
87  hPtRefBarrel[i] = new TH1F(name, name, 2000, 0, 2000);
88  sprintf(name, "ptJet_RefPt%d_Barrel", i);
89  hPtJetBarrel[i] = new TH1F(name, name, 2000, 0, 2000);
90  sprintf(name, "Response_RefPt%d_Barrel", i);
91  hResponseBarrel[i] = new TH1F(name, name, responseBins, responseLow, responseHigh);
92  for (j = 0; j < NETA; j++) {
93  sprintf(name, "ptRef_RefPt%d_Eta%d", i, j);
94  hPtRef[i][j] = new TH1F(name, name, 2000, 0, 2000);
95  sprintf(name, "ptCalo_RefPt%d_Eta%d", i, j);
96  hPtJet[i][j] = new TH1F(name, name, 2000, 0, 2000);
97  sprintf(name, "Response_RefPt%d_Eta%d", i, j);
98  hResponse[i][j] = new TH1F(name, name, responseBins, responseLow, responseHigh);
99  }
100  }
101  } else {
102  for (i = 0; i < NPT; i++) {
103  sprintf(name, "ptRef_RefPt%d", i);
104  hPtRefBarrel[i] = new TH1F(name, name, 2000, 0, 2000);
105  sprintf(name, "ptCalo_RefPt%d", i);
106  hPtJetBarrel[i] = new TH1F(name, name, 2000, 0, 2000);
107  sprintf(name, "Response_RefPt%d", i);
108  hResponseBarrel[i] = new TH1F(name, name, responseBins, responseLow, responseHigh);
109  }
110  }
111  std::cout << "Histograms booked" << std::endl;
112  for (entry = 0; entry < tr->GetEntries(); entry++) {
113  if (entry % (tr->GetEntries() / 10) == 0)
114  std::cout << "Entries: " << entry << std::endl;
115  tr->GetEntry(entry);
116  cut_ptmin = (ptJet > JetPT_MIN);
117  cut_dR = (dR < DR_MIN);
118  cut_njets = (rank < NJETS_MAX);
119  if (cut_ptmin && cut_dR && cut_njets) {
120  ind_pt = getBin(ptGen, pt_vec);
121  ind_eta = getBin(etaJet, eta_vec);
122  resp = 0.;
123  if (UseRatioForResponse && ptGen > 0)
124  resp = ptJet / ptGen;
125  else
126  resp = ptJet - ptGen;
127  if (NETA > 1) {
128  if (fabs(etaJet) < 1.3 && ind_pt >= 0) {
129  hPtRefBarrel[ind_pt]->Fill(ptGen);
130  hPtJetBarrel[ind_pt]->Fill(ptJet);
131  hResponseBarrel[ind_pt]->Fill(resp);
132  }
133  if (ind_pt >= 0 && ind_eta >= 0 && NETA > 1) {
134  hPtRef[ind_pt][ind_eta]->Fill(ptGen);
135  hPtJet[ind_pt][ind_eta]->Fill(ptJet);
136  hResponse[ind_pt][ind_eta]->Fill(resp);
137  }
138  }
139  if (NETA == 1)
140  if (ind_pt >= 0) {
141  hPtRefBarrel[ind_pt]->Fill(ptGen);
142  hPtJetBarrel[ind_pt]->Fill(ptJet);
143  hResponseBarrel[ind_pt]->Fill(resp);
144  }
145  }
146  }
147  outf->Write();
148  outf->Close();
149  return 0;
150 }
int main(int argc, char **argv)
Definition: ReadTree.cc:13
int getBin(double x, std::vector< double > boundaries)
Definition: Utilities.h:456