DAS  3.0
Das Analysis System
PUcorrection.h
Go to the documentation of this file.
1 #include <cstdlib>
2 #include <cassert>
3 #include <iostream>
4 #include <limits>
5 #include <functional>
6 
7 #include <TFile.h>
8 #include <TH1.h>
9 
11 
12 #include <darwin.h>
13 
14 namespace DAS::PUprofile {
15 
18 struct Correction {
19  TH1 * nominal, * upper, * lower;
20 
23  Correction (TFile * fMC, TFile * fData,
24  std::function<const char *(const char *, bool)> hname,
25  float maxWeight = std::numeric_limits<float>::max()
26  ) :
27  nominal(fData->Get<TH1>(hname("nominal", false))),
28  upper (fData->Get<TH1>(hname(("PU" + SysUp).c_str(), false))),
29  lower (fData->Get<TH1>(hname(("PU" + SysDown).c_str(), false)))
30  {
31  namespace DE = Darwin::Exceptions;
32  for (TH1 * h: {nominal, upper, lower}) {
33  if (h == nullptr)
34  BOOST_THROW_EXCEPTION( DE::BadInput("No profile found in input", *fData) );
35  h->SetDirectory(0);
36  auto integral = h->Integral();
37  if (integral == 0)
38  BOOST_THROW_EXCEPTION( DE::BadInput("Empty profile", *h) );
39  h->Scale(1./integral);
40  }
41 
42  auto n = hname("nominal", true);
43  std::cout << "Fetching " << n << std::endl;
44  auto sim = dynamic_cast<TH1*>(fMC->Get(n));
45  if (sim == nullptr)
46  BOOST_THROW_EXCEPTION( DE::BadInput("No profile found in input", *fMC) );
47  auto integral = sim->Integral();
48  if (integral == 0)
49  BOOST_THROW_EXCEPTION( DE::BadInput("Empty profile", *sim) );
50  sim->Scale(1./integral);
51  for (TH1 * h: {nominal, upper, lower})
52  h->Divide(sim);
53 
54  for (TH1 *h: {nominal, upper, lower}) {
55  for (int i = 0; i <= h->GetNbinsX()+1; ++i) {
56  if (h->GetBinContent(i) < maxWeight) continue;
57  h->SetBinContent(i, maxWeight);
58  }
59  }
60  }
61 
67  float operator() (float pu, char v = '0') const
68  {
69  namespace DE = Darwin::Exceptions;
70  TH1 * h = nullptr;
71  switch (v) {
72  case '0': h = nominal; break;
73  case '+': h = upper ; break;
74  case '-': h = lower ; break;
75  default:
76  BOOST_THROW_EXCEPTION( std::runtime_error("Unknown variation") );
77  }
78  if (h == nullptr)
79  BOOST_THROW_EXCEPTION( DE::BadInput("No correction found for this variation input", *h) );
80  int ibin = h->FindBin(pu);
81  return h->GetBinContent(ibin);
82  }
83 
86  void Write (TDirectory * d, bool reset)
87  {
88  d->cd();
89  for (TH1 * h: {nominal, upper, lower}) {
90  h->SetDirectory(d);
91  if (reset) h->Reset();
92  }
93  nominal->Write("nominal");
94  upper->Write("upper");
95  lower->Write("lower");
96  }
97 };
98 
99 } // end of namespaces
DAS::PUprofile::Correction::lower
TH1 * lower
Definition: PUcorrection.h:19
DAS::PUprofile::Correction::upper
TH1 * upper
Definition: PUcorrection.h:19
DAS::SysUp
const std::string SysUp
Suffix used for "up" uncertainties. Follows the Combine convention.
Definition: Format.h:8
Format.h
DAS::PUprofile
Definition: applyBinnedPUprofCorrection.cc:35
DAS::Normalisation::reset
void reset(Weights &wgts, const double v=0)
Definition: applyDataNormalisation.h:28
Darwin::Exceptions
Handling of exceptions.
Definition: darwin.h:35
DAS::PUprofile::Correction
Functor to apply the PU profile correction.
Definition: PUcorrection.h:18
DAS::PUprofile::Correction::Write
void Write(TDirectory *d, bool reset)
Write output to given directory.
Definition: PUcorrection.h:86
DAS::PUprofile::Correction::Correction
Correction(TFile *fMC, TFile *fData, std::function< const char *(const char *, bool)> hname, float maxWeight=std::numeric_limits< float >::max())
Constructor.
Definition: PUcorrection.h:23
DAS::PUprofile::Correction::nominal
TH1 * nominal
Definition: PUcorrection.h:19
DAS::SysDown
const std::string SysDown
Suffix used for "down" uncertainties. Follows the Combine convention.
Definition: Format.h:10
Darwin::Exceptions::BadInput
Generic exception for ill-defined input (before the event loop).
Definition: exceptions.h:68
DAS::PUprofile::Correction::operator()
float operator()(float pu, char v='0') const
Definition: PUcorrection.h:67