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 StatusCode TrigFastCalibWithRings::checkRings(const EventContext& ctx ) const {
33  ATH_CHECK(rgCont.isValid());
34  const xAOD::TrigRingerRings_v2 *ring=rgCont->at(0);
35 
36 
37  if(!ring->emCluster()){
38  ATH_MSG_WARNING("There is no link to emCluster.");
39  return StatusCode::FAILURE;
40  }
41 
42  return StatusCode::SUCCESS;
43 
44 }
45 
46 
48 
49 
50 
51  std::unique_ptr<TFile> f(TFile::Open(fileName.c_str()));
52  if (!f || f->IsZombie()) {
53  ATH_MSG_FATAL("Could not open " << fileName);
54  return StatusCode::FAILURE;
55  }
56 
57  // Load hPoly
58  TH2Poly *hPoly = nullptr;
59  f->GetObject("hPoly", hPoly);
60  if (!hPoly) {
61  ATH_MSG_FATAL("Could not find hPoly");
62  return StatusCode::FAILURE;
63  }
64  //pass ownership to class variable
65  m_hPoly.reset(static_cast<TH2Poly*>(hPoly));
66  m_hPoly->SetDirectory(nullptr);
67 
68  // Load variables
69  TObjArray *variablesTmp = nullptr;
70  f->GetObject("variables", variablesTmp);
71  if (!variablesTmp) {
72  ATH_MSG_FATAL("Could not find variables");
73  return StatusCode::FAILURE;
74  }
75  auto variables = std::unique_ptr<TObjArray>(variablesTmp);
76  variables->SetOwner(); // to delete the objects when d-tor is called
77 
78  // Load shifts
79  TObjArray *shiftsTmp = nullptr;
80  f->GetObject("shifts", shiftsTmp);
81  if (!shiftsTmp) {
82  ATH_MSG_FATAL("Could not find shifts");
83  return StatusCode::FAILURE;
84  }
85  auto shifts = std::unique_ptr<TObjArray>(shiftsTmp);
86  shifts->SetOwner(); // to delete the objects when d-tor is called
87 
88  // Load trees
89  TObjArray *treesTmp = nullptr;
90  //std::unique_ptr<TObjArray> trees;
91  TObjArray *trees = nullptr;
92  f->GetObject("trees", treesTmp);
93  if (treesTmp) {
94  trees = treesTmp;
95  trees->SetOwner(); // to delete the objects when d-tor is called
96  ATH_MSG_DEBUG("setupBDT " << "BDTs read from TObjArray");
97  } else {
98  ATH_MSG_DEBUG("setupBDT " << "Reading trees individually");
99  trees = new TObjArray();
100  trees->SetOwner(); // to delete the objects when d-tor is called
101  for (int i = 0; i < variables->GetEntries(); ++i)
102  {
103  TTree *tree = nullptr;
104  f->GetObject(Form("BDT%d", i), tree);
105  if (tree) tree->SetCacheSize(0);
106  trees->AddAtAndExpand(tree, i);
107  }
108  }
109 
110  // Ensure the objects have (the same number of) entries
111  if (!trees->GetEntries() || !(trees->GetEntries() == variables->GetEntries())) {
112  ATH_MSG_FATAL("Tree has size " << trees->GetEntries()
113  << " while variables has size " << variables->GetEntries());
114  return StatusCode::FAILURE;
115  }
116 
117  // Loop simultaneously over trees, variables and shifts
118  // Define the BDTs, the list of variables and the shift for each BDT
119  TObjString *str2;
120 
121  TTree *tree;
122  TIter nextTree(trees);
123  TIter nextVariables(variables.get());
124  TIter nextShift(shifts.get());
125  for (int i=0; (tree = (TTree*) nextTree()) && ((TObjString*) nextVariables()); ++i)
126  {
127  m_BDTs.emplace_back(tree);
128 
129  std::vector<std::function<float(const xAOD::Egamma*, const xAOD::CaloCluster*)> > funcs;
130  // Loop over variables, which are separated by comma
131  char separator_var = ';';
132  if (getString(variables->At(i)).Index(";") < 1) separator_var = ','; // old versions
133  std::unique_ptr<TObjArray> tokens(getString(variables->At(i)).Tokenize(separator_var));
134  TIter nextVar(tokens.get());
135  while ((str2 = (TObjString*) nextVar()))
136  {
137  const TString& varName = getString(str2);
138  if (!varName.Length()) {
139  ATH_MSG_FATAL("There was an empty variable name!");
140  return StatusCode::FAILURE;
141  }
142  }
143  }
144 return StatusCode::SUCCESS;
145 }
146 
147 
148 const TString& TrigFastCalibWithRings::getString(TObject* obj)
149 {
150  TObjString *objS = dynamic_cast<TObjString*>(obj);
151  if (!objS) {
152  throw std::runtime_error("egammaMVACalibTool::getString was passed something that was not a string object");
153  }
154  return objS->GetString();
155 }
156 
157 
158 
159 float TrigFastCalibWithRings::makeCalibWRings(const EventContext& ctx) const {
161  const xAOD::TrigRingerRings_v2 *ring=rgCont->at(0);
162 
163 
164  //Open the EventContext and create a BDT input vector: Rings + Et + eta
165  float eta_cluster=ring->emCluster()->eta();
166  float et_cluster=ring->emCluster()->et();
167  const static std::vector<float>rings=rgCont->at(0)->rings();
168 
169  //Define the Rings to be used as inputs
170  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};
171 
172  if (!(static_cast<int>(ring ->size()) > inputRingsIndex.back())){
173  throw std::runtime_error("The last ring index is bigger than the ring's lenght");
174  }
175 
176  std::vector<float>ringsInput;
177  for(auto index:inputRingsIndex)ringsInput.push_back(rings[index]);
178 
179  const TH2Poly* hPoly = m_hPoly.get();
180  const int bin = hPoly->FindFixBin(eta_cluster, et_cluster/Gaudi::Units::GeV) - 1; // poly bins are shifted by one
181 
182  ATH_MSG_DEBUG("Using bin: " << bin);
183 
184  if (bin < 0) {
185  ATH_MSG_DEBUG("The bin is under/overflow; just return the energy");
186  return et_cluster;
187  }
188 
189  if (bin >= static_cast<int>(m_BDTs.size())) {
190  ATH_MSG_WARNING("The bin is outside the range, so just return the energy");
191  return et_cluster;
192  }
193 
194  // select the bdt and functions. (shifts are done later if needed)
195  // if there is only one BDT just use that
196  const int bin_BDT = m_BDTs.size() != 1 ? bin : 0;
197  const auto& bdt = m_BDTs[bin_BDT];
198 
199  // evaluate the BDT response
200  const float mvaOutput = bdt.GetResponse(ringsInput);
201 
202 
203 
204  return et_cluster*mvaOutput;
205 }
python.CaloRecoConfig.f
f
Definition: CaloRecoConfig.py:127
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:159
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:92
PixelAthClusterMonAlgCfg.varName
string varName
end cluster ToT and charge
Definition: PixelAthClusterMonAlgCfg.py:117
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
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
TrigFastCalibWithRings::getString
static const TString & getString(TObject *obj)
Definition: TrigFastCalibWithRings.cxx:148
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
TrigFastCalibWithRings::checkRings
StatusCode checkRings(const EventContext &ctx) const
Definition: TrigFastCalibWithRings.cxx:31
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:135
GeV
#define GeV
Definition: CaloTransverseBalanceVecMon.cxx:30
readCCLHist.float
float
Definition: readCCLHist.py:83
TrigFastCalibWithRings::setupBDTFastCalo
StatusCode setupBDTFastCalo(const std::string &fileName)
Definition: TrigFastCalibWithRings.cxx:47
TrigFastCalibWithRings::m_CalibPath
Gaudi::Property< std::string > m_CalibPath
Definition: TrigFastCalibWithRings.h:41
TrigFastCalibWithRings::execute
StatusCode execute() const
Definition: TrigFastCalibWithRings.cxx:26