Skip to content

Commit

Permalink
feat: helicity correction function QADB::CorrectHelicitySign (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
c-dilks authored Feb 3, 2025
1 parent 2d6f3b3 commit 497d16d
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 6 deletions.
34 changes: 32 additions & 2 deletions src/clasqa/QADB.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import clasqa.Tools

class QADB {

final MISC_BIT = 5 // FIXME: should be obtained from `defect_definitions.json`
final BSAWRONG_BIT = 10
final BSAUNKNOWN_BIT = 11

//..............
// constructor
//``````````````
Expand Down Expand Up @@ -264,9 +268,9 @@ class QADB {
return false
}
def use_mask = mask
if(hasDefectBit(5)) {
if(hasDefectBit(MISC_BIT)) {
if(runnum_ in allowMiscBitList) {
use_mask &= ~(0x1 << 5) // set `use_mask`'s Misc bit to 0
use_mask &= ~(0x1 << MISC_BIT) // set `use_mask`'s Misc bit to 0
}
}
return foundHere && !(defect & use_mask)
Expand Down Expand Up @@ -471,6 +475,32 @@ class QADB {
public void resetAccumulatedCharge() { chargeTotal = 0 }


//.................................
// Helicity Sign Correction
//`````````````````````````````````
// use this method to get the correct helicity sign:
// - this method returns -1 if the QA bin has the `BSAWrong` defect,
// which indicates the helicity sign in the data is incorrect
// - therefore:
// 'true helicity' = 'helicity from data' * CorrectHelicitySign(run_number, event_number)
// - zero is returned, if the event is not found in the QADB or if the pion BSA
// sign is not distinguishable from zero
// - the return value is constant for a run, but is still assigned per QA bin, for
// full generality
// - the return value may NOT be constant for all runs in a data set; for example,
// deviations from the normal value happen when a single run is cooked with the
// wrong HWP position
public int correctHelicitySign(int runnum_, int evnum_) {
def foundHere = query(runnum_,evnum_)
if(!foundHere) {
return 0
}
if(hasDefectBit(BSAUNKNOWN_BIT)) {
return 0
}
return hasDefectBit(BSAWRONG_BIT) ? -1 : 1
}


//===============================================================

Expand Down
14 changes: 14 additions & 0 deletions src/tests/testCorrectHelicitySign.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import clasqa.QADB

def runnum
final EVNUM = 100000 // FIXME: assumption
def cook
if(args.length>=2) {
cook = args[0]
runnum = args[1].toInteger()
} else {
throw new Exception("arguments must be [cook] [runnum]")
}

QADB qa = new QADB(cook)
System.out.println(qa.correctHelicitySign(runnum, EVNUM))
2 changes: 2 additions & 0 deletions src/tests/testDumpQADB.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ runlistFile.eachLine { runnumStr ->
// print comment
println "comment: \"" + qa.getComment() + "\""

// print helicity sign correction
println "helicity sign: ${qa.correctHelicitySign(runnum, evnum)}"

// print overall defect info
println "- defect"
Expand Down
39 changes: 37 additions & 2 deletions srcC/include/QADB.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
#include <algorithm>

namespace QA {

int const MISC_BIT = 5; // FIXME: should be obtained from `defect_definitions.json`
int const BSAWRONG_BIT = 10;
int const BSAUNKNOWN_BIT = 11;

class QADB {
private:

Expand Down Expand Up @@ -178,6 +183,7 @@ namespace QA {
return false;
}


//.................................
// Faraday Cup charge
//`````````````````````````````````
Expand All @@ -197,6 +203,23 @@ namespace QA {
inline void ResetAccumulatedCharge() { chargeTotal = 0; };


//.................................
// Helicity Sign Correction
//`````````````````````````````````
// use this method to get the correct helicity sign:
// - this method returns -1 if the QA bin has the `BSAWrong` defect,
// which indicates the helicity sign in the data is incorrect
// - therefore:
// 'true helicity' = 'helicity from data' * CorrectHelicitySign(run_number, event_number)
// - zero is returned, if the event is not found in the QADB or if the pion BSA
// sign is not distinguishable from zero
// - the return value is constant for a run, but is still assigned per QA bin, for
// full generality
// - the return value may NOT be constant for all runs in a data set; for example,
// deviations from the normal value happen when a single run is cooked with the
// wrong HWP position
inline int CorrectHelicitySign(int runnum_, int evnum_);


private:

Expand Down Expand Up @@ -487,9 +510,9 @@ namespace QA {
if(!foundHere)
return false;
auto use_mask = mask;
if(this->HasDefectBit(5)) {
if(this->HasDefectBit(MISC_BIT)) {
if(allowMiscBitList.find(runnum_) != allowMiscBitList.end())
use_mask &= ~(0x1 << 5); // set `use_mask`'s Misc bit to 0
use_mask &= ~(0x1 << MISC_BIT); // set `use_mask`'s Misc bit to 0
}
return !(defect & use_mask);
}
Expand Down Expand Up @@ -662,6 +685,18 @@ namespace QA {
};
};

//.................................
// Helicity Sign Correction
//`````````````````````````````````
int QADB::CorrectHelicitySign(int runnum_, int evnum_) {
bool foundHere = this->Query(runnum_,evnum_);
if(!foundHere)
return 0;
if(this->HasDefectBit(BSAUNKNOWN_BIT))
return 0;
return this->HasDefectBit(BSAWRONG_BIT) ? -1 : 1;
}

};


Expand Down
2 changes: 1 addition & 1 deletion srcC/tests/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CXX = g++ -std=c++11
CXX = g++ -std=c++17
FLAGS = -g -Wno-deprecated -fPIC -m64 -fno-inline -Wno-write-strings

ifndef QADB
Expand Down
21 changes: 21 additions & 0 deletions srcC/tests/testCorrectHelicitySign.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>
#include <bitset>
#include <memory>

#include "QADB.h"

int main(int argc, char ** argv) {

int runnum;
int const EVNUM = 100000; // FIXME: assumption
std::string cook;
if(argc>2) {
cook = std::string(argv[1]);
runnum = std::stoi(argv[2]);
}
else throw std::runtime_error("arguments must be [cook] [runnum]");

auto qa = std::make_unique<QA::QADB>(cook);
std::cout << qa->CorrectHelicitySign(runnum, EVNUM) << std::endl;
return 0;
}
2 changes: 2 additions & 0 deletions srcC/tests/testDumpQADB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ int main(int argc, char ** argv) {
// print comment
cout << "comment: \"" << qa->GetComment() << "\"" << endl;

// print helicity sign correction
cout << "helicity sign: " << qa->CorrectHelicitySign(runnum, evnum) << endl;

// print overall defect info
cout << "- defect" << endl;
Expand Down
2 changes: 1 addition & 1 deletion util/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CXX = g++ -std=c++11
CXX = g++ -std=c++17
FLAGS = -g -Wno-deprecated -fPIC -m64 -fno-inline -Wno-write-strings

# QADB and rapidjson
Expand Down

0 comments on commit 497d16d

Please sign in to comment.