DAS  3.0
Das Analysis System
common.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cassert>
4 #include <iostream>
5 #include <optional>
6 #include <regex>
7 #include <string>
8 #include <sstream>
9 
13 
14 #include <boost/algorithm/string.hpp>
15 #include <boost/assert/source_location.hpp>
16 
17 #include <correction.h>
18 #include <darwin.h>
19 
20 namespace DAS::JetEnergy {
21 
22 inline static const std::vector<double> pt_JERC_edges = {15, 17, 20, 23, 27, 30, 35, 40, 45, 57, 72, 90, 120, 150, 200, 300, 400, 550, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500, 6000, 6500, 7000, 7500, 8000, 8500, 9000, 9500};
23 inline static const int nPtJERCbins = pt_JERC_edges.size()-1;
24 
27 inline static const std::vector<double> abseta_edges = { 0.000, 0.261, 0.522, 0.783, 1.044, 1.305, 1.566, 1.740, 1.930, 2.043, 2.172, 2.322, 2.500, 2.650, 2.853, 2.964, 3.139, 3.489, 3.839, 5.191 };
28 inline static const int nAbsEtaBins = abseta_edges.size()-1;
29 inline static const std::vector<TString> absetaBins = MakeTitle(abseta_edges, "|#eta|", false, true, [](double v) { return Form("%.3f", v);});
30 
31 inline static const std::map<int,std::vector<double>> rho_edges = {
32  { 2016, {0, 6.69, 12.39, 18.09, 23.79, 29.49, 35.19, 40.9, 999} }, // EOY16
33  { 2017, {0, 7.47, 13.49, 19.52, 25.54, 31.57, 37.59, 999} }, // UL17
34  { 2018, {0, 7.35, 13.26, 19.17, 25.08, 30.99, 36.9, 999} }, // UL18
35 
36  { 2024, {0.0, 8.23, 14.49, 20.75, 27.01, 33.27, 39.53, 45.79, 52.05, 999} }, // Winter24 (copy of Summer23)
37 };
38 
39 inline int GetNRhoBins (int year) { return rho_edges.at(year).size()-1; }
40 inline std::vector<TString> rhoBins (int year)
41 {
42  return MakeTitle(rho_edges.at(year), "#rho", false, false,
43  [](double v) { return Form("%.2f", v); });
44 }
45 
48 inline static const std::vector<double> eta_unc_edges = {-5.4, -5.0, -4.4, -4.0, -3.5, -3.0, -2.8, -2.6, -2.4, -2.2, -2.0, -1.8, -1.6, -1.4, -1.2, -1.0, -0.8, -0.6, -0.4, -0.2, 0.0 , 0.2 , 0.4 , 0.6 , 0.8 , 1.0 , 1.2 , 1.4 , 1.6 , 1.8 , 2.0 , 2.2 , 2.4 , 2.6 , 2.8 , 3.0, 3.5 , 4.0 , 4.4 , 5.0 , 5.4};
49 inline static const int nEtaUncBins = eta_unc_edges.size()-1;
50 
51 inline static const std::vector<std::string> JES_variations {"AbsoluteStat", "AbsoluteScale", "AbsoluteMPFBias", "Fragmentation", "SinglePionECAL", "SinglePionHCAL", "FlavorQCD", "RelativeJEREC1", "RelativeJEREC2", "RelativeJERHF", "RelativePtBB", "RelativePtEC1", "RelativePtEC2", "RelativePtHF", "RelativeBal", "RelativeSample", "RelativeFSR", "RelativeStatFSR", "RelativeStatEC", "RelativeStatHF", "PileUpDataMC", "PileUpPtRef", "PileUpPtBB", "PileUpPtEC1", "PileUpPtEC2", "PileUpPtHF"};
52 
53 inline static const float w = 0.8;
54 
55 inline std::vector<double> getBinning (int nBins, float first, float last)
56 {
57  std::vector<double> bins(nBins+1);
58  for(int i = 0; i <= nBins; ++i)
59  bins[i] = first + ((last-first)/nBins) * i;
60  return bins;
61 }
62 
63 inline void assertValidBinning (const std::vector<double>& v)
64 {
65  assert(v.size() > 1);
66  for (size_t i = 1; i<v.size(); ++i) {
67  if (v[i] > v[i-1]) continue;
68  std::cerr << i << ' ' << v[i] << ' ' << v[i-1] << '\n';
69  }
70 }
71 
74 inline std::vector<float> GetLogBinning (float minpt, float maxpt, int nbins)
75 {
76  assert(maxpt > minpt);
77  assert(minpt > 0);
78  assert(nbins > 1);
79 
80  std::vector<float> edges;
81 
82  float R = pow(maxpt/minpt,1./nbins);
83  for (float pt = minpt; pt <= maxpt; pt *= R) edges.push_back(pt);
85  std::cout << edges.size() << std::endl;
86 
87  return edges;
88 }
89 
92 inline std::string GetAlgo (const Darwin::Tools::UserInfo& metainfo)
93 {
94  if (metainfo.Find("flags", "labels", "CHS" )) return "chs";
95  if (metainfo.Find("flags", "labels", "PUPPI")) return "Puppi";
96 
97  std::cerr << orange << "Couldn't identify CHS or PUPPI. Running default "
98  "CHS for Run 2 (PUPPI foir Run 3)\n" << def;
99 
100  auto year = metainfo.Get<int>("flags", "year");
101  if (year > 2019) return "Puppi"; // run 3
102  if (year > 2014) return "chs"; // run 2
103  return ""; // run 1
104 }
105 
108 std::string GetShortCampaign (const std::string& campaign)
109 {
110  using namespace std;
111  regex r("^(Summer|Fall|Autumn|Winter|Spring)[0-9]{2}[A-Za-z0-9]*");
112  smatch campaign_short;
113  if (!regex_search(campaign, campaign_short, r))
114  BOOST_THROW_EXCEPTION( invalid_argument("The campaign could not be identified"));
115  return campaign_short.str();
116 }
117 
121 std::string ScanCorrections (const auto& corrections,
122  const std::string& key = "")
123 {
124  std::stringstream s;
125  s << "Available corrections:";
126  for (const auto& correction: corrections) {
127  bool found = correction.first == key;
128  if (found) s << bold << green;
129  s << ' ' << correction.first;
130  if (found) s << def;
131  }
132  return s.str();
133 }
134 
137 template<typename CorrectionType>
138 CorrectionType GetCorrection (const auto& corrections,
139  const std::string& campaign,
140  const std::string& type,
141  const std::string& level,
142  const std::string& suffix)
143 {
144  namespace al = boost::algorithm;
145  for (const auto& correction: corrections) {
146  if (!al::starts_with(correction.first, campaign) ||
147  !al::contains (correction.first, type) ||
148  !al::contains (correction.first, level) ||
149  !al::ends_with (correction.first, suffix))
150  continue;
151 
152  return correction.second;
153  }
154  using namespace std;
155  BOOST_THROW_EXCEPTION(
156  invalid_argument(
157  "No `"s + campaign + "*"s + type + "*" + level + "*"s + suffix
158  + "` correction can be found in the given tables. "s
159  + ScanCorrections(corrections)
160  )
161  );
162 }
163 
172 float Evaluate (const auto& correction,
173  std::ostream& cout,
174  const RecJet& recJet,
175  const std::optional<float>& rho = {},
176  const std::optional<GenJet>& genJet = {},
177  const std::optional<RecEvent>& recEvt = {},
178  const std::optional<std::string>& systematic = {},
179  const std::optional<float>& JER = {},
180  const std::optional<float>& JERSF = {}
181  )
182 try {
183  using namespace std;
184 
185  cout << correction->name();
186  vector<correction::Variable::Type> inputs;
187  inputs.reserve(10);
188 
189  auto push_back = [&cout,&inputs](const auto& v) {
190  inputs.push_back(v);
191  cout << '=' << v;
192  };
193 
194  for (const correction::Variable& input: correction->inputs()) {
195  const string& n = input.name();
196  cout << ' ' << n;
197  if (n == "JetPt" ) push_back(recJet.CorrPt());
198  else if (n == "JetEta" ) push_back(recJet.p4.Eta());
199  else if (n == "JetPhi" ) push_back(recJet.p4.Phi());
200  else if (n == "JetEta" ) push_back(recJet.p4.Phi());
201  else if (n == "JetA" ) push_back(recJet.area);
202  else if (n == "Rho" ) push_back(*rho);
203  else if (n == "systematic") push_back(*systematic);
204  else if (n == "GenPt" ) push_back(genJet ? genJet->CorrPt() : -1);
205  else if (n == "EventID" ) push_back((int) recEvt->evtNo);
206  else if (n == "run" ) push_back((float) recEvt->runNo);
207  else if (n == "JER" ) push_back(*JER);
208  else if (n == "JERSF" ) push_back(*JERSF);
209  else BOOST_THROW_EXCEPTION( invalid_argument("`"s + n + "` is needed by "s
210  + correction->name() + " but not recognized."s) );
214  }
215  auto corr = correction->evaluate(inputs);
216  cout << " -> " << corr << endl;
217  return corr;
218 }
219 catch (std::runtime_error& e) {
220  const char * name = typeid(*correction).name();
222  auto location = boost::source_location(__FILE__, __LINE__, name);
223  boost::throw_exception(e, location);
224 }
225 
228 int GetR (const Darwin::Tools::UserInfo& metainfo)
229 {
230  auto year = metainfo.Get<int>("flags", "year"),
231  R = metainfo.Get<int>("flags", "R");
232 
233  std::string warning = "Not a standard jet size.\n";
234  if (year > 2014) { // run 2 and run 3
235  if (R != 4 && R != 8)
236  std::cerr << orange << warning << def;
237  return R < 6 ? 4 : 8;
238  }
239  else { // run 1
240  if (R != 5 && R != 7)
241  std::cerr << orange << warning << def;
242  return R < 6 ? 5 : 7;
243  }
244 }
245 
246 } // end of DAS::JetEnergy namespace
DYToLL_M-50_13TeV_pythia8_cff_GEN_SIM_RECOBEFMIX_DIGI_L1_DIGI2RAW_L1Reco_RECO.name
name
Definition: DYToLL_M-50_13TeV_pythia8_cff_GEN_SIM_RECOBEFMIX_DIGI_L1_DIGI2RAW_L1Reco_RECO.py:48
Ntupliser_cfg.cerr
cerr
Definition: Ntupliser_cfg.py:101
metPhiCorrectionExample.bins
bins
Definition: metPhiCorrectionExample.py:89
DAS::JetEnergy::rho_edges
static const std::map< int, std::vector< double > > rho_edges
Definition: common.h:31
DAS::JetEnergy::rhoBins
std::vector< TString > rhoBins(int year)
Definition: common.h:40
jmarExample.pt
pt
Definition: jmarExample.py:19
Step::def
static const char * def
Definition: Step.h:36
DAS::PhysicsObject::p4
FourVector p4
raw four-momentum directly after reconstruction
Definition: PhysicsObject.h:50
DAS::JetEnergy::Evaluate
float Evaluate(const auto &correction, std::ostream &cout, const RecJet &recJet, const std::optional< float > &rho={}, const std::optional< GenJet > &genJet={}, const std::optional< RecEvent > &recEvt={}, const std::optional< std::string > &systematic={}, const std::optional< float > &JER={}, const std::optional< float > &JERSF={})
Wrapper to evaluate scale factors with DAS objects from correctionlib with exceptions.
Definition: common.h:172
DAS::JetEnergy::eta_unc_edges
static const std::vector< double > eta_unc_edges
binning for JES uncertainties (taken from JES files)
Definition: common.h:48
Darwin::Tools::UserInfo::Get
T Get(TList *mother, const char *key) const
Definition: UserInfo.h:75
Ntupliser_cfg.year
int year
Definition: Ntupliser_cfg.py:63
DAS::MakeTitle
std::vector< TString > MakeTitle(const std::vector< double > &edges, const char *v, bool lowEdge, bool highEdge, std::function< const char *(double)> format)
Make a vector of properly formated titles from bin edges.
Definition: binnings.h:13
DAS::JetEnergy::pt_JERC_edges
static const std::vector< double > pt_JERC_edges
Definition: common.h:22
DAS::JetEnergy::ScanCorrections
std::string ScanCorrections(const auto &corrections, const std::string &key="")
Definition: common.h:121
Event.h
DAS::RecJet
Definition: Jet.h:37
jercExample.key
string key
Definition: jercExample.py:109
DAS::JetEnergy::getBinning
std::vector< double > getBinning(int nBins, float first, float last)
Definition: common.h:55
DAS::JetEnergy::w
static const float w
Definition: common.h:53
Jet.h
DAS::JetEnergy::GetShortCampaign
std::string GetShortCampaign(const std::string &campaign)
Extracts for isntance Summer19UL18 from Summer19UL18_RunA
Definition: common.h:108
DAS::JetEnergy::absetaBins
static const std::vector< TString > absetaBins
Definition: common.h:29
DAS::MN::minpt
static const double minpt
Definition: getMNobservables.cc:39
DAS::JetEnergy::JES_variations
static const std::vector< std::string > JES_variations
Definition: common.h:51
binnings.h
DAS::JetEnergy::assertValidBinning
void assertValidBinning(const std::vector< double > &v)
Definition: common.h:63
DAS::JetEnergy::GetNRhoBins
int GetNRhoBins(int year)
Definition: common.h:39
DAS::MN::maxpt
static const double maxpt
Definition: getMNobservables.cc:39
orange
static const char * orange
Definition: colours.h:6
DYToLL_M-50_13TeV_pythia8_cff_GEN_SIM_RECOBEFMIX_DIGI_L1_DIGI2RAW_L1Reco_RECO.input
input
Definition: DYToLL_M-50_13TeV_pythia8_cff_GEN_SIM_RECOBEFMIX_DIGI_L1_DIGI2RAW_L1Reco_RECO.py:35
Darwin::Tools::UserInfo::Find
bool Find(TList *mother, const char *key) const
Recursive finder.
Definition: UserInfo.h:107
DAS::PhysicsObject::CorrPt
float CorrPt(size_t i) const
corrected transverse momentum
Definition: PhysicsObject.h:55
DAS::JetEnergy::nEtaUncBins
static const int nEtaUncBins
Definition: common.h:49
DAS::JetEnergy::abseta_edges
static const std::vector< double > abseta_edges
JERC binning (taken from JERCProtoLab repo, /macros/common_info/common_binning.hpp)
Definition: common.h:27
DAS::RecJet::area
float area
Jet area (should be peaked at pi*R^2), used for the JES corrections.
Definition: Jet.h:42
Step::green
static const char * green
Definition: Step.h:33
DAS::JetEnergy::GetR
int GetR(const Darwin::Tools::UserInfo &metainfo)
Determine the R values for which JetMET provides corrections.
Definition: common.h:228
DAS::JetEnergy::GetCorrection
CorrectionType GetCorrection(const auto &corrections, const std::string &campaign, const std::string &type, const std::string &level, const std::string &suffix)
Returns the correction corresponding to a key.
Definition: common.h:138
DAS::JetEnergy::GetLogBinning
std::vector< float > GetLogBinning(float minpt, float maxpt, int nbins)
Make equidistant binning in log pt.
Definition: common.h:74
jercExample.inputs
def inputs
Definition: jercExample.py:118
DAS::JetEnergy::nPtJERCbins
static const int nPtJERCbins
Definition: common.h:23
generate_html.campaign
campaign
Definition: generate_html.py:86
Darwin::Tools::UserInfo
Generic meta-information for n-tuple (can be used out of Darwin).
Definition: UserInfo.h:52
DAS::JetEnergy::GetAlgo
std::string GetAlgo(const Darwin::Tools::UserInfo &metainfo)
Determine from metainfo if CHS or PUPPI has been used to reconstruct the jets.
Definition: common.h:92
DAS::JetEnergy
Definition: applyJERsmearing.cc:41
Ntupliser_cfg.rho
rho
Definition: Ntupliser_cfg.py:320
DAS::JetEnergy::nAbsEtaBins
static const int nAbsEtaBins
Definition: common.h:28
Step::bold
static const char * bold
Definition: Step.h:35