ATLAS Offline Software
TrigFastCalibWithRings.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
6 
9 
10 TrigFastCalibWithRings::TrigFastCalibWithRings([[maybe_unused]] const std::string& type, const std::string& myname, [[maybe_unused]] const IInterface* parent):asg::AsgTool(myname){}
11 
12 
14 
16 
17  ATH_CHECK(m_ringerKey.initialize());
18 
19  //Setup the BDTs ...
21 
22 
23  return StatusCode::SUCCESS;
24 }
25 
27 
28  return StatusCode::SUCCESS;
29 }
30 
31 bool TrigFastCalibWithRings::checkRings(const EventContext& ctx ) const {
33  if (rgCont.isValid()){
34  ATH_MSG_DEBUG("No valid Ringer Container");
35  return false;
36  }
37  const xAOD::TrigRingerRings_v2 *ring=rgCont->at(0);
38  if(!ring->emCluster()){
39  ATH_MSG_DEBUG("There is no link to emCluster.");
40  return false;
41  }
42 
43  return true;
44 
45 }
46 
47 
49 
50 
51 
52  std::unique_ptr<TFile> f(TFile::Open(fileName.c_str()));
53  if (!f || f->IsZombie()) {
54  ATH_MSG_FATAL("Could not open " << fileName);
55  return StatusCode::FAILURE;
56  }
57 
58  // Load hPoly
59  TH2Poly *hPoly = nullptr;
60  f->GetObject("hPoly", hPoly);
61  if (!hPoly) {
62  ATH_MSG_FATAL("Could not find hPoly");
63  return StatusCode::FAILURE;
64  }
65  //pass ownership to class variable
66  m_hPoly.reset(static_cast<TH2Poly*>(hPoly));
67  m_hPoly->SetDirectory(nullptr);
68 
69  // Load variables
70  TObjArray *variablesTmp = nullptr;
71  f->GetObject("variables", variablesTmp);
72  if (!variablesTmp) {
73  ATH_MSG_FATAL("Could not find variables");
74  return StatusCode::FAILURE;
75  }
76  auto variables = std::unique_ptr<TObjArray>(variablesTmp);
77  variables->SetOwner(); // to delete the objects when d-tor is called
78 
79  // Load shifts
80  TObjArray *shiftsTmp = nullptr;
81  f->GetObject("shifts", shiftsTmp);
82  if (!shiftsTmp) {
83  ATH_MSG_FATAL("Could not find shifts");
84  return StatusCode::FAILURE;
85  }
86  auto shifts = std::unique_ptr<TObjArray>(shiftsTmp);
87  shifts->SetOwner(); // to delete the objects when d-tor is called
88 
89  // Load trees
90  TObjArray *treesTmp = nullptr;
91  //std::unique_ptr<TObjArray> trees;
92  TObjArray *trees = nullptr;
93  f->GetObject("trees", treesTmp);
94  if (treesTmp) {
95  trees = treesTmp;
96  trees->SetOwner(); // to delete the objects when d-tor is called
97  ATH_MSG_DEBUG("setupBDT " << "BDTs read from TObjArray");
98  } else {
99  ATH_MSG_DEBUG("setupBDT " << "Reading trees individually");
100  trees = new TObjArray();
101  trees->SetOwner(); // to delete the objects when d-tor is called
102  for (int i = 0; i < variables->GetEntries(); ++i)
103  {
104  TTree *tree = nullptr;
105  f->GetObject(Form("BDT%d", i), tree);
106  if (tree) tree->SetCacheSize(0);
107  trees->AddAtAndExpand(tree, i);
108  }
109  }
110 
111  // Ensure the objects have (the same number of) entries
112  if (!trees->GetEntries() || !(trees->GetEntries() == variables->GetEntries())) {
113  ATH_MSG_FATAL("Tree has size " << trees->GetEntries()
114  << " while variables has size " << variables->GetEntries());
115  return StatusCode::FAILURE;
116  }
117 
118  // Loop simultaneously over trees, variables and shifts
119  // Define the BDTs, the list of variables and the shift for each BDT
120  TObjString *str2;
121 
122  TTree *tree;
123  TIter nextTree(trees);
124  TIter nextVariables(variables.get());
125  TIter nextShift(shifts.get());
126  for (int i=0; (tree = (TTree*) nextTree()) && ((TObjString*) nextVariables()); ++i)
127  {
128  m_BDTs.emplace_back(tree);
129 
130  std::vector<std::function<float(const xAOD::Egamma*, const xAOD::CaloCluster*)> > funcs;
131  // Loop over variables, which are separated by comma
132  char separator_var = ';';
133  if (getString(variables->At(i)).Index(";") < 1) separator_var = ','; // old versions
134  std::unique_ptr<TObjArray> tokens(getString(variables->At(i)).Tokenize(separator_var));
135  TIter nextVar(tokens.get());
136  while ((str2 = (TObjString*) nextVar()))
137  {
138  const TString& varName = getString(str2);
139  if (!varName.Length()) {
140  ATH_MSG_FATAL("There was an empty variable name!");
141  return StatusCode::FAILURE;
142  }
143  }
144  }
145 return StatusCode::SUCCESS;
146 }
147 
148 
149 const TString& TrigFastCalibWithRings::getString(TObject* obj)
150 {
151  TObjString *objS = dynamic_cast<TObjString*>(obj);
152  if (!objS) {
153  throw std::runtime_error("egammaMVACalibTool::getString was passed something that was not a string object");
154  }
155  return objS->GetString();
156 }
157 
158 
159 
160 float TrigFastCalibWithRings::makeCalibWRings(const EventContext& ctx) const {
162  const xAOD::TrigRingerRings_v2 *ring=rgCont->at(0);
163 
164 
165  //Open the EventContext and create a BDT input vector: Rings + Et + eta
166  float eta_cluster=ring->emCluster()->eta();
167  float et_cluster=ring->emCluster()->et();
168  const static std::vector<float>rings=rgCont->at(0)->rings();
169 
170  //Define the Rings to be used as inputs
171  const std::vector<int>inputRingsIndex{0,1,2,3,8,9,10,11,12,13,14,15,72,73,74,75,76,77,78,79,81,82,83,84,88,89,90,91};
172 
173  if (!(static_cast<int>(ring ->size()) > inputRingsIndex.back())){
174  throw std::runtime_error("The last ring index is bigger than the ring's lenght");
175  }
176 
177  std::vector<float>ringsInput;
178  for(auto index:inputRingsIndex)ringsInput.push_back(rings[index]);
179 
180  const TH2Poly* hPoly = m_hPoly.get();
181  const int bin = hPoly->FindFixBin(eta_cluster, et_cluster/Gaudi::Units::GeV) - 1; // poly bins are shifted by one
182 
183  ATH_MSG_DEBUG("Using bin: " << bin);
184 
185  if (bin < 0) {
186  ATH_MSG_DEBUG("The bin is under/overflow; just return the energy");
187  return et_cluster;
188  }
189 
190  if (bin >= static_cast<int>(m_BDTs.size())) {
191  ATH_MSG_WARNING("The bin is outside the range, so just return the energy");
192  return et_cluster;
193  }
194 
195  // select the bdt and functions. (shifts are done later if needed)
196  // if there is only one BDT just use that
197  const int bin_BDT = m_BDTs.size() != 1 ? bin : 0;
198  const auto& bdt = m_BDTs[bin_BDT];
199 
200  // evaluate the BDT response
201  const float mvaOutput = bdt.GetResponse(ringsInput);
202 
203 
204 
205  return et_cluster*mvaOutput;
206 }
GeV
#define GeV
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/Root/HelperFunctions.cxx:17
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
xAOD::TrigEMCluster_v1::eta
float eta() const
get Eta (calibrated)
checkCoolLatestUpdate.variables
variables
Definition: checkCoolLatestUpdate.py:13
TrigFastCalibWithRings.h
TrigFastCalibWithRings::m_hPoly
std::unique_ptr< TH2Poly > m_hPoly
A TH2Poly used to extract bin numbers. Note there is an offset of 1.
Definition: TrigFastCalibWithRings.h:45
xAOD::TrigRingerRings_v2::emCluster
const TrigEMCluster * emCluster() const
The associated EM cluster, as a simple pointer.
Definition: TrigRingerRings_v2.cxx:41
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
index
Definition: index.py:1
RTTAlgmain.trees
list trees
Definition: RTTAlgmain.py:40
tree
TChain * tree
Definition: tile_monitor.h:30
asg
Definition: DataHandleTestTool.h:28
bin
Definition: BinsDiffFromStripMedian.h:43
xAOD::Egamma_v1
Definition: Egamma_v1.h:56
TrigFastCalibWithRings::makeCalibWRings
float makeCalibWRings(const EventContext &ctx) const
Definition: TrigFastCalibWithRings.cxx:160
beamspotman.tokens
tokens
Definition: beamspotman.py:1284
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
xAOD::CaloCluster_v1
Description of a calorimeter cluster.
Definition: CaloCluster_v1.h:59
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
lumiFormat.i
int i
Definition: lumiFormat.py:85
PixelAthClusterMonAlgCfg.varName
string varName
end cluster ToT and charge
Definition: PixelAthClusterMonAlgCfg.py:125
TrigFastCalibWithRings::m_BDTs
std::vector< MVAUtils::BDT > m_BDTs
Where the BDTs are stored.
Definition: TrigFastCalibWithRings.h:48
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
TrigFastCalibWithRings::m_ringerKey
SG::ReadHandleKey< xAOD::TrigRingerRingsContainer > m_ringerKey
Definition: TrigFastCalibWithRings.h:43
MessageCheck.h
macros for messaging and checking status codes
test_pyathena.parent
parent
Definition: test_pyathena.py:15
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
hist_file_dump.f
f
Definition: hist_file_dump.py:135
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
TrigFastCalibWithRings::checkRings
bool checkRings(const EventContext &ctx) const
Definition: TrigFastCalibWithRings.cxx:31
TrigFastCalibWithRings::getString
static const TString & getString(TObject *obj)
Definition: TrigFastCalibWithRings.cxx:149
TrigFastCalibWithRings::TrigFastCalibWithRings
TrigFastCalibWithRings(const std::string &type, const std::string &myname, const IInterface *parent)
Definition: TrigFastCalibWithRings.cxx:10
plotBeamSpotVxVal.bin
int bin
Definition: plotBeamSpotVxVal.py:83
xAOD::TrigRingerRings_v2
Definition: TrigRingerRings_v2.h:24
PathResolverFindCalibFile
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:431
TrigFastCalibWithRings::initialize
StatusCode initialize() final
Dummy implementation of the initialisation function.
Definition: TrigFastCalibWithRings.cxx:15
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
TrigFastCalibWithRings::~TrigFastCalibWithRings
~TrigFastCalibWithRings()
Definition: TrigFastCalibWithRings.cxx:13
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
xAOD::TrigEMCluster_v1::et
float et() const
get Et (calibrated)
DataVector::at
const T * at(size_type n) const
Access an element, as an rvalue.
python.PyAthena.obj
obj
Definition: PyAthena.py:132
readCCLHist.float
float
Definition: readCCLHist.py:83
TrigFastCalibWithRings::setupBDTFastCalo
StatusCode setupBDTFastCalo(const std::string &fileName)
Definition: TrigFastCalibWithRings.cxx:48
TrigFastCalibWithRings::m_CalibPath
Gaudi::Property< std::string > m_CalibPath
Definition: TrigFastCalibWithRings.h:41
TrigFastCalibWithRings::execute
StatusCode execute() const
Definition: TrigFastCalibWithRings.cxx:26