JAPAn
Just Another Parity Analyzer
Loading...
Searching...
No Matches
QwBlinder.h
Go to the documentation of this file.
1/*!
2 * \file QwBlinder.h
3 * \brief A class for blinding data, adapted from G0 blinder class.
4 *
5 * \author Peiqing Wang
6 * \date 2010-04-14
7 */
8
9/*!
10 * \file QwBlinder.h
11 * \brief Data blinding utilities for parity violation analysis
12 */
13
14#pragma once
15
16// System headers
17#include <vector>
18#include <iostream>
19#include <iomanip>
20
21// ROOT headers
22#include <TString.h>
23#include <TObjString.h>
24#include <TMD5.h>
25
26// Qweak headers
28#include "QwEPICSEvent.h"
29#include "QwTypes.h"
30
31// Forward declarations
32#ifdef __USE_DATABASE__
33class QwParityDB;
34#endif
35
36// Backup type definition for ULong64_t; needed with some older ROOT versions.
37#if !defined(ULong64_t)
38#if defined(R__WIN32) && !defined(__CINT__)
39typedef unsigned __int64 ULong64_t; // Portable unsigned long integer 8 bytes
40#else
41typedef unsigned long long ULong64_t; // Portable unsigned long integer 8 bytes
42#endif
43#endif
44
45
46/**
47 * \class QwBlinder
48 * \ingroup QwAnalysis
49 * \brief Data blinding utilities for parity violation analysis
50 *
51 * Implements cryptographic data blinding to prevent bias in parity violation
52 * measurements. Supports multiple blinding strategies (additive, multiplicative,
53 * or combined) with encrypted offsets and factors. Provides both asymmetry
54 * and difference blinding schemes to maintain analysis integrity while
55 * preserving statistical properties of the data.
56 */
57class QwBlinder {
59 public:
60
61 /// Available blinding strategies
68 /// Status of the blinding process or intermediate steps of the process
75 static const TString fStatusName[4];
76
77 /// Error flag value
78 static const UInt_t kErrorFlag_BlinderFail = 0x200;
79 constexpr static const Double_t kValue_BlinderFail = -1.0;
80
81 static void DefineOptions(QwOptions &options);
82
83
84
85 /// \brief Default constructor with optional database
86 QwBlinder(const EQwBlindingStrategy blinding_strategy = kAdditive);
87 /// \brief Default destructor
88 virtual ~QwBlinder();
89
90 /// \brief Update the status with new external information
91 void ProcessOptions(QwOptions& options);
92 /// \brief Update the status using a random number
93 void Update();
94 /// \brief Update the status with new external information
95 void Update(QwParityDB* db);
96 /// \brief Update the status with new external information
97 void Update(const QwSubsystemArrayParity& detectors);
98 /// \brief Update the status with new external information
99 void Update(const QwEPICSEvent& epics);
100
102 fBeamIsPresent = kTRUE;
103 };
104
105
106 void WriteFinalValuesToDB(QwParityDB* db);
107 void PrintCountersValues(std::vector<Int_t> fCounters, TString counter_type);
108 void PrintFinalValues(Int_t kVerbosity=1);
109
110
111#ifdef __USE_DATABASE__
112 /// Write to the database
113 void FillDB(QwParityDB *db, TString datatype);
114 void FillErrDB(QwParityDB *db, TString datatype);
115#endif // __USE_DATABASE__
116
117 /// Modifies the device error code variable passed to it, if the blinder is
118 /// not okay.
119 void ModifyThisErrorCode(UInt_t &errorcode) const {
120 errorcode |= kErrorFlag_BlinderFail;
121 };
122
123 /// Asymmetry blinding
124 void BlindValue(Double_t& value) const {
125 switch (fBlindingStrategy) {
126 case kAdditive:
127 value += fBlindingOffset; break;
128 case kMultiplicative:
129 value *= fBlindingFactor; break;
131 value = (value + fBlindingOffset) * fBlindingFactor; break;
132 default: break;
133 }
134 };
135 /// Asymmetry unblinding
136 void UnBlindValue(Double_t& value) const {
137 switch (fBlindingStrategy) {
138 case kAdditive:
139 value -= fBlindingOffset; break;
140 case kMultiplicative:
141 value /= fBlindingFactor; break;
143 value = value / fBlindingFactor - fBlindingOffset; break;
144 default: break;
145 }
146 };
147
148 /// Difference blinding
149 void BlindValue(Double_t& value, const Double_t& yield) const {
150 switch (fBlindingStrategy) {
151 case kAdditive:
152 value += yield * fBlindingOffset; break;
153 case kMultiplicative:
154 value *= fBlindingFactor; break;
156 value = (value + fBlindingOffset * yield) * fBlindingFactor; break;
157 default: break;
158 }
159 };
160 /// Difference unblinding
161 void UnBlindValue(Double_t& value, const Double_t& yield) const {
162 switch (fBlindingStrategy) {
163 case kAdditive:
164 value -= yield * fBlindingOffset; break;
165 case kMultiplicative:
166 value /= fBlindingFactor; break;
168 value = value / fBlindingFactor - yield * fBlindingOffset; break;
169 default: break;
170 }
171 };
172
173 /// Blind the asymmetry of an array of subsystems
176 diff.Blind(this);
177 };
178 /// Blind the pair asymmetry
179 /// and only check fBlindingStrategy to avoid overcounting fPatternCounters
184
185 /// Unblind the asymmetry of an array of subsystems
187 diff.UnBlind(this);
188 };
189
190 /// Blind the difference of an array of subsystems
193 diff.Blind(this, yield);
194 };
195 /// Blind the pair difference of an array of subsystems
198 diff.Blind(this, yield);
199 };
200
201 /// Unblind the difference of an array of subsystems
203 diff.UnBlind(this, yield);
204 };
205
206 const Bool_t& IsBlinderOkay() const {return fBlinderIsOkay;};
207
208 void ConstructObjects(TDirectory *folder, TString &prefix) {
209 if (folder != NULL) folder->cd();
210 const TObjString* seed = new TObjString(fSeed);
211 folder->WriteTObject(seed, prefix + "seed", "WriteDelete");
212 const TObjString* seedID = new TObjString(Form("%u",fSeedID));
213 folder->WriteTObject(seedID, prefix + "seedID", "WriteDelete");
214 const TObjString* strategy = new TObjString(Form("%u", fBlindingStrategy));
215 folder->WriteTObject(strategy, prefix + "strategy", "WriteDelete");
216 const TObjString* max_asymmetry = new TObjString(Form("%f",fMaximumBlindingAsymmetry));
217 folder->WriteTObject(max_asymmetry, prefix + "max_asymmetry", "WriteDelete");
218 const TObjString* max_factor = new TObjString(Form("%f",fMaximumBlindingFactor));
219 folder->WriteTObject(max_factor, prefix + "max_factor", "WriteDelete");
220 const TObjString* checksum = new TObjString(fChecksum.c_str());
221 folder->WriteTObject(checksum, prefix + "checksum", "WriteDelete");
222 };
223
224 private:
225 /// Indicates the first value received of the blindability of the target
234 /** \brief Set the current target blindability status. */
236 void SetWienState(EQwWienMode wienmode);
237 void SetIHWPPolarity(Int_t ihwppolarity);
238
239 // Target position look-up index for PREX/CREX.
240 // Index value of -1 is for the PREX positions
241 // CREX index values in date order go from "min" to "Max" and must correspond to cases in the QwBlinder::Update(const QwEPICSEvent& epics) function.
245
248
249 EQwBlinderStatus CheckBlindability(std::vector<Int_t> &fCounters);
251
252
253
254
255 private:
256
257 /// Private copy constructor
259 /// Private assignment operator
260 const QwBlinder& operator= (const QwBlinder& __attribute__((unused)) blinder) { return *this; };
261
262 // Variables and functions used in blinding the detector asymmetries
264 Double_t fBlindingOffset; /// The term to be added to detector asymmetries
265 Double_t fBlindingOffset_Base; /// The term to be added to detector asymmetries, before polarity correction
266 Double_t fBlindingFactor; /// The factor to be multiplied to detector asymmetries
267
268
269 static const Double_t kDefaultMaximumBlindingAsymmetry; /// Default maximum blinding asymmetry (in ppm)
270 static const Double_t kDefaultMaximumBlindingFactor; /// Default maximum blinding factor (in fraction from identity)
271
272 Double_t fMaximumBlindingAsymmetry; /// Maximum blinding asymmetry (in ppm)
273 Double_t fMaximumBlindingFactor; /// Maximum blinding factor (in fraction from identity)
274
275 UInt_t fSeedID; /// ID of seed used (seeds.seed_id)
276 TString fSeed; /// Seed string (seeds.seed)
277 static const TString kDefaultSeed; /// Default seed
278
279 std::vector<UChar_t> fDigest; /// Checksum in raw hex
280 std::string fChecksum; /// Checksum in ASCII hex
281
282 std::vector<double> fTestValues; /// Vector of test values, original
283 std::vector<double> fBlindTestValues; /// Vector of test values, after blinding
284 std::vector<double> fUnBlindTestValues; /// Vector of test values, after unblinding
285
286 void InitBlinders(const UInt_t seed_id); /// Initializes fBlindingFactor from fSeed.
287
288 void InitTestValues(const int n); /// Initializes the test values: fTestNumber, fTestValue,
289 /// fBlindTestValue, if fBlindingFactor is set.
290 Bool_t CheckTestValues(); /// Recomputes fBlindTestValue to check for memory errors
291
292
293 Int_t UseMD5(const TString &barestring); /// Returns an integer from a string using MD5
294
295 Int_t UseStringManip(const TString &barestring); /// Returns an integer from a string using
296 /// a character manipulation algorithm
297
298 Int_t UsePseudorandom(const TString &barestring); /// Returns an integer from a string using
299 /// a version of the helicity bit pseudorandom algorithm.
300
301
302 /// Reads the seed with specified id from the database object
303 Int_t ReadSeed(QwParityDB* db, const UInt_t seed_id);
304
305 /// Reads the seed from the database object
306 Int_t ReadSeed(QwParityDB* db);
307
308 /// Read the seed string generated utilizing a random number generator
309 Int_t ReadRandomSeed();
310
311 void WriteChecksum(QwParityDB* db); /// Writes fSeedID and fBFChecksum to DB for this analysis ID
312 void WriteTestValues(QwParityDB* db); /// Writes fTestNumber and fBlindTestValue to DB for this analysis ID
313
314 std::vector<UChar_t> GenerateDigest(const TString& input) const;
315
316 std::vector<Int_t> fPatternCounters; ///< Counts the number of events in each failure mode
317 std::vector<Int_t> fPairCounters; ///< Counts the number of helicity pairs in each failure mode
318};
EPICS data event handling and storage.
Basic data types and constants used throughout the Qweak analysis framework.
EQwWienMode
Double Wien configuration.
Definition QwTypes.h:308
__attribute__((no_sanitize("signed-integer-overflow"))) void QwMollerADC_Channel
Subsystem array container for parity analysis with asymmetry calculations.
unsigned long long ULong64_t
Definition QwBlinder.h:41
EPICS slow controls data management.
Command-line and configuration file options processor.
Definition QwOptions.h:141
Int_t kCREXTgtIndexMax
Definition QwBlinder.h:244
const QwBlinder & operator=(const QwBlinder &__attribute__((unused)) blinder)
Private assignment operator.
Definition QwBlinder.h:260
QwBlinder(const EQwBlindingStrategy blinding_strategy=kAdditive)
Default constructor with optional database.
Definition QwBlinder.cc:83
void InitTestValues(const int n)
Initializes fBlindingFactor from fSeed.
Definition QwBlinder.cc:754
EQwBlinderStatus
Status of the blinding process or intermediate steps of the process.
Definition QwBlinder.h:69
@ kBlindableFail
Definition QwBlinder.h:73
@ kIndeterminate
Definition QwBlinder.h:70
@ kNotBlindable
Definition QwBlinder.h:71
Int_t fIHWPPolarity
Definition QwBlinder.h:232
Int_t ReadRandomSeed()
Read the seed string generated utilizing a random number generator.
Definition QwBlinder.cc:526
std::vector< UChar_t > GenerateDigest(const TString &input) const
Writes fTestNumber and fBlindTestValue to DB for this analysis ID.
void Blind(QwSubsystemArrayParity &diff, const QwSubsystemArrayParity &yield)
Blind the difference of an array of subsystems.
Definition QwBlinder.h:191
Int_t fCREXTargetIndex
Definition QwBlinder.h:242
static const TString fStatusName[4]
Definition QwBlinder.h:58
EQwBlinderStatus fTargetBlindability_firstread
Indicates the first value received of the blindability of the target.
Definition QwBlinder.h:226
void WriteTestValues(QwParityDB *db)
Writes fSeedID and fBFChecksum to DB for this analysis ID.
Bool_t fTargetPositionForced
Definition QwBlinder.h:228
std::vector< double > fBlindTestValues
Vector of test values, original.
Definition QwBlinder.h:283
Int_t ReadSeed(QwParityDB *db)
Reads the seed from the database object.
std::vector< double > fTestValues
Checksum in ASCII hex.
Definition QwBlinder.h:282
void UnBlindValue(Double_t &value) const
Asymmetry unblinding.
Definition QwBlinder.h:136
void PrintFinalValues(Int_t kVerbosity=1)
Bool_t fBeamIsPresent
Definition QwBlinder.h:247
void Blind(QwSubsystemArrayParity &diff)
Blind the asymmetry of an array of subsystems.
Definition QwBlinder.h:174
std::vector< UChar_t > fDigest
Default seed.
Definition QwBlinder.h:279
Bool_t CheckTestValues()
EQwWienMode fWienMode
Definition QwBlinder.h:230
Double_t fBlindingOffset_Base
The term to be added to detector asymmetries.
Definition QwBlinder.h:265
static const TString kDefaultSeed
Seed string (seeds.seed)
Definition QwBlinder.h:277
std::vector< Int_t > fPatternCounters
Counts the number of events in each failure mode.
Definition QwBlinder.h:316
Int_t UsePseudorandom(const TString &barestring)
Definition QwBlinder.cc:846
Double_t fBlindingFactor
The term to be added to detector asymmetries, before polarity correction.
Definition QwBlinder.h:266
void Update()
Update the status using a random number.
Definition QwBlinder.cc:276
std::string fChecksum
Checksum in raw hex.
Definition QwBlinder.h:280
Double_t fBlindingOffset
Blinding strategy.
Definition QwBlinder.h:264
void UnBlind(QwSubsystemArrayParity &diff, const QwSubsystemArrayParity &yield)
Unblind the difference of an array of subsystems.
Definition QwBlinder.h:202
Double_t fBeamCurrentThreshold
Definition QwBlinder.h:246
void BlindValue(Double_t &value, const Double_t &yield) const
Difference blinding.
Definition QwBlinder.h:149
void UnBlindValue(Double_t &value, const Double_t &yield) const
Difference unblinding.
Definition QwBlinder.h:161
std::vector< Int_t > fPairCounters
Counts the number of helicity pairs in each failure mode.
Definition QwBlinder.h:317
void ClearEventData()
Definition QwBlinder.h:101
const Bool_t & IsBlinderOkay() const
Definition QwBlinder.h:206
Double_t fMaximumBlindingAsymmetry
Default maximum blinding factor (in fraction from identity)
Definition QwBlinder.h:272
void Update(QwParityDB *db)
Update the status with new external information.
void ModifyThisErrorCode(UInt_t &errorcode) const
Definition QwBlinder.h:119
static const Double_t kDefaultMaximumBlindingAsymmetry
The factor to be multiplied to detector asymmetries.
Definition QwBlinder.h:269
static void DefineOptions(QwOptions &options)
Definition QwBlinder.cc:69
void UnBlind(QwSubsystemArrayParity &diff)
Unblind the asymmetry of an array of subsystems.
Definition QwBlinder.h:186
void WriteChecksum(QwParityDB *db)
void BlindPair(QwSubsystemArrayParity &diff, const QwSubsystemArrayParity &yield)
Blind the pair difference of an array of subsystems.
Definition QwBlinder.h:196
void SetTargetBlindability(EQwBlinderStatus status)
Set the current target blindability status.
void SetWienState(EQwWienMode wienmode)
void BlindValue(Double_t &value) const
Asymmetry blinding.
Definition QwBlinder.h:124
Int_t kCREXTgtIndexMin
Definition QwBlinder.h:243
TString fSeed
ID of seed used (seeds.seed_id)
Definition QwBlinder.h:276
EQwWienMode fWienMode_firstread
Definition QwBlinder.h:229
void SetIHWPPolarity(Int_t ihwppolarity)
Bool_t fSpinDirectionForced
Definition QwBlinder.h:233
void WriteFinalValuesToDB(QwParityDB *db)
static constexpr const Double_t kValue_BlinderFail
Definition QwBlinder.h:79
static const Double_t kDefaultMaximumBlindingFactor
Default maximum blinding asymmetry (in ppm)
Definition QwBlinder.h:270
EQwBlindingStrategy
Available blinding strategies.
Definition QwBlinder.h:62
@ kMultiplicative
Definition QwBlinder.h:65
@ kAdditiveMultiplicative
Definition QwBlinder.h:66
Int_t UseStringManip(const TString &barestring)
Returns an integer from a string using MD5.
Definition QwBlinder.cc:801
void PrintCountersValues(std::vector< Int_t > fCounters, TString counter_type)
Int_t ReadSeed(QwParityDB *db, const UInt_t seed_id)
Reads the seed with specified id from the database object.
Int_t UseMD5(const TString &barestring)
Recomputes fBlindTestValue to check for memory errors.
Definition QwBlinder.cc:911
Double_t fMaximumBlindingFactor
Maximum blinding asymmetry (in ppm)
Definition QwBlinder.h:273
EQwBlinderStatus CheckBlindability(std::vector< Int_t > &fCounters)
void BlindPair(QwSubsystemArrayParity &diff)
Definition QwBlinder.h:180
std::vector< double > fUnBlindTestValues
Vector of test values, after blinding.
Definition QwBlinder.h:284
void InitBlinders(const UInt_t seed_id)
Vector of test values, after unblinding.
Definition QwBlinder.cc:670
QwBlinder(const QwBlinder &__attribute__((unused)) blinder)
Private copy constructor.
Definition QwBlinder.h:258
static const UInt_t kErrorFlag_BlinderFail
Error flag value.
Definition QwBlinder.h:78
EQwBlindingStrategy fBlindingStrategy
Definition QwBlinder.h:263
UInt_t fSeedID
Maximum blinding factor (in fraction from identity)
Definition QwBlinder.h:275
Int_t fIHWPPolarity_firstread
Definition QwBlinder.h:231
virtual ~QwBlinder()
Default destructor.
Definition QwBlinder.cc:219
EQwBlinderStatus fTargetBlindability
Definition QwBlinder.h:227
Bool_t fBlinderIsOkay
Definition QwBlinder.h:250
void ProcessOptions(QwOptions &options)
Update the status with new external information.
Definition QwBlinder.cc:230
void ConstructObjects(TDirectory *folder, TString &prefix)
Definition QwBlinder.h:208
Subsystem array container specialized for parity analysis with asymmetry calculations.
void Blind(const QwBlinder *blinder)
Blind the asymmetry of this subsystem.
void UnBlind(const QwBlinder *)
Unblind the asymmetry of this subsystem.