19 #define MYSQLPP_SSQLS_NO_STATICS
44 "Blindable",
"BlindableFail"};
56 "Forces the blinder to interpret the target position as LH2");
58 "Forces the blinder to interpret the target position as target-out");
59 options.
AddOptions(
"Blinder")(
"blinder.beam-current-threshold", po::value<double>()->default_value(1.0),
60 "Beam current in microamps below which data will not be blinded");
69 fTargetBlindability_firstread(kIndeterminate),
70 fTargetBlindability(kIndeterminate),
71 fTargetPositionForced(kFALSE),
75 fIHWPPolarity_firstread(0),
78 fBeamCurrentThreshold(1.0),
79 fBeamIsPresent(kFALSE),
80 fBlindingStrategy(blinding_strategy),
82 fBlindingOffset_Base(0.0),
114 if (options.
GetValue<
bool>(
"blinder.force-target-out")
115 && options.
GetValue<
bool>(
"blinder.force-target-lh2")){
116 QwError <<
"QwBlinder::Update: Both blinder.force-target-lh2 and blinder.force-target-out are set. "
117 <<
"Only one can be in force at one time. Exit and choose one option."
120 }
else if (options.
GetValue<
bool>(
"blinder.force-target-lh2")){
123 }
else if (options.
GetValue<
bool>(
"blinder.force-target-out")){
127 if (options.
HasValue(
"blinder.beam-current-threshold")){
166 Bool_t tmp_beam = kFALSE;
196 Double_t tgt_temperture = epics.
GetDataValue(
"QWT_miB");
197 Double_t tgt_temperture2 = epics.
GetDataValue(
"QWT_moB");
200 QwDebug <<
"Target parameters used by the blinder: "
201 <<
"QWtgt_name=" << position <<
" "
202 <<
"QWTGTPOS=" << tgt_pos <<
" "
203 <<
"QWT_miB=" << tgt_temperture <<
" "
204 <<
"QWT_moB=" << tgt_temperture2 <<
" "
205 <<
"QW_PT3=" << tgt_pressure <<
" "
206 <<
"QW_PT4=" << tgt_pressure2 <<
" "
208 if (position ==
"HYDROGEN-CELL"
210 && (tgt_temperture>18.0 && tgt_temperture<22.0)
211 && (tgt_temperture2>18.0 && tgt_temperture2<22.0)
212 && (tgt_pressure>20.0 && tgt_pressure < 35.0)
213 && (tgt_pressure2>20.0 && tgt_pressure2 < 35.0)){
215 }
else if ((position ==
"HYDROGEN-CELL"
217 && (tgt_temperture > 22.0)
218 && (tgt_temperture2 > 22.0)
219 && (tgt_pressure > 35.0)
220 && (tgt_pressure2 > 35.0)){
224 }
else if ((position !=
"HYDROGEN-CELL"
231 QwWarning <<
"Target parameters used by the blinder are indeterminate: "
232 <<
"QWtgt_name=" << position <<
" "
233 <<
"QWTGTPOS=" << tgt_pos <<
" "
234 <<
"QWT_miB=" << tgt_temperture <<
" "
235 <<
"QWT_moB=" << tgt_temperture2 <<
" "
236 <<
"QW_PT3=" << tgt_pressure <<
" "
237 <<
"QW_PT4=" << tgt_pressure2 <<
" "
279 fSeed =
"Default seed, No database specified";
286 QwError <<
"QwBlinder::ReadSeed db->GetRunNumber() returns "
289 string s_sql =
"SELECT seed_id, seed FROM seeds, run as rf, run as rl WHERE seeds.first_run_id = rf.run_id AND seeds.last_run_id = rl.run_id ";
290 s_sql +=
"AND rf.run_number <= ";
292 s_sql +=
" AND rl.run_number >= ";
294 s_sql +=
" AND seed_id>2";
296 QwError <<
"QwBlinder::ReadSeed s_sql contains \'"
300 mysqlpp::Query query = db->
Query();
302 mysqlpp::StoreQueryResult res = query.store();
307 if (res.size() == 1) {
313 if (!res[0][
"seed"].is_null()) {
314 fSeed = res[0][
"seed"].data();
316 QwError <<
"QwBlinder::ReadSeed(): Seed value came back NULL from the database." <<
QwLog::endl;
321 std::cout <<
"QwBlinder::ReadSeed(): Successfully read "
322 << Form(
"the fSeed with ID %d from the database.",
fSeedID)
329 fSeed = Form(
"ERROR: There should be one and only one seed_id for each seed, but this had %zu.",
331 std::cerr <<
"QwBlinder::ReadSeed(): "<<
fSeed<<std::endl;
341 fSeed =
"ERROR: Unable to open the connection to the database.";
342 QwError <<
"QwBlinder::ReadSeed(): Unable to open connection to database." <<
QwLog::endl;
366 fSeed =
"Default seed, No database specified";
374 string s_sql =
"SELECT * FROM seeds ";
377 Char_t s_seed_id[10];
378 sprintf(s_seed_id,
"%d", seed_id);
379 s_sql +=
"WHERE seed_id = ";
380 s_sql += string(s_seed_id);
383 s_sql +=
"ORDER BY seed_id DESC LIMIT 1";
387 mysqlpp::Query query = db->
Query();
389 std::vector<QwParitySSQLS::seeds> res;
395 if (res.size() == 1) {
401 if (!res[0].seed.is_null) {
402 fSeed = res[0].seed.data;
404 QwError <<
"QwBlinder::ReadSeed(): Seed value came back NULL from the database." <<
QwLog::endl;
409 std::cout <<
"QwBlinder::ReadSeed(): Successfully read "
410 << Form(
"the fSeed with ID %d from the database.",
fSeedID)
417 fSeed = Form(
"ERROR: There should be one and only one seed_id for each seed, but this had %zu.",
419 std::cerr <<
"QwBlinder::ReadSeed(): "<<
fSeed<<std::endl;
429 fSeed =
"ERROR: Unable to open the connection to the database.";
430 QwError <<
"QwBlinder::ReadSeed(): Unable to open connection to database." <<
QwLog::endl;
459 if ((finalseed & 0x80000000) == 0x80000000) {
460 newtempout = -1.0 * (finalseed & 0x7FFFFFFF);
462 newtempout = 1.0 * (finalseed & 0x7FFFFFFF);
472 Double_t tmp1 = maximum_asymmetry_sqrt * (newtempout / Int_t(0x7FFFFFFF));
477 tmp1 = fBlindingOffset * 4;
479 fBlindingOffset = tmp2 - tmp1;
503 for (
size_t i = 0; i <
fDigest.size(); i++)
512 QwError <<
"QwBlinder::WriteFinalValuesToDB(): "
513 <<
"Blinded test values have changed; may be a problem in the analysis!!!"
537 for (
int i = 0; i < n; i++) {
540 for (Int_t j = 0; j < 16; j++) {
541 finalseed &= 0x7FFFFFFF;
542 if ((finalseed & 0x800000) == 0x800000) {
543 finalseed = ((finalseed ^ 0x00000d) << 1) | 0x1;
552 Int_t mask = 0xFFFFFF;
553 Double_t tempval = (1.0 - 2.0*(finalseed&mask)/mask) / (1.0e6);
564 <<
" test values have been calculated successfully." <<
QwLog::endl;
574 std::vector<UInt_t> choppedwords;
578 for (Int_t i = 0; i < barestring.Length(); i++)
580 if (i % 4 == 0) tmpword = 0;
581 tmpword |= (char(barestring[i]))<<(24-8*(i%4));
582 if (i%4 == 3 || i == barestring.Length()-1)
584 choppedwords.push_back(tmpword);
585 finalseed ^= (tmpword);
588 for (Int_t i=0; i<64; i++)
590 finalseed &= 0x7FFFFFFF;
591 if ((finalseed & 0x800000) == 0x800000)
593 finalseed = ((finalseed^0x00000d)<<1) | 0x1;
600 if ((finalseed&0x80000000) == 0x80000000)
602 finalseed = -1 * (finalseed&0x7FFFFFFF);
606 finalseed = (finalseed&0x7FFFFFFF);
626 longmask|=0xFFFFFFFF;
630 for (Int_t i=0; i<barestring.Length(); i++)
632 if ( ((barestring[i])&0xC0)!=0 && ((barestring[i])&0xC0)!=0xC0)
634 finalseed = ((finalseed&longmask)<<1) | (((barestring[i])&0x40)>>6);
637 if ( ((barestring[i])&0x30)!=0 && ((barestring[i])&0x30)!=0x30)
639 finalseed = ((finalseed&longmask)<<1) | (((barestring[i])&0x10)>>4);
642 if ( ((barestring[i])&0xC)!=0 && ((barestring[i])&0xC)!=0xC)
644 finalseed = ((finalseed&longmask)<<1) | (((barestring[i])&0x4)>>2);
647 if ( ((barestring[i])&0x3)!=0 && ((barestring[i])&0x3)!=0x3)
649 finalseed = ((finalseed&longmask)<<1) | ((barestring[i])&0x1);
653 for (Int_t i=0; i<(192-bitcount); i++)
655 if ((finalseed & 0x800000) == 0x800000)
657 finalseed = ((finalseed^0x00000d)<<1) | 0x1;
664 tempout = (finalseed&0xFFFFFFFF) ^ ((finalseed>>32)&0xFFFFFFFF);
665 if ((tempout&0x80000000) == 0x80000000)
667 tempout = -1 * (tempout&0x7FFFFFFF);
671 tempout = 1 * (tempout&0x7FFFFFFF);
688 for (
size_t i = 0; i < digest.size(); i++)
695 temp |= (digest[i])<<(24-(j*8));
696 if ( (j==3) || (i==(digest.size()-1)))
702 if ((tempout & 0x80000000) == 0x80000000) {
703 tempout = -1 * (tempout & 0x7FFFFFFF);
705 tempout = (tempout & 0x7FFFFFFF);
731 string s_sql =
"UPDATE analysis SET seed_id = ";
732 sprintf(s_number,
"%d",
fSeedID);
733 s_sql += string(s_number);
734 s_sql +=
", bf_checksum = ";
736 s_sql +=
" WHERE analysis_id = ";
738 s_sql += string(s_number);
744 mysqlpp::Query query = db->
Query();
766 string s_sql_pre =
"INSERT INTO bf_test (analysis_id, test_number, test_value) VALUES (";
769 s_sql_pre += string(s_number);
778 string s_sql = s_sql_pre;
781 sprintf(s_number,
"%d", (
int) i);
782 s_sql += string(s_number);
792 mysqlpp::Query query = db->
Query();
809 Bool_t status = kTRUE;
813 double epsilon = std::numeric_limits<double>::epsilon();
821 double test2 = checkval;
822 if ((test1 - test2) <= -epsilon || (test1 - test2) >= epsilon) {
823 QwError <<
"QwBlinder::CheckTestValues(): Unblinded test value "
825 <<
" does not agree with original test value, "
826 <<
"with a difference of "
828 <<
" (epsilon==" << epsilon <<
")"
836 if ((test1 - test2) <= -epsilon || (test1 - test2) >= epsilon) {
837 QwError <<
"QwBlinder::CheckTestValues(): Unblinded test value "
839 <<
" does not agree with original test value, "
840 <<
"with a difference of "
858 const UInt_t length = 16;
859 UChar_t value[length];
860 for (UInt_t i = 0; i < length; i++)
865 md5.Update((UChar_t*) input.Data(), input.Length());
869 std::vector<UChar_t> output;
870 for (UInt_t i = 0; i < length; i++)
871 output.push_back(value[i]);
899 QwMessage <<
"The blinding parameters checksum for seed ID "
904 double epsilon = std::numeric_limits<double>::epsilon();
908 << std::setw(16) <<
"Original value"
909 << std::setw(16) <<
"Blinded value"
910 << std::setw(22) <<
"Orig.-Unblind value"
919 << std::setw(16) << Form(
" [CENSORED]")
921 << std::setw(22) << diff
939 QwDebug <<
" --------------------------------------------------------------- " <<
QwLog::endl;
941 QwDebug <<
" --------------------------------------------------------------- " <<
QwLog::endl;
948 QwError <<
"QwBlinder::FillDB(): "
949 <<
"Blinded test values have changed; "
950 <<
"may be a problem in the analysis!!!"
954 QwParitySSQLS::bf_test bf_test_row(0);
955 std::vector<QwParitySSQLS::bf_test> bf_test_list;
957 bf_test_row.bf_test_id = 0;
958 bf_test_row.analysis_id = analysis_id;
959 bf_test_row.test_number = i;
961 bf_test_list.push_back(bf_test_row);
971 mysqlpp::Query query = db->
Query();
972 query <<
"select * from analysis where analysis_id = "
974 std::vector<QwParitySSQLS::analysis> analysis_res;
976 query.storein(analysis_res);
977 if (analysis_res.size() == 1) {
978 QwParitySSQLS::analysis analysis_row_new = analysis_res[0];
980 analysis_row_new.seed_id =
fSeedID;
981 analysis_row_new.bf_checksum =
fChecksum;
983 query.update(analysis_res[0], analysis_row_new);
987 QwError <<
"Unable to update analysis table with blinder information. "
990 }
catch (
const mysqlpp::Exception& err) {
996 if (bf_test_list.size()) {
997 mysqlpp::Query query = db->
Query();
998 query.insert(bf_test_list.begin(), bf_test_list.end());
1002 QwMessage <<
"QwBlinder::FillDB(): No bf_test entries to write."
1005 }
catch (
const mysqlpp::Exception& err) {
1016 QwDebug <<
" --------------------------------------------------------------- " <<
QwLog::endl;
1018 QwDebug <<
" --------------------------------------------------------------- " <<
QwLog::endl;
1020 std::vector<QwParitySSQLS::general_errors> entrylist;
1034 if( entrylist.size() ) {
1035 mysqlpp::Query query= db->
Query();
1036 query.insert(entrylist.begin(), entrylist.end());
1052 QwMessage <<
"QwBlinder: First set target blindability to "
1063 QwMessage <<
"QwBlinder: First set Wien state to "
1073 QwMessage <<
"QwBlinder: First set IHWP state to "
1086 QwDebug <<
"QwBlinder::CheckBlindability: The target blindability is not determined. "
1092 QwDebug <<
"QwBlinder::CheckBlindability: The target blindability has changed. "
1136 QwError <<
"QwBlinder::CheckBlindability: The pattern blindability is unclear. "
Int_t UsePseudorandom(const TString &barestring)
#define QwMessage
Predefined log drain for regular messages.
void ProcessOptions(QwOptions &options)
Update the status with new external information.
void FillErrDB(QwParityDB *db, TString datatype)
Double_t fBlindingOffset_Base
The term to be added to detector asymmetries.
Double_t fBlindingFactor
The term to be added to detector asymmetries, before polarity correction.
const VQwHardwareChannel * ReturnInternalValue(const TString &name) const
Retrieve the variable name from subsystems in this subsystem array.
void InitBlinders(const UInt_t seed_id)
Vector of test values, after unblinding.
Bool_t fTargetPositionForced
#define default_bool_value(b)
Int_t UseStringManip(const TString &barestring)
Returns an integer from a string using MD5.
Bool_t Connect()
Open a connection to the database using the predefined parameters.
EQwBlinderStatus fTargetBlindability_firstread
Indicates the first value recieved of the blindability of the target.
void BlindValue(Double_t &value) const
Asymmetry blinding.
Double_t GetDataValue(const string &tag) const
bool HasValue(const std::string &key)
Has this key been defined.
Int_t DetermineIHWPPolarity() const
static const TString kDefaultSeed
Seed string (seeds.seed)
static void DefineOptions(QwOptions &options)
UInt_t fSeedID
Maximum blinding factor (in % from identity)
std::vector< double > fTestValues
Checksum in ASCII hex.
void WriteChecksum(QwParityDB *db)
void InitTestValues(const int n)
Initializes fBlindingFactor from fSeed.
Double_t GetValue(size_t element) const
static const Double_t kMaximumBlindingFactor
Maximum blinding asymmetry (in ppm)
EQwWienMode
Double Wien configuration.
void WriteFinalValuesToDB(QwParityDB *db)
void SetAnalysisID(UInt_t id)
EQwBlinderStatus CheckBlindability()
std::vector< UChar_t > fDigest
Default seed.
po::options_description_easy_init AddOptions(const std::string &blockname="Specialized options")
Add an option to a named block or create new block.
std::string fChecksum
Checksum in raw hex.
T GetValue(const std::string &key)
Get a templated value.
EQwBlindingStrategy
Available blinding strategies.
Virtual base class for the parity subsystems.
void SetTargetBlindability(EQwBlinderStatus status)
#define QwDebug
Predefined log drain for debugging output.
EQwWienMode fWienMode_firstread
Double_t fBlindingOffset
Blinding strategy.
A logfile class, based on an identical class in the Hermes analyzer.
TString GetDataString(const string &tag) const
void Update(QwParityDB *db)
Update the status with new external information.
void SetIHWPPolarity(Int_t ihwppolarity)
unsigned long long ULong64_t
Int_t fIHWPPolarity_firstread
mysqlpp::Query Query(const char *qstr=0)
EQwWienMode DetermineWienMode() const
EQwBlinderStatus fTargetBlindability
void FillDB(QwParityDB *db, TString datatype)
Write to the database.
EQwBlinderErrorCounterIndices
Blinder event counter indices.
Int_t UseMD5(const TString &barestring)
Recomputes fBlindTestValue to check for memory errors.
static const Double_t kMaximumBlindingAsymmetry
The factor to be mutliplied to detector asymmetries.
virtual ~QwBlinder()
Default destructor.
A class for blinding data, adapted from G0 blinder class.
QwBlinder(const EQwBlindingStrategy blinding_strategy=kAdditive)
Default constructor with optional database.
Int_t ReadSeed(QwParityDB *db, const UInt_t seed_id)
Reads the seed with specified id from the database object.
void SetErrorCodeId(UInt_t in)
std::vector< double > fBlindTestValues
Vector of test values, original.
void AddThisEntryToList(std::vector< T > &list)
static std::ostream & endl(std::ostream &)
End of the line.
void SetDeviceID(UInt_t id)
static const TString fStatusName[4]
void UnBlindValue(Double_t &value) const
Asymmetry unblinding.
void WriteTestValues(QwParityDB *db)
Writes fSeedID and fBFChecksum to DB for this analysis ID.
Double_t fBeamCurrentThreshold
#define QwWarning
Predefined log drain for warnings.
void SetWienState(EQwWienMode wienmode)
EQwBlinderStatus
Status of the blinding process or intermediate steps of the process.
const EQwBlindingStrategy fBlindingStrategy
std::vector< Int_t > fPatternCounters
Counts the number of events in each failure mode.
std::vector< double > fUnBlindTestValues
Vector of test values, after blinding.
std::vector< UChar_t > GenerateDigest(const TString &input) const
Writes fTestNumber and fBlindTestValue to DB for this analysis ID.
TString fSeed
ID of seed used (seeds.seed_id)
std::string WienModeName(EQwWienMode type)
#define QwError
Predefined log drain for errors.