ATLAS Offline Software
Loading...
Searching...
No Matches
CalibrationDataUpdater.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 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
15using std::string;
16
17//================ Constructor =================================================
18
19Analysis::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 (const std::string& 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
91void Analysis::CalibrationDataUpdater::setDefaultResults(const string& Name, TFile* fOut) const
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
136void 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
196void 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}
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
JetDumper::Name Name
Definition JetDumper.cxx:19
StatusCode initialize() override
standard Athena-Algorithm method
std::string m_inputRootFile
input ROOT file containing new results
void writeOutput(TObject *in, const std::string &Name, TFile *fOut) const
void copyResults(const std::string &from, const std::string &to, TFile *fIn, TFile *fOut) const
std::string m_DbRootFile
ROOT file to be entered in COOL.
Gaudi::Property m_overrideDefaults
flag if these results are to be used as defaults (they will anyway if no other results exist for the ...
bool isValidName(const std::string &Name) const
std::vector< std::string > m_paramNames
names of input and output parametrisations
CalibrationDataUpdater(const std::string &name, ISvcLocator *pSvcLocator)
Standard Athena-Algorithm Constructor.
void setDefaultResults(const std::string &Name, TFile *fOut) const
auxiliary functions
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)