CMS 3D CMS Logo

Utilities.cc
Go to the documentation of this file.
1 // Utilities.cxx //
2 // =====================================================================//
3 // //
4 // Various helpful functions. //
5 // //
7 
9 // _______________________Includes_______________________________________//
11 
13 
14 #include "TRandom3.h"
15 #include "TStopwatch.h"
16 #include "TTree.h"
17 #include "TNtuple.h"
18 #include "TFile.h"
19 #include "TChain.h"
20 #include "TMath.h"
21 
22 using namespace emtf;
23 
25 // ------------------Some Helpful Arrays----------------------------------
27 
28 // Array of GeV values for error calculation.
29 const double emtf::ptscale[31] = { 0,
30  1.5, 2.0, 2.5, 3.0, 3.5, 4.0,
31  4.5, 5.0, 6.0, 7.0, 8.0, 10.0, 12.0, 14.0,
32  16.0, 18.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0,
33  50.0, 60.0, 70.0, 80.0, 90.0, 100.0, 120.0, 140.0 };
34 
35 const std::vector<double> ptScale = std::vector<double>(ptscale, ptscale + sizeof ptscale / sizeof ptscale[0]);
36 
37 // Array of counts for error calculation.
38 const double emtf::twoJets_scale[16] = { 0,
39  0.5, 1.0, 2.0, 3.0, 4.0, 5.0, 10.0, 20.0, 50.0,
40  100, 500, 1000, 5000, 7500, 50000};
41 
42 const std::vector<double> emtf::twoJetsScale = std::vector<double>(twoJets_scale, twoJets_scale + sizeof twoJets_scale / sizeof twoJets_scale[0]);
43 
44 
45 
47 // ------------------Some Helpful Functions-------------------------------
49 
50 float processPrediction(float BDTPt, int Quality, float PrelimFit)
51 {
52 // Discretize and scale the BDTPt prediction
53 
54 
55  // Fix terrible predictions
56  if(BDTPt < 0) BDTPt = PrelimFit;
57  if(BDTPt > 250) BDTPt = PrelimFit;
58 
59  float BDTPt1 = BDTPt;
60  float scaleF = 1.0;
61 
62  // Scale based upon quality
63  if (Quality == 3) scaleF = 1.15;
64  if (Quality == 2) scaleF = 1.3;
65  if (Quality == 1) scaleF = 1.7;
66 
67  BDTPt1 = scaleF*BDTPt1;
68 
69 
70  // Discretize based upon ptscale
71  for (int pts=0; pts<31; pts++)
72  {
73  if (ptscale[pts]<=BDTPt1 && ptscale[pts+1]>BDTPt1)
74  {
75  BDTPt1 = ptscale[pts];
76  break;
77  }
78  }
79 
80  if (BDTPt1 > 140) BDTPt1 = 140;
81  if (BDTPt1 < 0) BDTPt1 = 0;
82 
83  return BDTPt1;
84 }
85 
87 // ----------------------------------------------------------------------
89 
90 void mergeNtuples(const char* ntuplename, const char* filestomerge, const char* outputfile)
91 {
92  TChain chain(ntuplename);
93  chain.Add(filestomerge);
94  chain.Merge(outputfile);
95 }
96 
98 // ----------------------------------------------------------------------
100 
101 void sortNtupleByEvent(const char* ntuplename, const char* filenametosort, const char* outputfile)
102 {
103  //TFile f("../../all_test_redux_post.root");
104  TFile f(filenametosort);
105  TNtuple *tree = (TNtuple*)f.Get(ntuplename);
106  int nentries = (int)tree->GetEntries();
107  //Drawing variable pz with no graphics option.
108  //variable pz stored in array fV1 (see TTree::Draw)
109  tree->Draw("Event","","goff");
110  int *index = new int[nentries];
111  //sort array containing pz in decreasing order
112  //The array index contains the entry numbers in decreasing order
113  TMath::Sort(nentries,tree->GetV1(),index);
114 
115  //open new file to store the sorted Tree
116  //TFile f2("../../test_events_sorted.root","recreate");
117  TFile f2(outputfile,"recreate");
118 
119  //Create an empty clone of the original tree
120  TTree *tsorted = (TTree*)tree->CloneTree(0);
121  for (int i=0;i<nentries;i++) {
122  tree->GetEntry(index[i]);
123  tsorted->Fill();
124  }
125  tsorted->Write();
126  delete [] index;
127 }
Definition: chain.py:1
Definition: Event.h:15
static const double pts[33]
Definition: Constants.h:30
void sortNtupleByEvent(const char *ntuplename, const char *filenametosort, const char *outputfile)
Definition: Utilities.cc:101
double f[11][100]
const double twoJets_scale[16]
Definition: Utilities.cc:38
void mergeNtuples(const char *ntuplename, const char *filestomerge, const char *outputfile)
Definition: Utilities.cc:90
const std::vector< double > twoJetsScale
Definition: Utilities.cc:42
const std::vector< double > ptScale
Definition: Utilities.cc:35
const double ptscale[31]
Definition: Utilities.cc:29
float processPrediction(float BDTPt, int Quality, float PrelimFit)
Definition: Utilities.cc:50
Definition: tree.py:1