ra4_stats  0341147a0dc35f80f4e12c6003afb76a38e2ed6e
yield_manager.cpp
Go to the documentation of this file.
1 #include "yield_manager.hpp"
2 
3 #include <iostream>
4 #include <sstream>
5 #include <array>
6 
7 #include "bin.hpp"
8 #include "process.hpp"
9 #include "cut.hpp"
10 #include "utilities.hpp"
11 
12 using namespace std;
13 
14 map<YieldKey, GammaParams> YieldManager::yields_ = map<YieldKey, GammaParams>();
15 const double YieldManager::store_lumi_ = 4.;
16 
18  local_lumi_(lumi),
19  verbose_(false){
20 }
21 
23  if(!HaveYield(key)) ComputeYield(key);
24 
25  double factor = local_lumi_/store_lumi_;
26  if(GetProcess(key).IsData()) factor = 1.;
27 
28  return factor*yields_.at(key);
29 }
30 
32  const Process &process,
33  const Cut &cut) const{
34  return GetYield(YieldKey(bin, process, cut));
35 }
36 
37 const double & YieldManager::Luminosity() const{
38  return local_lumi_;
39 }
40 
42  return local_lumi_;
43 }
44 
45 bool YieldManager::HaveYield(const YieldKey &key) const{
46  return yields_.find(key) != yields_.end();
47 }
48 
49 void YieldManager::ComputeYield(const YieldKey &key) const{
50  const Bin &bin = GetBin(key);
51  const Process &process = GetProcess(key);
52  const Cut &cut = GetCut(key);
53 
54  GammaParams gps;
55 
56  if(HaveYield(key)){
57  if(verbose_){
58  cout << "Using known yield for " << key << endl;
59  }
60  gps = GetYield(key);
61  }else if(process.GetEntries() == 0){
62  if(verbose_){
63  cout << "No entries found for " << key << endl;
64  }
65  gps.SetNEffectiveAndWeight(0., 0.);
66  }else{
67  if(verbose_){
68  cout << "Computing yield for " << key << endl;
69  }
70  ostringstream oss;
71  oss << local_lumi_ << flush;
72  Cut lumi_weight = process.IsData() ? Cut() :
73  (Contains(process.Name(), "sig")?Cut(oss.str()+"*weight*eff_trig"):Cut(oss.str()+"*weight*eff_trig"));
74 
75  array<Cut, 5> cuts;
76  cuts.at(0) = lumi_weight*(cut && bin.Cut() && process.Cut());
77  cuts.at(1) = lumi_weight*(cut && process.Cut());
78  cuts.at(2) = lumi_weight*(process.Cut());
79  cuts.at(3) = lumi_weight;
80  cuts.at(4) = Cut();
81 
82  for(size_t icut = 0; icut < cuts.size() && gps.Weight()<=0.; ++icut){
83  if(icut > 0 && !process.CountZeros()){
84  gps.SetNEffectiveAndWeight(0., 0.);
85  break;
86  }
87  Cut &this_cut = cuts.at(icut);
88  if(verbose_){
89  cout << "Trying cut " << this_cut << endl;
90  }
91  GammaParams temp_gps = process.GetYield(this_cut);
94  if(Contains(process.Name(), "sig")){
95  string mettru_s = this_cut.GetCut();
96  ReplaceAll(mettru_s, "met_calo", "XXXYYYZZZ_calo");
97  ReplaceAll(mettru_s, "met", "met_tru");
98  ReplaceAll(mettru_s, "XXXYYYZZZ_calo", "met_calo");
99  Cut mettru_cut(mettru_s);
100  GammaParams mettru_gps = process.GetYield(mettru_cut);
101  if(verbose_) cout<<"Yields: met "<<temp_gps.Yield()<<", met_tru "<<mettru_gps.Yield();
102  temp_gps.SetYieldAndUncertainty(0.5*(temp_gps.Yield()+mettru_gps.Yield()),
103  max(temp_gps.Uncertainty(), mettru_gps.Uncertainty()));
104  if(verbose_) cout<<", average "<<temp_gps.Yield()<<" for bin "<<bin.Name()<<endl;
105  } // If it is signal
106  if(icut == 0) gps = temp_gps;
107  else gps.SetNEffectiveAndWeight(0., temp_gps.Weight());
108  }
109  }
110 
111  if(verbose_){
112  cout << "Found yield=" << gps << '\n' << endl;
113  }
114  double factor = store_lumi_/local_lumi_;
115  if(process.IsData()) factor = 1.;
116  yields_[key] = factor*gps;
117 }
double Weight() const
Definition: bin.hpp:12
const std::string & Name() const
Definition: process.cpp:52
void ComputeYield(const YieldKey &key) const
void SetYieldAndUncertainty(double yield, double uncertainty)
Definition: cut.hpp:8
void ReplaceAll(std::string &str, const std::string &orig, const std::string &rep)
Definition: utilities.cpp:34
STL namespace.
bool Contains(const std::string &str, const std::string &pat)
Definition: utilities.cpp:26
YieldManager(double lumi=4.)
const Cut & GetCut(const YieldKey &yk)
Definition: yield_key.cpp:13
long GetEntries() const
Definition: process.cpp:74
const bool & IsData() const
Definition: process.cpp:86
std::tuple< Bin, Process, Cut > YieldKey
Definition: yield_key.hpp:11
const bool & CountZeros() const
Definition: process.cpp:102
GammaParams GetYield(const class Cut &cut=::Cut("1")) const
Definition: process.cpp:78
const Bin & GetBin(const YieldKey &yk)
Definition: yield_key.cpp:5
const class Cut & Cut() const
Definition: bin.cpp:28
double Yield() const
const class Cut & Cut() const
Definition: process.cpp:62
static const double store_lumi_
std::string GetCut()
Definition: cut.cpp:22
double local_lumi_
bool HaveYield(const YieldKey &key) const
const Process & GetProcess(const YieldKey &yk)
Definition: yield_key.cpp:9
const std::string & Name() const
Definition: bin.cpp:19
GammaParams GetYield(const YieldKey &key) const
double Uncertainty() const
const double & Luminosity() const
static std::map< YieldKey, GammaParams > yields_
void SetNEffectiveAndWeight(double n_effective, double weight)