ATLAS Offline Software
CalibrationDataUpdater.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // CalibrationDataUpdater.cxx, (c) ATLAS Detector software
8 
10 
11 #include "TObject.h"
12 #include "TDirectory.h"
13 #include "TFile.h"
14 
15 using std::string;
16 
17 //================ Constructor =================================================
18 
19 Analysis::CalibrationDataUpdater::CalibrationDataUpdater(const string& name, ISvcLocator* pSvcLocator)
20  :
21  AthAlgorithm(name,pSvcLocator)
22 {
23  // property declarations
24  declareProperty("inputRootFile", m_inputRootFile, "ROOT file containing new parametrisations");
25  declareProperty("DbRootFile", m_DbRootFile, "ROOT file to be entered in COOL");
26  declareProperty("paramNames", m_paramNames, "list of input and output parametrisation names");
27  declareProperty("overrideDefaults", m_overrideDefaults, "true if new results are to be used as default");
28 
29 }
30 
31 //================ Initialisation =================================================
32 
34 {
35  // Code entered here will be executed once at program start.
36 
37  ATH_MSG_INFO( name() << " initialize()" );
38 
39  // basic sanity and consistency checks
40 
41  TFile* fIn = 0;
42  bool newInput = (m_inputRootFile.empty());
43  if (newInput) {
44  fIn = new TFile(m_inputRootFile.c_str());
45  if (fIn->IsZombie()) {
46  ATH_MSG_FATAL( name() << ": requested input ROOT file invalid: "
47  << m_inputRootFile );
48  return StatusCode::FAILURE;
49  }
50  } else if (! m_overrideDefaults) {
51  ATH_MSG_FATAL( name() << ": no input ROOT file given and no defaults to be modified:"
52  << " no action to be taken" );
53  return StatusCode::FAILURE;
54  }
55  if (m_DbRootFile.empty()) {
56  ATH_MSG_FATAL( name() << ": no DB ROOT file given" );
57  return StatusCode::FAILURE;
58  }
59  TFile* fOut = new TFile(m_DbRootFile.c_str(), "UPDATE");
60  if (fOut->IsZombie()) {
61  ATH_MSG_FATAL( name() << ": DB ROOT file invalid: "
62  << m_DbRootFile );
63  return StatusCode::FAILURE;
64  }
65 
66  for (auto itname : m_paramNames){
67  string to = *itname;
68  // 1. determine whether to insert new results
69  if (newInput) {
70  // if no "->" is found, assume that the input and output object names are identical
71  const string::size_type delim = itname->find("->");
72  string from = (delim == string::npos) ? *itname : itname->substr(0, delim);
73  if (delim != string::npos) to = itname->substr(delim+2);
74  copyResults(from, to, fIn, fOut);
75  } else if (! isValidName(to)) {
76  ATH_MSG_WARNING( name() << ": invalid parametrisation name: " << to );
77  continue;
78  }
79  // 2. make these new results the default (if requested or if no default exists yet)
80  setDefaultResults(to, fOut);
81  }
82  ATH_MSG_DEBUG( name() << ": all done" );
83 
84  if (fIn) fIn->Close();
85  fOut->Close();
86 
87  ATH_MSG_DEBUG( "initialize() successful in " << name() );
88  return StatusCode::SUCCESS;
89 }
90 
92 {
93  fOut->cd("/");
94  TObject* obj = fOut->Get(Name.c_str());
95  if (! obj) {
96  ATH_MSG_WARNING( name() << ": nonexistent parametrisation name: " << Name );
97  return;
98  }
99 
100  string::size_type delim = Name.rfind("/");
101  string dir = Name.substr(0, delim);
102  TDirectory* newdir = fOut->GetDirectory(dir.c_str());
103  if (! newdir->cd()) {
104  // This should never happen, given that the object above apparently exists...
105  ATH_MSG_WARNING( name() << ": Name now: " << Name << "; pwd: "
106  << gDirectory->GetPath() << "; cd() to subdir "
107  << dir << " failed!" );
108  };
109 
110  // if no change of defaults is requested to begin with, check whether a default
111  // exists already
112  delim = Name.rfind("_");
113  string defaultName("default"); defaultName += Name.substr(delim);
114  if (! m_overrideDefaults) {
115  string fullName(dir); fullName += "/"; fullName += defaultName;
116  if (fOut->Get(fullName.c_str())) return;
117  }
118 
119  ATH_MSG_INFO( name() << ": copying parametrisation name: "
120  << Name << " (+ stat., syst.) as default" );
121 
122  // do the actual copying for the parametrisation itself
123  obj->Write(defaultName.c_str());
124 
125  // and for the parameter covariance matrix
126  string nameCov(Name); nameCov += "_stat";
127  string defaultNameCov(defaultName); defaultNameCov += "_stat";
128  fOut->Get(nameCov.c_str())->Write(defaultNameCov.c_str());
129 
130  // and for the systematic uncertainty
131  string nameSyst(Name); nameSyst += "_syst";
132  string defaultNameSyst(defaultName); defaultNameSyst += "_syst";
133  fOut->Get(nameSyst.c_str())->Write(defaultNameSyst.c_str());
134 }
135 
136 void Analysis::CalibrationDataUpdater::copyResults(const string& from, const string& to,
137  TFile* fIn, TFile* fOut) const
138 {
139  if (! isValidName(to)) {
140  ATH_MSG_WARNING( name() << ": invalid parametrisation name: " << to );
141  return;
142  }
143 
144  // copy the parametrisation itself
145  TObject* in = fIn->Get(from.c_str());
146  if (! in) {
147  ATH_MSG_WARNING( name() << ": input parametrisation name not found: "
148  << from );
149  return;
150  }
151  writeOutput(in, to, fOut);
152 
153  // copy the corresponding covariance matrix
154  std::string fromCov(from); fromCov += "_stat";
155  std::string toCov(to); toCov += "_stat";
156  in = fIn->Get(fromCov.c_str());
157  if (! in) {
158  ATH_MSG_WARNING( name() << ": input parametrisation covariance matrix name not found: "
159  << fromCov );
160  return;
161  }
162  writeOutput(in, toCov, fOut);
163 
164  // copy the corresponding covariance matrix
165  std::string fromSyst(from); fromSyst += "_syst";
166  std::string toSyst(to); toSyst += "_syst";
167  in = fIn->Get(fromSyst.c_str());
168  if (! in) {
169  ATH_MSG_WARNING( name() << ": input parametrisation systematics name not found: "
170  << fromSyst );
171  return;
172  }
173  writeOutput(in, toSyst, fOut);
174 
175 }
176 
178 {
179  // this is a very basic check: the name should contain four slashes
180  // and should end either in "_SF" or "_Eff"
181  int slashes = 0;
182  string::size_type delim = Name.find("/");
183  while (delim != string::npos) {
184  ++slashes;
185  Name = Name.substr(delim+1);
186  delim = Name.find("/");
187  }
188  if (slashes != 4) return false;
189 
190  delim = Name.rfind("_");
191  if (delim == string::npos) return false;
192  Name = Name.substr(delim+1);
193  return (Name == "SF" || Name == "Eff");
194 }
195 
196 void Analysis::CalibrationDataUpdater::writeOutput(TObject* in, const string& Name, TFile* fOut) const
197 {
198  // first check whether the output directory exists, and if not, create it
199  fOut->cd("/");
200  TDirectory* tdir = dynamic_cast<TDirectory*>(fOut);
201  string::size_type delim = Name.find("/");
202  while (delim != string::npos) {
203  string dir = Name.substr(0, delim);
204  TDirectory* newdir = tdir->GetDirectory(dir.c_str());
205  if (! newdir) newdir = tdir->mkdir(dir.c_str());
206  if (! newdir->cd()) {
207  ATH_MSG_WARNING( name() << ": Name now: " << Name << "; pwd: "
208  << gDirectory->GetPath() << "; cd() to subdir "
209  << dir << " failed!" );
210  };
211  tdir = newdir;
212  Name = Name.substr(delim+1, string::npos);
213  delim = Name.find("/");
214  }
215 
216  // then write the named object
217  ATH_MSG_INFO( name() << ": writing object " << Name << " to directory "
218  << gDirectory->GetPath() );
219  in->Write(Name.c_str());
220 }
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
Analysis::CalibrationDataUpdater::initialize
StatusCode initialize() override
standard Athena-Algorithm method
Definition: CalibrationDataUpdater.cxx:33
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
Analysis::CalibrationDataUpdater::CalibrationDataUpdater
CalibrationDataUpdater(const std::string &name, ISvcLocator *pSvcLocator)
Standard Athena-Algorithm Constructor.
Definition: CalibrationDataUpdater.cxx:19
Analysis::CalibrationDataUpdater::writeOutput
void writeOutput(TObject *in, const std::string &Name, TFile *fOut) const
Definition: CalibrationDataUpdater.cxx:196
WriteCellNoiseToCool.fullName
fullName
Definition: WriteCellNoiseToCool.py:461
Analysis::CalibrationDataUpdater::m_overrideDefaults
Gaudi::Property m_overrideDefaults
flag if these results are to be used as defaults (they will anyway if no other results exist for the ...
Definition: CalibrationDataUpdater.h:72
Analysis::CalibrationDataUpdater::m_DbRootFile
std::string m_DbRootFile
ROOT file to be entered in COOL.
Definition: CalibrationDataUpdater.h:64
Analysis::CalibrationDataUpdater::isValidName
bool isValidName(const std::string &Name) const
Definition: CalibrationDataUpdater.cxx:177
Analysis::CalibrationDataUpdater::setDefaultResults
void setDefaultResults(const std::string &Name, TFile *fOut) const
auxiliary functions
Definition: CalibrationDataUpdater.cxx:91
ParseInputs.gDirectory
gDirectory
Definition: Final2012/ParseInputs.py:133
Analysis::CalibrationDataUpdater::m_paramNames
std::vector< std::string > m_paramNames
names of input and output parametrisations
Definition: CalibrationDataUpdater.h:67
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
CalibrationDataUpdater.h
Analysis::CalibrationDataUpdater::m_inputRootFile
std::string m_inputRootFile
input ROOT file containing new results
Definition: CalibrationDataUpdater.h:61
makeTOC.fOut
fOut
Definition: makeTOC.py:37
AthAlgorithm
Definition: AthAlgorithm.h:47
beamspotman.dir
string dir
Definition: beamspotman.py:623
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
CxxUtils::to
CONT to(RANGE &&r)
Definition: ranges.h:32
Name
JetDumper::Name Name
Definition: JetDumper.cxx:19
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
python.PyAthena.obj
obj
Definition: PyAthena.py:135
MooRTT_summarizeCPU.fIn
fIn
Definition: MooRTT_summarizeCPU.py:11
Analysis::CalibrationDataUpdater::copyResults
void copyResults(const std::string &from, const std::string &to, TFile *fIn, TFile *fOut) const
Definition: CalibrationDataUpdater.cxx:136