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:
6#include "boost/tokenizer.hpp"
7#include <boost/algorithm/string.hpp>
8#include <set>
9#include <cmath>
10
11// EDM includes:
13
14// ROOT includes
15#include "TString.h"
16
17// Local include(s):
19
20namespace xAOD {
21
22 //--------------------------------------------------------------------------
23 // Constructor
24 //--------------------------------------------------------------------------
26 : asg::AsgTool( name ),
27 m_vtxContainer(nullptr), m_vtxAuxContainer(nullptr),
37
38#ifdef ASGTOOL_ATHENA
39 declareInterface< IBPhysBlindingTool >( this );
40#endif // ASGTOOL_ATHENA
41
42 // Vertex container
43 declareProperty("VertexContainerName", m_vertexContainerName = "");
44
45 // List of variables to blind
46 // (as concatenated string using . as delimiter)
47 declareProperty("VarToBlindNames", m_varToBlindNames = "");
48
49 // Flag to indicate candidates for blinding
50 // Left empty: Blind values for all candidates.
51 declareProperty("BlindingFlag" , m_blindingFlag = "");
52
53 // Offsets applied to values before blinding
54 // List must have same length as VarToBlindNames or zero.
55 declareProperty("BlindingOffsets", m_vOffsets);
56
57 // Scale factors applied before blinding
58 // List must have same length as VarToBlindNames or zero.
59 declareProperty("BlindingFactors", m_vFactors);
60
61 // Flip signs to negative range?
62 declareProperty("NegativeSigns" , m_vNegSigns);
63
64 // Key for blinding
65 declareProperty("BlindingKey" , m_blindKey = "");
66
67 // Key for unblinding
68 declareProperty("UnblindingKey" , m_unblindKey = "");
69
70 }
71 //--------------------------------------------------------------------------
73
74 // Greet the user:
75 ATH_MSG_DEBUG( "Initializing xAOD::BPhysBlindingTool" );
76
77 // Setup of variables
78 if ( m_vertexContainerName == "" ) {
79 ATH_MSG_INFO("No vertex container name provided.");
80 }
81
82 if ( m_varToBlindNames != "" ) {
84 }
85
86 // Blinding and unblinding keys
87 if ( m_blindKey == "" && m_unblindKey == "" ) {
88 ATH_MSG_ERROR("You must at least set a key for blinding or unblinding!");
89 } else {
90 if ( m_blindKey != "" ) {
91 m_senc.setPubKey(m_blindKey);
92 ATH_MSG_INFO("Setting blinding key.");
93 }
94 if ( m_unblindKey != "" ) {
95 m_senc.setPrivKey(m_unblindKey);
96 ATH_MSG_INFO("Setting unblinding key.");
97 }
98 }
99
100 // make sure offsets vector is of correct length
101 if ( m_vOffsets.size() < m_vVarNames.size() ) {
102 for (uint i=m_vOffsets.size(); i<m_vVarNames.size(); ++i) {
103 m_vOffsets.push_back(0.);
104 }
105 ATH_MSG_INFO("Extending BlindingOffsets list ...");
106 } else if ( m_vOffsets.size() > m_vVarNames.size() ) {
107 ATH_MSG_WARNING("BlindingOffsets list longer than VarToBlindNames.");
108 }
109
110 // make sure scale factors vector is of correct length
111 if ( m_vFactors.size() < m_vVarNames.size() ) {
112 for (uint i=m_vFactors.size(); i<m_vVarNames.size(); ++i) {
113 m_vFactors.push_back(1.);
114 }
115 ATH_MSG_INFO("Extending BlindingOffsets list ...");
116 } else if ( m_vFactors.size() > m_vVarNames.size() ) {
117 ATH_MSG_WARNING("BlindingFactors list longer than VarToBlindNames.");
118 }
119
120 // make sure negative signs vector is of correct length
121 if ( m_vNegSigns.size() < m_vVarNames.size() ) {
122 for (uint i=m_vNegSigns.size(); i<m_vVarNames.size(); ++i) {
123 m_vNegSigns.push_back(1.);
124 }
125 ATH_MSG_INFO("Extending NegativeSigns list ...");
126 } else if ( m_vNegSigns.size() > m_vVarNames.size() ) {
127 ATH_MSG_WARNING("NegativeSigns list longer than VarToBlindNames.");
128 }
129
130 // some info for the job log
131 ATH_MSG_INFO("VertexContainerName : " << m_vertexContainerName);
132 ATH_MSG_INFO("BlindingFlag : " << m_blindingFlag);
133 ATH_MSG_INFO("VarToBlindNames : " << m_varToBlindNames);
134 ATH_MSG_INFO("BlindingOffsets : " << vecToString(m_vOffsets));
135 ATH_MSG_INFO("BlindingFactors : " << vecToString(m_vFactors));
136 ATH_MSG_INFO("NegativeSigns : " << vecToString(m_vNegSigns));
137 ATH_MSG_INFO("BlindingKey : " << m_blindKey);
138 ATH_MSG_INFO("UnblindingKey : " << m_unblindKey);
139
140 // Return gracefully:
141 return StatusCode::SUCCESS;
142 }
143 //--------------------------------------------------------------------------
145
146 ATH_MSG_DEBUG( "Finalizing xAOD::BPhysBlindingTool" );
147
148 ATH_MSG_INFO("Statistics for " << name() << ":");
149 ATH_MSG_INFO(Form("N_eventsForBlindingSeen : %10ld",
151 ATH_MSG_INFO(Form("N_eventsBlinded : %10ld",
153 ATH_MSG_INFO(Form("N_eventsForUnblindingSeen : %10ld",
155 ATH_MSG_INFO(Form("N_eventsUnblinded : %10ld",
157 ATH_MSG_INFO(Form("N_candidatesForBlindingSeen : %10ld",
159 ATH_MSG_INFO(Form("N_candidatesBlinded : %10ld",
161 ATH_MSG_INFO(Form("N_candidatesForUnblindingSeen : %10ld",
163 ATH_MSG_INFO(Form("N_candidatesUnblinded : %10ld",
165
166 // Return gracefully:
167 return StatusCode::SUCCESS;
168 }
169 //--------------------------------------------------------------------------
170 // Simply blind one positive float value
171 //--------------------------------------------------------------------------
172 float BPhysBlindingTool::doBlind(const float& val) {
173
174 return m_senc.encrypt(val);
175 }
176 //--------------------------------------------------------------------------
177 // Simply unblind one positive float value
178 //--------------------------------------------------------------------------
179 float BPhysBlindingTool::doUnblind(const float& val) {
180
181 return m_senc.decrypt(val);
182 }
183 //--------------------------------------------------------------------------
184 // Simply blind one (positive) float value
185 //--------------------------------------------------------------------------
186 float BPhysBlindingTool::doBlind(const float& val,
187 const bool& negativeSign,
188 const float& offset,
189 const float& factor) {
190
191 // adjustment if requested
192 float bval(val);
193 float cval = val*factor + offset;
194 if ( cval > 0. ) {
195 // perform actual blinding
196 bval = m_senc.encrypt(cval);
197 if (negativeSign) bval *= -1.;
198 } else {
199 ATH_MSG_WARNING("Blinding: Corrected value not positive: "
200 << val << Form(" (%a) -> ", val)
201 << cval << Form(" (%a)", cval));
202 } // if cval > 0
203
204 return bval;
205 }
206 //--------------------------------------------------------------------------
207 // Simply unblind one (positive) float value
208 //--------------------------------------------------------------------------
209 float BPhysBlindingTool::doUnblind(const float& val,
210 const bool& negativeSign,
211 const float& offset,
212 const float& factor) {
213
214 float bval(val), cval(val);
215 if (negativeSign) bval *= -1.;
216 // if ( bval > 0. || isnan(bval) ) {
217 if ( bval > 0. || !std::isnormal(bval) ) {
218 // perform actual unblinding
219 cval = m_senc.decrypt(bval);
220 if ( factor != 0. ) {
221 cval = (cval - offset)/factor;
222 } else {
223 ATH_MSG_WARNING("Unblinding: BlindingFactor == 0!: "
224 << val << Form(" (%a)", val));
225 } // if m_vFactors[ivtx] != 0
226 } else {
227 ATH_MSG_WARNING("Unblinding: Corrected value not positive: "
228 << val << Form(" (%a) -> ", val)
229 << bval << Form(" (%a)", bval));
230 } // if bval > 0
231
232 return cval;
233 }
234 //--------------------------------------------------------------------------
235 // Perform blinding of requested variables
236 //--------------------------------------------------------------------------
238
239 if ( m_blindKey == "" ) {
240 ATH_MSG_WARNING("Can not blind without blinding key!");
241 } else {
242 ATH_CHECK( doBlindingAction(false) );
243 }
244
245 // Return gracefully:
246 return StatusCode::SUCCESS;
247 }
248 //--------------------------------------------------------------------------
249 // Perform unblinding of requested variables
250 //--------------------------------------------------------------------------
252
253 if ( m_unblindKey == "" ) {
254 ATH_MSG_WARNING("Can not unblind without unblinding key!");
255 } else {
257 }
258
259 // Return gracefully:
260 return StatusCode::SUCCESS;
261 }
262 //--------------------------------------------------------------------------
263
264 //--------------------------------------------------------------------------
265 // Protected methods
266 //--------------------------------------------------------------------------
267
268 //--------------------------------------------------------------------------
269 // Perform blinding or unblinding action
270 //--------------------------------------------------------------------------
271 StatusCode BPhysBlindingTool::doBlindingAction(bool unblind) {
272
274
275 // counters
276 if ( unblind ) {
278 } else {
280 }
281
282 if ( m_vVarNames.size() > 0 ) {
283 long candidatesBlinded(0);
284 long candidatesUnblinded(0);
285 // loop over vertices
286 // int ivtx(0);
288 vtxItr = m_vtxContainer->begin();
289 vtxItr != m_vtxContainer->end(); ++vtxItr) {
290 // counters
291 if ( unblind ) {
293 } else {
295 }
296 const xAOD::Vertex* vtx = *vtxItr;
297 // check whether to apply (un-)blinding to this candidate
298 if ( m_blindingFlag == "" || pass(*vtx, m_blindingFlag) ) {
299 // counters
300 if ( unblind ) {
301 ++candidatesUnblinded;
302 } else {
303 ++candidatesBlinded;
304 }
305 // loop over variable names
306 for (size_t iv=0; iv<m_vVarNames.size(); ++iv) {
308 // check for variable
309 if ( floatDec.isAvailable(*vtx) ) {
310 float val = floatDec(*vtx);
311 if ( unblind ) {
312 // unblinding
313 floatDec(*vtx) = doUnblind(val, m_vNegSigns[iv],
314 m_vOffsets[iv], m_vFactors[iv]);
315 ATH_MSG_DEBUG("Unblind: " << val << Form(" (%a) -> ", val)
316 << floatDec(*vtx)
317 << Form(" (%a)", floatDec(*vtx)));
318 } else {
319 // blinding
320 floatDec(*vtx) = doBlind(val, m_vNegSigns[iv],
321 m_vOffsets[iv], m_vFactors[iv]);
322 ATH_MSG_DEBUG("Blind: " << val << Form(" (%a) -> ", val)
323 << floatDec(*vtx)
324 << Form(" (%a)", floatDec(*vtx)));
325 } // if unblind
326 } else {
327 ATH_MSG_WARNING("Missing variable " << m_vVarNames[iv]);
328 } // if isAvailable
329 } // for m_vVarNames
330 } // if blinding
331 } // for iv
332 // counters
333 if ( unblind ) {
334 m_candidatesUnblinded += candidatesUnblinded;
335 if ( candidatesUnblinded > 0 ) ++m_eventsUnblinded;
336 } else {
337 m_candidatesBlinded += candidatesBlinded;
338 if ( candidatesBlinded > 0 ) ++m_eventsBlinded;
339 }
340 } // if m_vVarNames.size()
341
342 // Return gracefully:
343 return StatusCode::SUCCESS;
344 }
345 //--------------------------------------------------------------------------
346 // Cache current event.
347 //
348 // Call this once per event.
349 // Repeated calls for the same run/event are not updating the cache again.
350 //--------------------------------------------------------------------------
352
353 ATH_MSG_DEBUG("BPhysBlindingTool::cacheEvent -- begin");
354
355 const xAOD::EventInfo* eventInfo = NULL;
356 ATH_CHECK(evtStore()->retrieve(eventInfo, "EventInfo"));
357
358 if ( m_cachedRun != (int)eventInfo->runNumber() ||
359 m_cachedEvent != (int)eventInfo->eventNumber() ) {
360
361 // note update
362 m_cachedRun = eventInfo->runNumber();
363 m_cachedEvent = eventInfo->eventNumber();
364
365 ATH_MSG_DEBUG("BPhysBlindingTool::cacheEvent: caching now: "
366 << "run " << m_cachedRun << " event " << m_cachedEvent);
367
368 // retrieve vertices container
369 m_vtxContainer = nullptr;
370 m_vtxAuxContainer = nullptr;
371
372 if ( evtStore()->transientContains<xAOD::VertexContainer>(m_vertexContainerName) ) {
373 ATH_MSG_DEBUG("In transient store: " << m_vertexContainerName);
377 m_vertexContainerName+"Aux."));
378 } else {
379 ATH_MSG_DEBUG("Not in transient store: " << m_vertexContainerName);
380 const xAOD::VertexContainer* constVtxContainer = nullptr;
381 const xAOD::VertexAuxContainer* constVtxAuxContainer = nullptr;
382 ATH_CHECK(evtStore()->retrieve(constVtxContainer,
384 ATH_CHECK(evtStore()->retrieve(constVtxAuxContainer,
385 m_vertexContainerName+"Aux."));
386 // create a copy
390 for (const xAOD::Vertex* constVtx : *constVtxContainer) {
391 xAOD::Vertex* vtx = new xAOD::Vertex();
392 m_vtxContainer->push_back(vtx);
393 *vtx = *constVtx;
394 }
398 m_vertexContainerName+"Aux."));
399 }
400
401 ATH_MSG_DEBUG("Found vertex collection with key "
403
404 } // if new run/event
405
406 ATH_MSG_DEBUG("BPhysBlindingTool::cacheEvent -- end");
407
408 // Return gracefully:
409 return StatusCode::SUCCESS;
410 }
411 //--------------------------------------------------------------------------
412 // Helper to check whether an element is marked as passing a specific
413 // hypothesis.
414 //--------------------------------------------------------------------------
415 bool BPhysBlindingTool::pass(const SG::AuxElement& em, std::string hypo) {
416
417 if ( !hypo.starts_with( "passed_") )
418 hypo = "passed_" + hypo;
420 return flagAcc.isAvailable(em) && flagAcc(em) != 0;
421 }
422 //--------------------------------------------------------------------------
423 // Tokenize a string using certain separators
424 //--------------------------------------------------------------------------
425 std::vector<std::string>
426 BPhysBlindingTool::getTokens(std::string input, std::string seperators) {
427
428 std::vector<std::string> tokens;
429 boost::char_separator<char> sep(seperators.c_str());
430 typedef boost::tokenizer<boost::char_separator<char> > Tokenizer_t;
431 Tokenizer_t tokenizer(input, sep);
432 for (auto& token : tokenizer) {
433 tokens.push_back(token);
434 }
435 return tokens;
436 }
437 //--------------------------------------------------------------------------
438 // Format vector of floats as string
439 //--------------------------------------------------------------------------
440 std::string BPhysBlindingTool::vecToString(const std::vector<float>& v)
441 const {
442 std::string str("[");
443 for (unsigned int i=0; i<v.size(); ++i) {
444 str += std::to_string(v[i]);
445 if ( i < v.size()-1 ) str += ",";
446 }
447 str += "]";
448 return str;
449 }
450 //--------------------------------------------------------------------------
451 // Format vector of bools as string
452 //--------------------------------------------------------------------------
453 std::string BPhysBlindingTool::vecToString(const std::vector<bool>& v)
454 const {
455 std::string str("[");
456 for (unsigned int i=0; i<v.size(); ++i) {
457 str += std::to_string(v[i]);
458 if ( i < v.size()-1 ) str += ",";
459 }
460 str += "]";
461 return str;
462 }
463 //--------------------------------------------------------------------------
464} // 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
Base class for elements of a container that can have aux data.
Definition AuxElement.h:483
SG::Decorator< T, ALLOC > Decorator
Definition AuxElement.h:575
SG::Accessor< T, ALLOC > Accessor
Definition AuxElement.h:572
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
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.
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.