DESY Hbb Analysis Framework
PileupWeight.cc
Go to the documentation of this file.
1 // system include files
2 // user include files
4 #include "TFile.h"
5 
6 //
7 // class declaration
8 //
9 
10 using namespace analysis;
11 using namespace analysis::tools;
12 
13 //
14 // constructors and destructor
16 {
17 }
18 
19 // Main constructor
20 PileupWeight::PileupWeight(const std::string & fname )
21 {
22  TH1D * tmp;
23  std::shared_ptr<TFile> f = std::make_shared<TFile>(fname.c_str(),"old");
24  tmp = (TH1D*) f->Get("weight_2down");
25  if ( tmp ) histos_[-2] = std::make_shared<TH1D>(*tmp);
26 
27  tmp = (TH1D*) f->Get("weight_1down");
28  if ( tmp ) histos_[-1] = std::make_shared<TH1D>(*tmp);
29 
30  tmp = (TH1D*) f->Get("weight");
31  if ( tmp ) histos_[0] = std::make_shared<TH1D>(*tmp);
32 
33  tmp = (TH1D*) f->Get("weight_1up");
34  if ( tmp ) histos_[1] = std::make_shared<TH1D>(*tmp);
35 
36  tmp = (TH1D*) f->Get("weight_2up");
37  if ( tmp ) histos_[2] = std::make_shared<TH1D>(*tmp);
38 
39  if ( ! histos_[-2] )
40  std::cout << "WARNING - PileupWeight::PileupWeight | Histogram weight_2down not found. Weight = 1" << std::endl;
41  if ( ! histos_[-1] )
42  std::cout << "WARNING - PileupWeight::PileupWeight | Histogram weight_1down not found. Weight = 1" << std::endl;
43  if ( ! histos_[0] )
44  std::cout << "WARNING - PileupWeight::PileupWeight | Histogram weight not found. Weight = 1" << std::endl;
45  if ( ! histos_[1] )
46  std::cout << "WARNING - PileupWeight::PileupWeight | Histogram weight_1up not found. Weight = 1" << std::endl;
47  if ( ! histos_[2] )
48  std::cout << "WARNING - PileupWeight::PileupWeight | Histogram weight_2up not found. Weight = 1" << std::endl;
49 }
50 
52 {
53  // do anything here that needs to be done at desctruction time
54  // (e.g. close files, deallocate resources etc.)
55 }
56 
57 
58 //
59 // member functions
60 //
61 // ------------ method called for each event ------------
62 
63 // output tree
64 float PileupWeight::weight(const float & truepu, const int & var)
65 {
66  float weight = 1.;
67  if ( ! histos_[0] ) return weight;
68  if ( ! histos_[var] ) return weight;
69  int bin = histos_[var] -> FindBin(truepu);
70  weight = histos_[var] -> GetBinContent(bin);
71  return weight;
72 }
73 
float weight(const float &truepu, const int &var=0)
Definition: PileupWeight.cc:64
TFile * f[10]
Definition: PlotsCompare.cc:24
std::map< int, std::shared_ptr< TH1D > > histos_
Definition: PileupWeight.h:51