ATLAS Offline Software
Loading...
Searching...
No Matches
BPhysBlindingTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2018 CERN for the benefit of the ATLAS collaboration
3*/
4
5// system include:
7#include <set>
8#include <cmath>
9
10// EDM includes:
12
13// ROOT includes
14#include "TString.h"
15
16// Local include(s):
18
19namespace xAOD {
20
21 //--------------------------------------------------------------------------
22 // Constructor
23 //--------------------------------------------------------------------------
25 : asg::AsgTool( name ),
26 m_vtxContainer(nullptr), m_vtxAuxContainer(nullptr),
36
37#ifdef ASGTOOL_ATHENA
38 declareInterface< IBPhysBlindingTool >( this );
39#endif // ASGTOOL_ATHENA
40
41 // Vertex container
42 declareProperty("VertexContainerName", m_vertexContainerName = "");
43
44 // List of variables to blind
45 // (as concatenated string using . as delimiter)
46 declareProperty("VarToBlindNames", m_varToBlindNames = "");
47
48 // Flag to indicate candidates for blinding
49 // Left empty: Blind values for all candidates.
50 declareProperty("BlindingFlag" , m_blindingFlag = "");
51
52 // Offsets applied to values before blinding
53 // List must have same length as VarToBlindNames or zero.
54 declareProperty("BlindingOffsets", m_vOffsets);
55
56 // Scale factors applied before blinding
57 // List must have same length as VarToBlindNames or zero.
58 declareProperty("BlindingFactors", m_vFactors);
59
60 // Flip signs to negative range?
61 declareProperty("NegativeSigns" , m_vNegSigns);
62
63 // Key for blinding
64 declareProperty("BlindingKey" , m_blindKey = "");
65
66 // Key for unblinding
67 declareProperty("UnblindingKey" , m_unblindKey = "");
68
69 }
70 //--------------------------------------------------------------------------
72
73 // Greet the user:
74 ATH_MSG_DEBUG( "Initializing xAOD::BPhysBlindingTool" );
75
76 // Setup of variables
77 if ( m_vertexContainerName == "" ) {
78 ATH_MSG_INFO("No vertex container name provided.");
79 }
80
81 if ( m_varToBlindNames != "" ) {
83 }
84
85 // Blinding and unblinding keys
86 if ( m_blindKey == "" && m_unblindKey == "" ) {
87 ATH_MSG_ERROR("You must at least set a key for blinding or unblinding!");
88 } else {
89 if ( m_blindKey != "" ) {
90 m_senc.setPubKey(m_blindKey);
91 ATH_MSG_INFO("Setting blinding key.");
92 }
93 if ( m_unblindKey != "" ) {
94 m_senc.setPrivKey(m_unblindKey);
95 ATH_MSG_INFO("Setting unblinding key.");
96 }
97 }
98
99 // make sure offsets vector is of correct length
100 if ( m_vOffsets.size() < m_vVarNames.size() ) {
101 for (uint i=m_vOffsets.size(); i<m_vVarNames.size(); ++i) {
102 m_vOffsets.push_back(0.);
103 }
104 ATH_MSG_INFO("Extending BlindingOffsets list ...");
105 } else if ( m_vOffsets.size() > m_vVarNames.size() ) {
106 ATH_MSG_WARNING("BlindingOffsets list longer than VarToBlindNames.");
107 }
108
109 // make sure scale factors vector is of correct length
110 if ( m_vFactors.size() < m_vVarNames.size() ) {
111 for (uint i=m_vFactors.size(); i<m_vVarNames.size(); ++i) {
112 m_vFactors.push_back(1.);
113 }
114 ATH_MSG_INFO("Extending BlindingOffsets list ...");
115 } else if ( m_vFactors.size() > m_vVarNames.size() ) {
116 ATH_MSG_WARNING("BlindingFactors list longer than VarToBlindNames.");
117 }
118
119 // make sure negative signs vector is of correct length
120 if ( m_vNegSigns.size() < m_vVarNames.size() ) {
121 for (uint i=m_vNegSigns.size(); i<m_vVarNames.size(); ++i) {
122 m_vNegSigns.push_back(1.);
123 }
124 ATH_MSG_INFO("Extending NegativeSigns list ...");
125 } else if ( m_vNegSigns.size() > m_vVarNames.size() ) {
126 ATH_MSG_WARNING("NegativeSigns list longer than VarToBlindNames.");
127 }
128
129 // some info for the job log
130 ATH_MSG_INFO("VertexContainerName : " << m_vertexContainerName);
131 ATH_MSG_INFO("BlindingFlag : " << m_blindingFlag);
132 ATH_MSG_INFO("VarToBlindNames : " << m_varToBlindNames);
133 ATH_MSG_INFO("BlindingOffsets : " << vecToString(m_vOffsets));
134 ATH_MSG_INFO("BlindingFactors : " << vecToString(m_vFactors));
135 ATH_MSG_INFO("NegativeSigns : " << vecToString(m_vNegSigns));
136 ATH_MSG_INFO("BlindingKey : " << m_blindKey);
137 ATH_MSG_INFO("UnblindingKey : " << m_unblindKey);
138
139 // Return gracefully:
140 return StatusCode::SUCCESS;
141 }
142 //--------------------------------------------------------------------------
144
145 ATH_MSG_DEBUG( "Finalizing xAOD::BPhysBlindingTool" );
146
147 ATH_MSG_INFO("Statistics for " << name() << ":");
148 ATH_MSG_INFO(Form("N_eventsForBlindingSeen : %10ld",
150 ATH_MSG_INFO(Form("N_eventsBlinded : %10ld",
152 ATH_MSG_INFO(Form("N_eventsForUnblindingSeen : %10ld",
154 ATH_MSG_INFO(Form("N_eventsUnblinded : %10ld",
156 ATH_MSG_INFO(Form("N_candidatesForBlindingSeen : %10ld",
158 ATH_MSG_INFO(Form("N_candidatesBlinded : %10ld",
160 ATH_MSG_INFO(Form("N_candidatesForUnblindingSeen : %10ld",
162 ATH_MSG_INFO(Form("N_candidatesUnblinded : %10ld",
164
165 // Return gracefully:
166 return StatusCode::SUCCESS;
167 }
168 //--------------------------------------------------------------------------
169 // Simply blind one positive float value
170 //--------------------------------------------------------------------------
171 float BPhysBlindingTool::doBlind(const float& val) {
172
173 return m_senc.encrypt(val);
174 }
175 //--------------------------------------------------------------------------
176 // Simply unblind one positive float value
177 //--------------------------------------------------------------------------
178 float BPhysBlindingTool::doUnblind(const float& val) {
179
180 return m_senc.decrypt(val);
181 }
182 //--------------------------------------------------------------------------
183 // Simply blind one (positive) float value
184 //--------------------------------------------------------------------------
185 float BPhysBlindingTool::doBlind(const float& val,
186 const bool& negativeSign,
187 const float& offset,
188 const float& factor) {
189
190 // adjustment if requested
191 float bval(val);
192 float cval = val*factor + offset;
193 if ( cval > 0. ) {
194 // perform actual blinding
195 bval = m_senc.encrypt(cval);
196 if (negativeSign) bval *= -1.;
197 } else {
198 ATH_MSG_WARNING("Blinding: Corrected value not positive: "
199 << val << Form(" (%a) -> ", val)
200 << cval << Form(" (%a)", cval));
201 } // if cval > 0
202
203 return bval;
204 }
205 //--------------------------------------------------------------------------
206 // Simply unblind one (positive) float value
207 //--------------------------------------------------------------------------
208 float BPhysBlindingTool::doUnblind(const float& val,
209 const bool& negativeSign,
210 const float& offset,
211 const float& factor) {
212
213 float bval(val), cval(val);
214 if (negativeSign) bval *= -1.;
215 // if ( bval > 0. || isnan(bval) ) {
216 if ( bval > 0. || !std::isnormal(bval) ) {
217 // perform actual unblinding
218 cval = m_senc.decrypt(bval);
219 if ( factor != 0. ) {
220 cval = (cval - offset)/factor;
221 } else {
222 ATH_MSG_WARNING("Unblinding: BlindingFactor == 0!: "
223 << val << Form(" (%a)", val));
224 } // if m_vFactors[ivtx] != 0
225 } else {
226 ATH_MSG_WARNING("Unblinding: Corrected value not positive: "
227 << val << Form(" (%a) -> ", val)
228 << bval << Form(" (%a)", bval));
229 } // if bval > 0
230
231 return cval;
232 }
233 //--------------------------------------------------------------------------
234 // Perform blinding of requested variables
235 //--------------------------------------------------------------------------
237
238 if ( m_blindKey == "" ) {
239 ATH_MSG_WARNING("Can not blind without blinding key!");
240 } else {
241 ATH_CHECK( doBlindingAction(false) );
242 }
243
244 // Return gracefully:
245 return StatusCode::SUCCESS;
246 }
247 //--------------------------------------------------------------------------
248 // Perform unblinding of requested variables
249 //--------------------------------------------------------------------------
251
252 if ( m_unblindKey == "" ) {
253 ATH_MSG_WARNING("Can not unblind without unblinding key!");
254 } else {
256 }
257
258 // Return gracefully:
259 return StatusCode::SUCCESS;
260 }
261 //--------------------------------------------------------------------------
262
263 //--------------------------------------------------------------------------
264 // Protected methods
265 //--------------------------------------------------------------------------
266
267 //--------------------------------------------------------------------------
268 // Perform blinding or unblinding action
269 //--------------------------------------------------------------------------
270 StatusCode BPhysBlindingTool::doBlindingAction(bool unblind) {
271
273
274 // counters
275 if ( unblind ) {
277 } else {
279 }
280
281 if ( m_vVarNames.size() > 0 ) {
282 long candidatesBlinded(0);
283 long candidatesUnblinded(0);
284 // loop over vertices
285 // int ivtx(0);
287 vtxItr = m_vtxContainer->begin();
288 vtxItr != m_vtxContainer->end(); ++vtxItr) {
289 // counters
290 if ( unblind ) {
292 } else {
294 }
295 const xAOD::Vertex* vtx = *vtxItr;
296 // check whether to apply (un-)blinding to this candidate
297 if ( m_blindingFlag == "" || pass(*vtx, m_blindingFlag) ) {
298 // counters
299 if ( unblind ) {
300 ++candidatesUnblinded;
301 } else {
302 ++candidatesBlinded;
303 }
304 // loop over variable names
305 for (size_t iv=0; iv<m_vVarNames.size(); ++iv) {
306 SG::AuxElement::Decorator<float> floatDec(m_vVarNames[iv]);
307 // check for variable
308 if ( floatDec.isAvailable(*vtx) ) {
309 float val = floatDec(*vtx);
310 if ( unblind ) {
311 // unblinding
312 floatDec(*vtx) = doUnblind(val, m_vNegSigns[iv],
313 m_vOffsets[iv], m_vFactors[iv]);
314 ATH_MSG_DEBUG("Unblind: " << val << Form(" (%a) -> ", val)
315 << floatDec(*vtx)
316 << Form(" (%a)", floatDec(*vtx)));
317 } else {
318 // blinding
319 floatDec(*vtx) = doBlind(val, m_vNegSigns[iv],
320 m_vOffsets[iv], m_vFactors[iv]);
321 ATH_MSG_DEBUG("Blind: " << val << Form(" (%a) -> ", val)
322 << floatDec(*vtx)
323 << Form(" (%a)", floatDec(*vtx)));
324 } // if unblind
325 } else {
326 ATH_MSG_WARNING("Missing variable " << m_vVarNames[iv]);
327 } // if isAvailable
328 } // for m_vVarNames
329 } // if blinding
330 } // for iv
331 // counters
332 if ( unblind ) {
333 m_candidatesUnblinded += candidatesUnblinded;
334 if ( candidatesUnblinded > 0 ) ++m_eventsUnblinded;
335 } else {
336 m_candidatesBlinded += candidatesBlinded;
337 if ( candidatesBlinded > 0 ) ++m_eventsBlinded;
338 }
339 } // if m_vVarNames.size()
340
341 // Return gracefully:
342 return StatusCode::SUCCESS;
343 }
344 //--------------------------------------------------------------------------
345 // Cache current event.
346 //
347 // Call this once per event.
348 // Repeated calls for the same run/event are not updating the cache again.
349 //--------------------------------------------------------------------------
351
352 ATH_MSG_DEBUG("BPhysBlindingTool::cacheEvent -- begin");
353
354 const xAOD::EventInfo* eventInfo = NULL;
355 ATH_CHECK(evtStore()->retrieve(eventInfo, "EventInfo"));
356
357 if ( m_cachedRun != (int)eventInfo->runNumber() ||
358 m_cachedEvent != (int)eventInfo->eventNumber() ) {
359
360 // note update
361 m_cachedRun = eventInfo->runNumber();
362 m_cachedEvent = eventInfo->eventNumber();
363
364 ATH_MSG_DEBUG("BPhysBlindingTool::cacheEvent: caching now: "
365 << "run " << m_cachedRun << " event " << m_cachedEvent);
366
367 // retrieve vertices container
368 m_vtxContainer = nullptr;
369 m_vtxAuxContainer = nullptr;
370
371 if ( evtStore()->transientContains<xAOD::VertexContainer>(m_vertexContainerName) ) {
372 ATH_MSG_DEBUG("In transient store: " << m_vertexContainerName);
376 m_vertexContainerName+"Aux."));
377 } else {
378 ATH_MSG_DEBUG("Not in transient store: " << m_vertexContainerName);
379 const xAOD::VertexContainer* constVtxContainer = nullptr;
380 const xAOD::VertexAuxContainer* constVtxAuxContainer = nullptr;
381 ATH_CHECK(evtStore()->retrieve(constVtxContainer,
383 ATH_CHECK(evtStore()->retrieve(constVtxAuxContainer,
384 m_vertexContainerName+"Aux."));
385 // create a copy
389 for (const xAOD::Vertex* constVtx : *constVtxContainer) {
390 xAOD::Vertex* vtx = new xAOD::Vertex();
391 m_vtxContainer->push_back(vtx);
392 *vtx = *constVtx;
393 }
397 m_vertexContainerName+"Aux."));
398 }
399
400 ATH_MSG_DEBUG("Found vertex collection with key "
402
403 } // if new run/event
404
405 ATH_MSG_DEBUG("BPhysBlindingTool::cacheEvent -- end");
406
407 // Return gracefully:
408 return StatusCode::SUCCESS;
409 }
410 //--------------------------------------------------------------------------
411 // Helper to check whether an element is marked as passing a specific
412 // hypothesis.
413 //--------------------------------------------------------------------------
414 bool BPhysBlindingTool::pass(const SG::AuxElement& em, std::string hypo) {
415
416 if ( !hypo.starts_with( "passed_") )
417 hypo = "passed_" + hypo;
418 SG::AuxElement::Accessor<Char_t> flagAcc(hypo);
419 return flagAcc.isAvailable(em) && flagAcc(em) != 0;
420 }
421 //--------------------------------------------------------------------------
422 // Tokenize a string using certain separators
423 //--------------------------------------------------------------------------
424 std::vector<std::string>
425 BPhysBlindingTool::getTokens(std::string input, std::string seperators) {
426
427 return CxxUtils::tokenize(input, seperators);
428 }
429 //--------------------------------------------------------------------------
430 // Format vector of floats as string
431 //--------------------------------------------------------------------------
432 std::string BPhysBlindingTool::vecToString(const std::vector<float>& v)
433 const {
434 std::string str("[");
435 for (unsigned int i=0; i<v.size(); ++i) {
436 str += std::to_string(v[i]);
437 if ( i < v.size()-1 ) str += ",";
438 }
439 str += "]";
440 return str;
441 }
442 //--------------------------------------------------------------------------
443 // Format vector of bools as string
444 //--------------------------------------------------------------------------
445 std::string BPhysBlindingTool::vecToString(const std::vector<bool>& v)
446 const {
447 std::string str("[");
448 for (unsigned int i=0; i<v.size(); ++i) {
449 str += std::to_string(v[i]);
450 if ( i < v.size()-1 ) str += ",";
451 }
452 str += "]";
453 return str;
454 }
455 //--------------------------------------------------------------------------
456} // namespace xAOD
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
Dual-use tool for blinding and unblinding certain float values.
unsigned int uint
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
ServiceHandle< StoreGateSvc > & evtStore()
DataModel_detail::const_iterator< DataVector > const_iterator
Definition DataVector.h:838
AsgTool(const std::string &name)
Constructor specifying the tool instance's name.
Definition AsgTool.cxx:58
BPhysBlindingTool(const std::string &name="BPhysBlindingTool")
Declare the correct constructor for Athena.
std::vector< float > m_vFactors
Scale factors applied before blinding.
virtual StatusCode cacheEvent()
virtual StatusCode doBlindingAction(bool unblind=false)
std::vector< bool > m_vNegSigns
Flip signs to negative range?
std::string m_varToBlindNames
List of variables to blind.
virtual StatusCode doBlind() override
Perform blinding of requested variables.
virtual StatusCode doUnblind() override
Perform unblinding of requested variables.
std::vector< float > m_vOffsets
Offsets applied to values before blinding.
SimpleEncrypter m_senc
Instance of SimpleEncrypter.
std::string m_blindKey
Key for blinding.
std::vector< std::string > m_vVarNames
Vector of variable names.
virtual std::string vecToString(const std::vector< float > &v) const
Convert vector of floats to string.
xAOD::VertexContainer * m_vtxContainer
std::string m_blindingFlag
Flag to indicate candidates for blinding.
virtual std::vector< std::string > getTokens(std::string input, std::string seperators)
Tokenize a string using certain separators.
virtual StatusCode finalize() override
Method finalizing the tool.
virtual StatusCode initialize() override
Method initialising the tool.
xAOD::VertexAuxContainer * m_vtxAuxContainer
virtual bool pass(const SG::AuxElement &em, std::string hypo)
std::string m_unblindKey
Key for unblinding.
uint32_t runNumber() const
The current event's run number.
uint64_t eventNumber() const
The current event's event number.
std::vector< std::string > tokenize(std::string_view the_str, std::string_view delimiters)
Splits the string into smaller substrings.
AuxElement(SG::AuxVectorData *container, size_t index)
Base class for elements of a container that can have aux data.
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.
VertexAuxContainer_v1 VertexAuxContainer
Definition of the current jet auxiliary container.
EventInfo_v1 EventInfo
Definition of the latest event info version.
VertexContainer_v1 VertexContainer
Definition of the current "Vertex container version".
Vertex_v1 Vertex
Define the latest version of the vertex class.