ATLAS Offline Software
TrackSystematicsMaker.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3  */
4 
6 
7 #include <limits>
8 
10 
13 
17 
18 #include "TopEvent/EventTools.h"
21 
27 
28 // Other includes:
29 #include "xAODCore/ShallowCopy.h"
30 
31 namespace top {
32  /* explicit */
34  : ::asg::AsgTool(name),
35  m_config(nullptr),
36  m_runPeriods(),
37  m_specifiedSystematics(),
38  m_recommendedSystematics(),
39  m_nominalSystematicSet(),
40  m_smearingTool("top::TrackCPTools::InDetTrackSmearingTool"),
41  m_truthOriginTool("top::TrackCPTools::InDetTrackTruthOriginTool"),
42  m_truthFilterTool("top::TrackCPTools::InDetTrackTruthFilterTool")
43  {
44  declareProperty("config", m_config);
45  }
46 
48  }
49 
51  ATH_MSG_INFO(" top::TrackSystematicsMaker initialize");
52 
53  ATH_MSG_WARNING("Usage of tracks in AnalysisTop is experimental for now!\n");
54 
55  m_runPeriods = m_config->runPeriodsTrack();
56 
58 
59  // Retrieve the systematics tools. Each individual function will cause
60  // program abortion upon failure. This is intended as there's no
61  // meaningful method of recovery.
62  // These tools are setup and initialised in the TopCPTools package, and we just retrieve them here.
63  top::check(retrieveTrackCPTool(), "Failed to retrieve the track CP tools");
64 
65  // Setup the recommended systematics. This utilises the tools that were
66  // setup before.
67  const std:: string& syststr = m_config->systematics();
68  std::set<std::string> syst;
69 
70  if (m_config->isSystNominal(syststr)) {
71 
73 
74 
75  } else if (m_config->isSystAll(syststr)) {
76 
77  specifiedSystematics({"AllTracks"});
78 
79  } else {
80 
81  std::set<std::string> syst;
82  bool ok = m_config->getSystematicsList(syststr, syst);
83  if (!ok) {
84  ATH_MSG_ERROR(" top::TrackSystematicsMaker could not determine systematic list");
85  return StatusCode::FAILURE;
86  }
87 
88 
89  // No need to manually handle the AllTrack case in here.
91  }
92 
93 
94  // Pass the systematics list back to the top::TopConfig object.
95  m_config->systematicsTracks(specifiedSystematics());
96 
97  ATH_MSG_INFO(" top::TrackSystematicsMaker completed initialize");
98  return StatusCode::SUCCESS;
99  }
100 
101 
102 
104  const CP::SystematicSet& syst) const {
105 
106  if(m_config->sgKeyTracks(syst.hash()) == "InDetTrackParticles_")
107  return StatusCode::SUCCESS;
108 
109  // Create a shallow copy of InDetTrackParticles:
110  const xAOD::TrackParticleContainer *tp(nullptr);
111  top::check(evtStore()->retrieve(tp, "InDetTrackParticles"), "Retrieve InDetParticles");
112 
113  std::pair< xAOD::TrackParticleContainer*, xAOD::ShallowAuxContainer* > shallow_xaod_copy = xAOD::shallowCopyContainer( *tp );
114 
116  top::check(tool->applySystematicVariation(syst),
117  "Failed to configure tool for systematic variation");
118 
119 
120 
121  for ( xAOD::TrackParticle* trkCpy : *(shallow_xaod_copy.first) ) {
122 
123 
124  if (not tool->accept(trkCpy)) trkCpy->auxdecor<char>("passTruthFilter") = 0;
125  else trkCpy->auxdecor<char>("passTruthFilter") = 1;
126 
127 
128  }
129 
131  std::string outputSGKey = m_config->sgKeyTracks(syst.hash());
132  std::string outputSGKeyAux = outputSGKey + "Aux.";
133 
134 
135  StatusCode save = evtStore()->tds()->record(shallow_xaod_copy.first, outputSGKey);
136  StatusCode saveAux = evtStore()->tds()->record(shallow_xaod_copy.second, outputSGKeyAux);
137 
138  if (!save || !saveAux) {
139  return StatusCode::FAILURE;
140  }
141 
142  return StatusCode::SUCCESS;
143  }
144 
145 
147  const CP::SystematicSet& syst) const {
148 
149 
150  if(m_config->sgKeyTracks(syst.hash()) == "InDetTrackParticles_")
151  return StatusCode::SUCCESS;
152 
153  // Create a shallow copy of InDetTrackParticles:
154  const xAOD::TrackParticleContainer *tp(nullptr);
155  top::check(evtStore()->retrieve(tp, "InDetTrackParticles"), "Retrieve InDetParticles");
156 
157  std::pair< xAOD::TrackParticleContainer*, xAOD::ShallowAuxContainer* > shallow_xaod_copy = xAOD::shallowCopyContainer( *tp );
158 
160  top::check(tool->applySystematicVariation(syst), "Failed to configure tool for systematic variation");
161 
162 
163  for ( xAOD::TrackParticle* trkCpy : *(shallow_xaod_copy.first) ) {
164 
165  top::check(tool->applyCorrection(*trkCpy), "Failure to apply track systematic");
166 
167  }
168 
170  std::string outputSGKey = m_config->sgKeyTracks(syst.hash());
171  std::string outputSGKeyAux = outputSGKey + "Aux.";
172 
173 
174  StatusCode save = evtStore()->tds()->record(shallow_xaod_copy.first, outputSGKey);
175  StatusCode saveAux = evtStore()->tds()->record(shallow_xaod_copy.second, outputSGKeyAux);
176 
177  if (!save || !saveAux) {
178  return StatusCode::FAILURE;
179  }
180 
181  return StatusCode::SUCCESS;
182  }
183 
185  const CP::SystematicSet& syst) const {
186 
187  if(m_config->sgKeyTracks(syst.hash()) == "InDetTrackParticles_")
188  return StatusCode::SUCCESS;
189 
190 
191  // Create a shallow copy of InDetTrackParticles:
192  const xAOD::TrackParticleContainer *tp(nullptr);
193  top::check(evtStore()->retrieve(tp, "InDetTrackParticles"), "Retrieve InDetParticles");
194 
195  std::pair< xAOD::TrackParticleContainer*, xAOD::ShallowAuxContainer* > shallow_xaod_copy = xAOD::shallowCopyContainer( *tp );
196 
198  top::check(tool->applySystematicVariation(syst),
199  "Failed to configure tool for systematic variation");
200 
201  for ( xAOD::TrackParticle* trkCpy : *(shallow_xaod_copy.first) ) {
202 
203  top::check(tool->applyCorrection(*trkCpy), "Failure to apply track systematic");
204 
205  }
206 
208  std::string outputSGKey = m_config->sgKeyTracks(syst.hash());
209  std::string outputSGKeyAux = outputSGKey + "Aux.";
210 
211  StatusCode save = evtStore()->tds()->record(shallow_xaod_copy.first, outputSGKey);
212  StatusCode saveAux = evtStore()->tds()->record(shallow_xaod_copy.second, outputSGKeyAux);
213 
214  if (!save || !saveAux) {
215  return StatusCode::FAILURE;
216  }
217 
218  return StatusCode::SUCCESS;
219  }
220 
222  ATH_MSG_DEBUG(" top::TrackSystematicsMaker execute:");
223 
224 
225  if (!m_config->isMC() && executeNominal) {
226 
227  // Create a shallow copy of InDetTrackParticles:
228  const xAOD::TrackParticleContainer *tp(nullptr);
229  top::check(evtStore()->retrieve(tp, "InDetTrackParticles"), "Retrieve InDetParticles");
230 
231  std::pair< xAOD::TrackParticleContainer*, xAOD::ShallowAuxContainer* > shallow_xaod_copy = xAOD::shallowCopyContainer( *tp );
232 
234  std::string outputSGKey = m_config->sgKeyTracks(m_nominalSystematicSet.hash());
235  std::string outputSGKeyAux = outputSGKey + "Aux.";
236 
237  StatusCode save = evtStore()->tds()->record(shallow_xaod_copy.first, outputSGKey);
238  StatusCode saveAux = evtStore()->tds()->record(shallow_xaod_copy.second, outputSGKeyAux);
239 
240  if (!save || !saveAux) {
241  return StatusCode::FAILURE;
242  }
243 
244 
245  return StatusCode::SUCCESS;
246  }
247  if (!m_config->isMC() && !executeNominal) {
248  return StatusCode::SUCCESS;
249  }
250 
251 
253  if (executeNominal){
254 
255  // Create a shallow copy of InDetTrackParticles:
256  const xAOD::TrackParticleContainer *tp(nullptr);
257  top::check(evtStore()->retrieve(tp, "InDetTrackParticles"), "Retrieve InDetParticles");
258 
259  std::pair< xAOD::TrackParticleContainer*, xAOD::ShallowAuxContainer* > shallow_xaod_copy = xAOD::shallowCopyContainer( *tp );
260 
262  std::string outputSGKey = m_config->sgKeyTracks(m_nominalSystematicSet.hash());
263  std::string outputSGKeyAux = outputSGKey + "Aux.";
264 
265  StatusCode save = evtStore()->tds()->record(shallow_xaod_copy.first, outputSGKey);
266  StatusCode saveAux = evtStore()->tds()->record(shallow_xaod_copy.second, outputSGKeyAux);
267 
268  if (!save || !saveAux) {
269  return StatusCode::FAILURE;
270  }
271 
272 
273  return StatusCode::SUCCESS;
274 
275  }
276 
278  for (const auto& syst : m_systs.smearing) {
280  "Failure to apply TrackSystematic");
281  }
282 
284  InDet::InDetTrackBiasingTool* biasingTool {
285  nullptr
286  };
287 
288  if (m_runPeriods.size() == 1) {
289  biasingTool = &(*m_biasTool[0]);
290 
291  top::check(biasingTool, "Failure to selected biasing tool");
292  for (const auto& syst : m_systs.bias) {
293  top::check(applyBiasingSystematic(biasingTool, syst),
294  "Failure to apply TrackSystematic");
295  }
296  }
297  else {
298  const xAOD::EventInfo* ei {
299  nullptr
300  };
301  top::check(evtStore()->retrieve(ei, "EventInfo"),
302  "Failure to retrieve EventInfo");
303 
304  top::check(ei->isAvailable<unsigned int>("RandomRunNumber"),
305  "Require that RandomRunNumber decoration is available.");
306  auto randomRunNumber = ei->auxdataConst<unsigned int>("RandomRunNumber");
307 
308  if (randomRunNumber != 0 ){
309  top::check(m_runPeriods[0] <= randomRunNumber,
310  "RandomRunNumber is below valid range.");
311  top::check(randomRunNumber < m_runPeriods[m_runPeriods.size() - 1],
312  "RandomRunNumber is above valid range.");
313 
314  for (std::size_t i = 1; i < m_runPeriods.size(); ++i) {
315  if (randomRunNumber < m_runPeriods[i]) {
316  biasingTool = &(*m_biasTool[i - 1]);
317  break;
318  }
319  }
320  top::check(biasingTool, "Failure to selected biasing tool");
321  for (const auto& syst : m_systs.bias) {
322  top::check(applyBiasingSystematic(biasingTool, syst),
323  "Failure to apply TrackSystematic");
324  }
325  }
326  }
327 
329  for (const auto& syst : m_systs.truthFilter) {
330 
332  "Failure to apply TrackSystematic");
333  }
334 
335  ATH_MSG_DEBUG(" top::TrackSystematicsMaker completed execute");
336  return StatusCode::SUCCESS;
337  }
338 
339  void TrackSystematicsMaker::specifiedSystematics(const std::set<std::string>& specSys) {
340 
341  // NOTE: For this function -- unlike the "proper" ObjectCollectionMakers in AnalysisTop -- an empty
342  // specifiedSystematics input is considered to represent "No Systematics". All Track systematics can be
343  // requested with the string "AllTrack".
344  const bool allTrackSystematics = m_config->contains(specSys, "AllTracks");
345 
346  // Acquire the recommended systematics from the various tools and put
347  // into containers that store the per-tool systematics as well as a
348  // global container. At a later point we'll make the per-tool containers
349  // unique and check that the containers for various tools are
350  // orthogonal.
351  std::set<CP::SystematicSet> systs;
352 
353  // SMEARING
354  m_systs.smearing = CP::make_systematics_vector(m_smearingTool->recommendedSystematics());
355  systs.insert(m_systs.smearing.begin(), m_systs.smearing.end());
356 
357  // BIAS
358  for (const auto& tool : m_biasTool) {
359  m_systs.bias =
360  CP::make_systematics_vector(tool->recommendedSystematics());
361  systs.insert(m_systs.bias.begin(), m_systs.bias.end());
362  }
363 
364  // TRUTH FILTER
365  m_systs.truthFilter =
366  CP::make_systematics_vector(m_truthFilterTool->recommendedSystematics());
367  systs.insert(m_systs.truthFilter.begin(), m_systs.truthFilter.end());
368 
369  // PUT INTO RECOMMENDED AND SPECIFIED
370  for (const auto& s : systs) {
371 
372  // We don't want nominal.
373  if (s.name() == "") {
374  continue;
375  }
376 
377  m_recommendedSystematics.push_back(s);
378 
380  if (m_config->isMC()) {
382  if (!m_config->isSystNominal(m_config->systematics())) {
383  if (allTrackSystematics) {
384 
385  m_specifiedSystematics.push_back(s);
386  continue;
387  }
388 
389  for (const auto& i : specSys) {
391  if (!filter.filterTree(s.name())) {
392  m_specifiedSystematics.push_back(s);
393  }
394  }
395  }
396  }
397  }
398 
400  m_recommendedSystematics.unique();
401 
402  m_specifiedSystematics.sort();
403  m_specifiedSystematics.unique();
404 
405  // Make the sublists unique and remove any elements that have not "been specified".
406  m_systs.makeUnique();
407  m_systs.removeNonSpecified(m_specifiedSystematics);
408 
409  // Lastly, let use introduce a safeguard which ensures that the
410  // systematics selected for the various tools are orthogonal. This is
411  // done by asserting that the sum of the sizes of the individual
412  // systematics lists equals the size of the list of specified
413  // systematics. We can do this because we've made the individual
414  // per-tool containers unique (in makeUnique).
415  if(m_specifiedSystematics.size() > 1)
416  top::check(systs.size() == m_specifiedSystematics.size(), "Sanity check failed");
417  else
418  top::check(m_specifiedSystematics.size() == 1, "Sanity check failed");
419 
420 
421  }
422 
424  ATH_MSG_INFO(" Retrieving track CPTools");
425 
426 
427  top::check(m_smearingTool.retrieve(), "Failed to retrieve track smearing tool");
428 
429  std::string biasToolPrefix {"top::TrackCPTools::InDetTrackBiasingTool"};
430  top::check(not m_runPeriods.empty(), "Assertion failed");
431  // Two cases are possible:
432  // - Either a single run number was specified to the runPeriods
433  // parameter in which case we'll use exactly that run number, or
434  // - at least two numbers have been specified, which then define
435  // (potentially multiple) run number ranges.
436  unsigned int end = 0;
437  if (m_runPeriods.size() == 1) {
438  end = 1;
439  m_biasTool.resize(1);
440  } else {
441  end = m_runPeriods.size() - 1;
442  m_biasTool.resize(m_runPeriods.size() - 1);
443  }
444 
445  for (unsigned int i = 0; i < end; i++) {
446 
447 
448  std::string biasToolName {
449  ""
450  };
451  if (m_runPeriods.size() == 1) {
452  biasToolName = biasToolPrefix + "_" + std::to_string(m_runPeriods[0]);
453  }
454  else {
455  biasToolName = biasToolPrefix + "_" + std::to_string(m_runPeriods[i]) + "_" +
457  }
458 
459 
460  if (asg::ToolStore::contains<InDet::InDetTrackBiasingTool>(biasToolName)) {
461  m_biasTool[i] = asg::ToolStore::get<InDet::InDetTrackBiasingTool>(biasToolName);
462  } else {
463  ATH_MSG_ERROR(" Impossible to retrieve " + biasToolName);
464  return StatusCode::FAILURE;
465  }
466 
467 
468 
469  }
470 
471  top::check(m_truthOriginTool.retrieve(), "Failed to retrieve track truth origin tool");
472  top::check(m_truthFilterTool.retrieve(), "Failed to retrieve track truth filter tool");
473 
474 
475  return StatusCode::SUCCESS;
476  }
477 
478 
479  inline const std::list<CP::SystematicSet>& TrackSystematicsMaker::specifiedSystematics() const {
480  return m_specifiedSystematics;
481  }
482 
483  inline const std::list<CP::SystematicSet>& TrackSystematicsMaker::recommendedSystematics() const {
485  }
486 
487 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
ShallowCopy.h
InDetTrackTruthFilterTool.h
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
top::TrackSystematicsMaker::m_recommendedSystematics
std::list< CP::SystematicSet > m_recommendedSystematics
Definition: TrackSystematicsMaker.h:100
top
TopConfig A simple configuration that is NOT a singleton.
Definition: AnalysisTrackingHelper.cxx:58
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
top::TrackSystematicsMaker::m_systs
struct top::TrackSystematicsMaker::@221 m_systs
CP::make_systematics_vector
std::vector< CP::SystematicSet > make_systematics_vector(const SystematicSet &systematics)
utility functions for working with systematics
Definition: SystematicsUtil.cxx:25
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
TreeFilter.h
asg
Definition: DataHandleTestTool.h:28
CP::SystematicSet
Class to wrap a set of SystematicVariations.
Definition: SystematicSet.h:31
ParticleTest.tp
tp
Definition: ParticleTest.py:25
top::TrackSystematicsMaker::m_specifiedSystematics
std::list< CP::SystematicSet > m_specifiedSystematics
Definition: TrackSystematicsMaker.h:99
InDet::InDetTrackSmearingTool
Definition: InDetTrackSmearingTool.h:32
top::TrackSystematicsMaker::applySmearingSystematic
StatusCode applySmearingSystematic(InDet::InDetTrackSmearingTool *tool, const CP::SystematicSet &syst) const
Definition: TrackSystematicsMaker.cxx:146
top::TrackSystematicsMaker::m_biasTool
std::vector< ToolHandle< InDet::InDetTrackBiasingTool > > m_biasTool
Definition: TrackSystematicsMaker.h:145
EventTools.h
A few functions for doing operations on particles / events. Currently holds code for dR,...
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
covarianceTool.filter
filter
Definition: covarianceTool.py:514
top::TreeFilter
Definition: TreeFilter.h:13
AthCommonDataStore< AthCommonMsg< AlgTool > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
CP::SystematicSet::hash
std::size_t hash() const
returns: hash value for the joined string.
Definition: SystematicSet.cxx:289
top::TrackSystematicsMaker::m_nominalSystematicSet
CP::SystematicSet m_nominalSystematicSet
Definition: TrackSystematicsMaker.h:102
checkTP.save
def save(self, fileName="./columbo.out")
Definition: checkTP.py:178
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
top::TrackSystematicsMaker::m_truthOriginTool
ToolHandle< InDet::InDetTrackTruthOriginTool > m_truthOriginTool
Definition: TrackSystematicsMaker.h:146
TrackParticleAuxContainer.h
lumiFormat.i
int i
Definition: lumiFormat.py:92
top::TrackSystematicsMaker::applyTruthFilterSystematic
StatusCode applyTruthFilterSystematic(InDet::InDetTrackTruthFilterTool *tool, const CP::SystematicSet &syst) const
Definition: TrackSystematicsMaker.cxx:103
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
top::TrackSystematicsMaker::m_smearingTool
ToolHandle< InDet::InDetTrackSmearingTool > m_smearingTool
Definition: TrackSystematicsMaker.h:144
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
top::TrackSystematicsMaker::retrieveTrackCPTool
StatusCode retrieveTrackCPTool()
Definition: TrackSystematicsMaker.cxx:423
InDet::InDetTrackBiasingTool
Definition: InDetTrackBiasingTool.h:36
top::check
void check(bool thingToCheck, const std::string &usefulFailureMessage)
Print an error message and terminate if thingToCheck is false.
Definition: EventTools.cxx:15
top::TrackSystematicsMaker::recommendedSystematics
const std::list< CP::SystematicSet > & recommendedSystematics() const
Definition: TrackSystematicsMaker.cxx:483
InDetTrackBiasingTool.h
DataVector< xAOD::TrackParticle_v1 >
top::TrackSystematicsMaker::initialize
StatusCode initialize()
Dummy implementation of the initialisation function.
Definition: TrackSystematicsMaker.cxx:50
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
top::TrackSystematicsMaker::~TrackSystematicsMaker
virtual ~TrackSystematicsMaker()
Definition: TrackSystematicsMaker.cxx:47
AtlCoolConsole.tool
tool
Definition: AtlCoolConsole.py:453
top::TrackSystematicsMaker::applyBiasingSystematic
StatusCode applyBiasingSystematic(InDet::InDetTrackBiasingTool *tool, const CP::SystematicSet &syst) const
Definition: TrackSystematicsMaker.cxx:184
InDet::InDetTrackTruthFilterTool
Definition: InDetTrackTruthFilterTool.h:36
TopConfig.h
xAOD::shallowCopyContainer
std::pair< std::unique_ptr< T >, std::unique_ptr< ShallowAuxContainer > > shallowCopyContainer(const T &cont, [[maybe_unused]] const EventContext &ctx)
Function making a shallow copy of a constant container.
Definition: ShallowCopy.h:110
EventInfo.h
xAOD::EventInfo_v1
Class describing the basic event information.
Definition: EventInfo_v1.h:43
InDetTrackTruthOriginTool.h
InDetTrackSystematics.h
TrackParticle.h
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
top::TrackSystematicsMaker::execute
StatusCode execute(bool)
Definition: TrackSystematicsMaker.cxx:221
InDetTrackSmearingTool.h
top::TrackSystematicsMaker::m_truthFilterTool
ToolHandle< InDet::InDetTrackTruthFilterTool > m_truthFilterTool
Definition: TrackSystematicsMaker.h:147
IParticleHelpers.h
top::TrackSystematicsMaker::specifiedSystematics
const std::list< CP::SystematicSet > & specifiedSystematics() const
Definition: TrackSystematicsMaker.cxx:479
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
TrackSystematicsMaker.h
top::TrackSystematicsMaker::TrackSystematicsMaker
TrackSystematicsMaker(const std::string &name)
Definition: TrackSystematicsMaker.cxx:33
TrackParticleContainer.h
top::TrackSystematicsMaker::m_config
std::shared_ptr< top::TopConfig > m_config
Definition: TrackSystematicsMaker.h:96
SystematicsUtil.h
top::TrackSystematicsMaker::m_runPeriods
std::vector< std::uint32_t > m_runPeriods
Definition: TrackSystematicsMaker.h:97