DAS  3.0
Das Analysis System
toolbox.h
Go to the documentation of this file.
1 #include <exception>
2 #include <string>
3 
4 #include <boost/algorithm/string.hpp>
5 #include <boost/assert/source_location.hpp>
6 
7 #include <correction.h>
8 #include <darwin.h>
9 
10 namespace DAS::JetEnergy {
11 
14 inline std::string GetAlgo (const Darwin::Tools::UserInfo& metainfo)
15 {
16  if (metainfo.Find("flags", "labels", "CHS" )) return "chs";
17  if (metainfo.Find("flags", "labels", "PUPPI")) return "Puppi";
18 
19  std::cerr << orange << "Couldn't identify CHS or PUPPI. Running default "
20  "CHS for Run 2 (PUPPI foir Run 3)\n" << def;
21 
22  auto year = metainfo.Get<int>("flags", "year");
23  if (year > 2019) return "Puppi"; // run 3
24  if (year > 2014) return "chs"; // run 2
25  return ""; // run 1
26 }
27 
30 std::string GetShortCampaign (const std::string& campaign)
31 {
32  using namespace std;
33  regex r("^(Summer|Fall|Autumn|Winter|Spring)[0-9]{2}[A-Za-z0-9]*");
34  smatch campaign_short;
35  if (!regex_search(campaign, campaign_short, r))
36  BOOST_THROW_EXCEPTION( invalid_argument("The campaign could not be identified"));
37  return campaign_short.str();
38 }
39 
43 std::string ScanCorrections (const auto& corrections,
44  const std::string& key = "")
45 {
46  std::stringstream s;
47  s << "Available corrections:";
48  for (const auto& correction: corrections) {
49  bool found = correction.first == key;
50  if (found) s << bold << green;
51  s << ' ' << correction.first;
52  if (found) s << def;
53  }
54  return s.str();
55 }
56 
59 template<typename CorrectionType>
60 CorrectionType GetCorrection (const auto& corrections,
61  const std::string& campaign,
62  const std::string& type,
63  const std::string& level,
64  const std::string& suffix)
65 {
66  namespace al = boost::algorithm;
67  for (const auto& correction: corrections) {
68  if (!al::starts_with(correction.first, campaign) ||
69  !al::contains (correction.first, type) ||
70  !al::contains (correction.first, level) ||
71  !al::ends_with (correction.first, suffix))
72  continue;
73 
74  return correction.second;
75  }
76  using namespace std;
77  BOOST_THROW_EXCEPTION(
78  invalid_argument(
79  "No `"s + campaign + "*"s + type + "*" + level + "*"s + suffix
80  + "` correction can be found in the given tables. "s
81  + ScanCorrections(corrections)
82  )
83  );
84 }
85 
94 float Evaluate (const auto& correction,
95  std::ostream& cout,
96  const RecJet& recJet,
97  const std::optional<float>& rho = {},
98  const std::optional<GenJet>& genJet = {},
99  const std::optional<RecEvent>& recEvt = {},
100  const std::optional<std::string>& systematic = {},
101  const std::optional<float>& JER = {},
102  const std::optional<float>& JERSF = {}
103  )
104 try {
105  using namespace std;
106 
107  cout << correction->name();
108  vector<correction::Variable::Type> inputs;
109  inputs.reserve(10);
110 
111  auto push_back = [&cout,&inputs](const auto& v) {
112  inputs.push_back(v);
113  cout << '=' << v;
114  };
115 
116  for (const correction::Variable& input: correction->inputs()) {
117  const string& n = input.name();
118  cout << ' ' << n;
119  if (n == "JetPt" ) push_back(recJet.CorrPt());
120  else if (n == "JetEta" ) push_back(recJet.p4.Eta());
121  else if (n == "JetPhi" ) push_back(recJet.p4.Phi());
122  else if (n == "JetEta" ) push_back(recJet.p4.Phi());
123  else if (n == "JetA" ) push_back(recJet.area);
124  else if (n == "Rho" ) push_back(*rho);
125  else if (n == "systematic") push_back(*systematic);
126  else if (n == "GenPt" ) push_back(genJet ? genJet->CorrPt() : -1);
127  else if (n == "EventID" ) push_back((int) recEvt->evtNo);
128  else if (n == "run" ) push_back((float) recEvt->runNo);
129  else if (n == "JER" ) push_back(*JER);
130  else if (n == "JERSF" ) push_back(*JERSF);
131  else BOOST_THROW_EXCEPTION( invalid_argument("`"s + n + "` is needed by "s
132  + correction->name() + " but not recognized."s) );
136  }
137  auto corr = correction->evaluate(inputs);
138  cout << " -> " << corr << endl;
139  return corr;
140 }
141 catch (std::runtime_error& e) {
142  const char * name = typeid(*correction).name();
144  auto location = boost::source_location(__FILE__, __LINE__, name);
145  boost::throw_exception(e, location);
146 }
147 
150 int GetR (const Darwin::Tools::UserInfo& metainfo)
151 {
152  auto year = metainfo.Get<int>("flags", "year"),
153  R = metainfo.Get<int>("flags", "R");
154 
155  std::string warning = "Not a standard jet size.\n";
156  if (year > 2014) { // run 2 and run 3
157  if (R != 4 && R != 8)
158  std::cerr << orange << warning << def;
159  return R < 6 ? 4 : 8;
160  }
161  else { // run 1
162  if (R != 5 && R != 7)
163  std::cerr << orange << warning << def;
164  return R < 6 ? 5 : 7;
165  }
166 }
167 
168 } // 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:105
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: toolbox.h:94
Darwin::Tools::UserInfo::Get
T Get(TList *mother, const char *key) const
Definition: UserInfo.h:77
Ntupliser_cfg.year
int year
Definition: Ntupliser_cfg.py:67
DAS::JetEnergy::ScanCorrections
std::string ScanCorrections(const auto &corrections, const std::string &key="")
Definition: toolbox.h:43
DAS::RecJet
Definition: Jet.h:37
jercExample.key
string key
Definition: jercExample.py:109
DAS::JetEnergy::GetShortCampaign
std::string GetShortCampaign(const std::string &campaign)
Extracts for isntance Summer19UL18 from Summer19UL18_RunA
Definition: toolbox.h:30
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
Check if an element exists in a TList.
Definition: UserInfo.h:109
DAS::PhysicsObject::CorrPt
float CorrPt(size_t i) const
corrected transverse momentum
Definition: PhysicsObject.h:55
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: toolbox.h:150
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: toolbox.h:60
jercExample.inputs
def inputs
Definition: jercExample.py:118
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:54
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: toolbox.h:14
DAS::JetEnergy
Definition: applyJERsmearing.cc:42
Ntupliser_cfg.rho
rho
Definition: Ntupliser_cfg.py:333
Step::bold
static const char * bold
Definition: Step.h:35