QwAnalysis
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 #ifndef __QWBLINDER__
10 #define __QWBLINDER__
11 
12 // System headers
13 #include <vector>
14 #include <iostream>
15 #include <iomanip>
16 
17 // ROOT headers
18 #include <TString.h>
19 #include <TMD5.h>
20 
21 // Qweak headers
22 #include "QwSubsystemArrayParity.h"
23 #include "QwEPICSEvent.h"
24 #include "QwTypes.h"
25 
26 // Forward declarations
27 class QwParityDB;
28 
29 // Backup type definition for ULong64_t; needed with some older ROOT versions.
30 #if !defined(ULong64_t)
31 #if defined(R__WIN32) && !defined(__CINT__)
32 typedef unsigned __int64 ULong64_t; // Portable unsigned long integer 8 bytes
33 #else
34 typedef unsigned long long ULong64_t; // Portable unsigned long integer 8 bytes
35 #endif
36 #endif
37 
38 
39 /**
40  * \class QwBlinder
41  * \ingroup QwAnalysis
42  *
43  * \brief Class for blinding data, adapted from G0 blinder class
44  *
45  * 1. Asymmetry blinding scheme:
46  * \f[
47  * Asym_{blinded} = (Asym_{actual} + fBlindingOffset) * fBlindingFactor
48  * \f]
49  * where \f$ fBlindingOffset = F \times sign(\lambda/2) \f$, F is an encrypted factor
50  * with |F| < 0.06 ppm.
51  * This offset fBlindingOffset will be applied on the block and blocksum of the asymmetry.
52  *
53  * 2. Difference blinding scheme:
54  * For blinding the helicity correlated differences of the detectors, we'd have to do:
55  * \f[
56  * Diff_{blinded} = (Diff_{raw} + Yield_{raw} * fBlindingOffset) * fBlindingFactor)
57  * \f]
58  * where \f$ Asym_{raw} = Diff_{raw} / Yield_{raw} \f$ is the unblinded asymmetry,
59  * and \f$ Asym_{blinded} = Diff_{blinded} / Yield_{blinded} \f$ the blinded asymmetry.
60  * Blinding the differences allows that the difference can be written to the output
61  * ROOT files without compromising the blinding.
62  *
63  */
64 class QwBlinder {
65 
66  public:
67 
68  /// Available blinding strategies
74  };
75  /// Status of the blinding process or intermediate steps of the process
81  };
82  static const TString fStatusName[4];
83 
84  /// Error flag value
85  static const UInt_t kErrorFlag_BlinderFail = 0x200;
86 
87  static void DefineOptions(QwOptions &options);
88 
89 
90 
91  /// \brief Default constructor with optional database
92  QwBlinder(const EQwBlindingStrategy blinding_strategy = kAdditive);
93  /// \brief Default destructor
94  virtual ~QwBlinder();
95 
96  /// \brief Update the status with new external information
97  void ProcessOptions(QwOptions& options);
98  /// \brief Update the status with new external information
99  void Update(QwParityDB* db);
100  /// \brief Update the status with new external information
101  void Update(const QwSubsystemArrayParity& detectors);
102  /// \brief Update the status with new external information
103  void Update(const QwEPICSEvent& epics);
104 
106  fBeamIsPresent = kTRUE;
107  };
108 
109 
111  void PrintFinalValues();
112 
113  /// Write to the database
114  void FillDB(QwParityDB *db, TString datatype);
115  void FillErrDB(QwParityDB *db, TString datatype);
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  /// Unblind the asymmetry of an array of subsystems
180  diff.UnBlind(this);
181  };
182 
183  /// Blind the difference of an array of subsystems
186  diff.Blind(this, yield);
187  };
188  /// Unblind the difference of an array of subsystems
190  diff.UnBlind(this, yield);
191  };
192 
193  const Bool_t& IsBlinderOkay() const {return fBlinderIsOkay;};
194 
195  private:
196  /// Indicates the first value recieved of the blindability of the target
205  void SetWienState(EQwWienMode wienmode);
206  void SetIHWPPolarity(Int_t ihwppolarity);
207 
208 
211 
214 
215 
216 
217 
218  private:
219 
220  /// Private copy constructor
221  QwBlinder (const QwBlinder& __attribute__((unused)) blinder): fBlindingStrategy(kDisabled) { };
222  /// Private assignment operator
223  const QwBlinder& operator= (const QwBlinder& __attribute__((unused)) blinder) { return *this; };
224 
225  // Variables and functions used in blinding the detector asymmetries
226  const EQwBlindingStrategy fBlindingStrategy; /// Blinding strategy
227  Double_t fBlindingOffset; /// The term to be added to detector asymmetries
228  Double_t fBlindingOffset_Base; /// The term to be added to detector asymmetries, before polarity correction
229  Double_t fBlindingFactor; /// The factor to be mutliplied to detector asymmetries
230 
231 
232  static const Double_t kMaximumBlindingAsymmetry; /// Maximum blinding asymmetry (in ppm)
233  static const Double_t kMaximumBlindingFactor; /// Maximum blinding factor (in % from identity)
234 
235  UInt_t fSeedID; /// ID of seed used (seeds.seed_id)
236  TString fSeed; /// Seed string (seeds.seed)
237  static const TString kDefaultSeed; /// Default seed
238 
239  std::vector<UChar_t> fDigest; /// Checksum in raw hex
240  std::string fChecksum; /// Checksum in ASCII hex
241 
242  std::vector<double> fTestValues; /// Vector of test values, original
243  std::vector<double> fBlindTestValues; /// Vector of test values, after blinding
244  std::vector<double> fUnBlindTestValues; /// Vector of test values, after unblinding
245 
246  void InitBlinders(const UInt_t seed_id); /// Initializes fBlindingFactor from fSeed.
247 
248  void InitTestValues(const int n); /// Initializes the test values: fTestNumber, fTestValue,
249  /// fBlindTestValue, if fBlindingFactor is set.
250  Bool_t CheckTestValues(); /// Recomputes fBlindTestValue to check for memory errors
251 
252 
253  Int_t UseMD5(const TString &barestring); /// Returns an integer from a string using MD5
254 
255  Int_t UseStringManip(const TString &barestring); /// Returns an integer from a string using
256  /// a character manipulation algorithm
257 
258  Int_t UsePseudorandom(const TString &barestring); /// Returns an integer from a string using
259  /// a version of the helicity bit pseudorandom algorithm.
260 
261 
262  /// Reads the seed with specified id from the database object
263  Int_t ReadSeed(QwParityDB* db, const UInt_t seed_id);
264 
265  /// Reads the seed from the database object
266  Int_t ReadSeed(QwParityDB* db);
267 
268  void WriteChecksum(QwParityDB* db); /// Writes fSeedID and fBFChecksum to DB for this analysis ID
269  void WriteTestValues(QwParityDB* db); /// Writes fTestNumber and fBlindTestValue to DB for this analysis ID
270 
271  std::vector<UChar_t> GenerateDigest(const TString& input) const;
272 
273  std::vector<Int_t> fPatternCounters; ///< Counts the number of events in each failure mode
274 
275 };
276 
277 #endif
Int_t UsePseudorandom(const TString &barestring)
Definition: QwBlinder.cc:617
void ProcessOptions(QwOptions &options)
Update the status with new external information.
Definition: QwBlinder.cc:112
void FillErrDB(QwParityDB *db, TString datatype)
Definition: QwBlinder.cc:1014
void UnBlind(QwSubsystemArrayParity &diff)
Unblind the asymmetry of an array of subsystems.
Definition: QwBlinder.h:179
Double_t fBlindingOffset_Base
The term to be added to detector asymmetries.
Definition: QwBlinder.h:228
Double_t fBlindingFactor
The term to be added to detector asymmetries, before polarity correction.
Definition: QwBlinder.h:229
EQwWienMode fWienMode
Definition: QwBlinder.h:201
void InitBlinders(const UInt_t seed_id)
Vector of test values, after unblinding.
Definition: QwBlinder.cc:441
Bool_t fTargetPositionForced
Definition: QwBlinder.h:199
Int_t UseStringManip(const TString &barestring)
Returns an integer from a string using MD5.
Definition: QwBlinder.cc:572
void Blind(const QwBlinder *blinder)
Blind the asymmetry of this subsystem.
const Bool_t & IsBlinderOkay() const
Definition: QwBlinder.h:193
EQwBlinderStatus fTargetBlindability_firstread
Indicates the first value recieved of the blindability of the target.
Definition: QwBlinder.h:193
An options class.
Definition: QwOptions.h:133
void BlindValue(Double_t &value) const
Asymmetry blinding.
Definition: QwBlinder.h:124
void BlindValue(Double_t &value, const Double_t &yield) const
Difference blinding.
Definition: QwBlinder.h:149
static const TString kDefaultSeed
Seed string (seeds.seed)
Definition: QwBlinder.h:237
Class for blinding data, adapted from G0 blinder class.
Definition: QwBlinder.h:64
void UnBlindValue(Double_t &value, const Double_t &yield) const
Difference unblinding.
Definition: QwBlinder.h:161
static void DefineOptions(QwOptions &options)
Definition: QwBlinder.cc:54
UInt_t fSeedID
Maximum blinding factor (in % from identity)
Definition: QwBlinder.h:235
std::vector< double > fTestValues
Checksum in ASCII hex.
Definition: QwBlinder.h:242
void WriteChecksum(QwParityDB *db)
Definition: QwBlinder.cc:725
void InitTestValues(const int n)
Initializes fBlindingFactor from fSeed.
Definition: QwBlinder.cc:525
static const Double_t kMaximumBlindingFactor
Maximum blinding asymmetry (in ppm)
Definition: QwBlinder.h:233
EQwWienMode
Double Wien configuration.
Definition: QwTypes.h:302
static const UInt_t kErrorFlag_BlinderFail
Error flag value.
Definition: QwBlinder.h:85
void WriteFinalValuesToDB(QwParityDB *db)
Definition: QwBlinder.cc:508
Int_t fIHWPPolarity
Definition: QwBlinder.h:203
EQwBlinderStatus CheckBlindability()
Definition: QwBlinder.cc:1079
void PrintFinalValues()
Definition: QwBlinder.cc:880
std::vector< UChar_t > fDigest
Default seed.
Definition: QwBlinder.h:239
std::string fChecksum
Checksum in raw hex.
Definition: QwBlinder.h:240
EQwBlindingStrategy
Available blinding strategies.
Definition: QwBlinder.h:69
Virtual base class for the parity subsystems.
void SetTargetBlindability(EQwBlinderStatus status)
Definition: QwBlinder.cc:1046
void ModifyThisErrorCode(UInt_t &errorcode) const
Definition: QwBlinder.h:119
EQwWienMode fWienMode_firstread
Definition: QwBlinder.h:200
Double_t fBlindingOffset
Blinding strategy.
Definition: QwBlinder.h:227
void Blind(QwSubsystemArrayParity &diff)
Blind the asymmetry of an array of subsystems.
Definition: QwBlinder.h:174
void Update(QwParityDB *db)
Update the status with new external information.
Definition: QwBlinder.cc:138
void SetIHWPPolarity(Int_t ihwppolarity)
Definition: QwBlinder.cc:1068
void ClearEventData()
Definition: QwBlinder.h:105
unsigned long long ULong64_t
Definition: QwBlinder.h:27
Int_t fIHWPPolarity_firstread
Definition: QwBlinder.h:202
Bool_t fBeamIsPresent
Definition: QwBlinder.h:210
EQwBlinderStatus fTargetBlindability
Definition: QwBlinder.h:198
void FillDB(QwParityDB *db, TString datatype)
Write to the database.
Definition: QwBlinder.cc:937
Int_t UseMD5(const TString &barestring)
Recomputes fBlindTestValue to check for memory errors.
Definition: QwBlinder.cc:682
QwBlinder(const QwBlinder &__attribute__((unused)) blinder)
Private copy constructor.
Definition: QwBlinder.h:221
static const Double_t kMaximumBlindingAsymmetry
The factor to be mutliplied to detector asymmetries.
Definition: QwBlinder.h:232
virtual ~QwBlinder()
Default destructor.
Definition: QwBlinder.cc:100
void UnBlind(QwSubsystemArrayParity &diff, const QwSubsystemArrayParity &yield)
Unblind the difference of an array of subsystems.
Definition: QwBlinder.h:189
QwBlinder(const EQwBlindingStrategy blinding_strategy=kAdditive)
Default constructor with optional database.
Definition: QwBlinder.cc:68
Int_t ReadSeed(QwParityDB *db, const UInt_t seed_id)
Reads the seed with specified id from the database object.
Definition: QwBlinder.cc:360
std::vector< double > fBlindTestValues
Vector of test values, original.
Definition: QwBlinder.h:243
void UnBlind(const QwBlinder *blinder)
Unblind the asymmetry of this subsystem.
static const TString fStatusName[4]
Definition: QwBlinder.h:82
void Blind(QwSubsystemArrayParity &diff, const QwSubsystemArrayParity &yield)
Blind the difference of an array of subsystems.
Definition: QwBlinder.h:184
void UnBlindValue(Double_t &value) const
Asymmetry unblinding.
Definition: QwBlinder.h:136
const QwBlinder & operator=(const QwBlinder &__attribute__((unused)) blinder)
Private assignment operator.
Definition: QwBlinder.h:223
void WriteTestValues(QwParityDB *db)
Writes fSeedID and fBFChecksum to DB for this analysis ID.
Definition: QwBlinder.cc:759
Double_t fBeamCurrentThreshold
Definition: QwBlinder.h:209
Bool_t fBlinderIsOkay
Definition: QwBlinder.h:213
void SetWienState(EQwWienMode wienmode)
Definition: QwBlinder.cc:1057
EQwBlinderStatus
Status of the blinding process or intermediate steps of the process.
Definition: QwBlinder.h:76
Bool_t CheckTestValues()
Definition: QwBlinder.cc:807
const EQwBlindingStrategy fBlindingStrategy
Definition: QwBlinder.h:223
std::vector< Int_t > fPatternCounters
Counts the number of events in each failure mode.
Definition: QwBlinder.h:273
std::vector< double > fUnBlindTestValues
Vector of test values, after blinding.
Definition: QwBlinder.h:244
std::vector< UChar_t > GenerateDigest(const TString &input) const
Writes fTestNumber and fBlindTestValue to DB for this analysis ID.
Definition: QwBlinder.cc:855
TString fSeed
ID of seed used (seeds.seed_id)
Definition: QwBlinder.h:236