JAPAn
Just Another Parity Analyzer
Loading...
Searching...
No Matches
QwHelicityBase.h
Go to the documentation of this file.
1/**********************************************************\
2* File: QwHelicityBase.h *
3* *
4* Author: Arindam Sen (asen@jlab.org) *
5* Time-stamp: November, 2025 *
6\**********************************************************/
7
8/*!
9 * \file QwHelicityBase.h
10 * \brief Helicity state management and pattern recognition
11 */
12
13#pragma once
14
15// System headers
16#include <vector>
17
18// ROOT headers
19#include "TTree.h"
20
21#ifdef HAS_RNTUPLE_SUPPORT
22#include "ROOT/RNTupleModel.hxx"
23#include "ROOT/RNTupleWriter.hxx"
24#include "ROOT/RField.hxx"
25#endif // HAS_RNTUPLE_SUPPORT
26
27// Qweak headers
28#include "VQwSubsystemParity.h"
29#include "QwWord.h"
30
31// Forward declarations
32#ifdef __USE_DATABASE__
33class QwParityDB;
34#endif
35
39// this emun vector needs to be coherent with the DetectorTypes declaration in the QwBeamLine constructor
40
41
42/**
43 * \class QwHelicityBase
44 * \ingroup QwAnalysis_BeamLine
45 * \brief Base subsystem for helicity state management and pattern recognition
46 *
47 * Manages helicity information from the polarized electron beam, including
48 * helicity state determination, pattern recognition, delayed helicity decoding,
49 * and helicity-correlated systematic checks. Supports multiple helicity
50 * encoding modes and provides helicity information to other subsystems.
51 */
53
54 private:
55 /// Private default constructor (not implemented, will throw linker error on use)
57
58 public:
59 /// Constructor with name
60 QwHelicityBase(const TString& name);
61 /// Copy constructor
62 QwHelicityBase(const QwHelicityBase& source);
63 /// Virtual destructor
64 virtual ~QwHelicityBase() override { }
65
66
67 /* derived from VQwSubsystem */
68 /// \brief Define options function
69
70 static void DefineOptions(QwOptions &options);
71 void ProcessOptions(QwOptions &options) override;
72 Int_t LoadChannelMap(TString mapfile) override;
73 Int_t LoadInputParameters(TString pedestalfile) override;
74 Int_t LoadEventCuts(TString filename) override;//Loads event cuts applicable to QwHelicityBase class, derived from VQwSubsystemParity
75 Bool_t ApplySingleEventCuts() override;//Apply event cuts in the QwHelicityBase class, derived from VQwSubsystemParity
76
77 Bool_t CheckForBurpFail(const VQwSubsystem *ev_error) override{
78 return kFALSE;
79 };
80
81 void IncrementErrorCounters() override;
82 void PrintErrorCounters() const override;// report number of events failed due to HW and event cut failure, derived from VQwSubsystemParity
83 UInt_t GetEventcutErrorFlag() override;//return the error flag
84 //update the error flag in the subsystem level from the top level routines related to stability checks. This will uniquely update the errorflag at each channel based on the error flag in the corresponding channel in the ev_error subsystem
85 void UpdateErrorFlag(const VQwSubsystem *ev_error) override{
86 };
87
88 Int_t ProcessConfigurationBuffer(const ROCID_t roc_id, const BankID_t bank_id,
89 UInt_t* buffer, UInt_t num_words) override;
90 Int_t ProcessEvBuffer(const ROCID_t roc_id, const BankID_t bank_id, UInt_t* buffer, UInt_t num_words) {
91 return ProcessEvBuffer(0x1,roc_id,bank_id,buffer,num_words);
92 };
93 Int_t ProcessEvBuffer(UInt_t ev_type, const ROCID_t roc_id, const BankID_t bank_id, UInt_t* buffer, UInt_t num_words) override;
94
95 virtual void ClearEventData() override;
96
97 UInt_t GetRandomSeedActual() { return iseed_Actual; };
98 UInt_t GetRandomSeedDelayed() { return iseed_Delayed; };
99
100 void PredictHelicity();
101 void RunPredictor();
102 void SetHelicityDelay(Int_t delay);
103 void SetHelicityBitPattern(TString hex);
104
105 Int_t GetHelicityReported();
106 Int_t GetHelicityActual();
107 Int_t GetHelicityDelayed();
108 Long_t GetEventNumber();
109 Long_t GetPatternNumber();
110 Int_t GetPhaseNumber();
112 return fMaxPatternPhase;
113 };
115 return fMinPatternPhase;
116 }
117 void SetFirstBits(UInt_t nbits, UInt_t firstbits);
118 void SetEventPatternPhase(Int_t event, Int_t pattern, Int_t phase);
119
120 VQwSubsystem& operator= (VQwSubsystem *value) override;
121 VQwSubsystem& operator+= (VQwSubsystem *value) override;
122
123 //the following functions do nothing really : adding and subtracting helicity doesn't mean anything
124 virtual VQwSubsystem& operator-= (VQwSubsystem *value) override {return *this;};
125 void Scale(Double_t factor) override {return;};
126 void Ratio(VQwSubsystem *numer, VQwSubsystem *denom) override;
127 // end of "empty" functions
128
129 void AccumulateRunningSum(VQwSubsystem* value, Int_t count=0, Int_t ErrorMask=0xFFFFFFF) override;
130 //remove one entry from the running sums for devices
131 void DeaccumulateRunningSum(VQwSubsystem* value, Int_t ErrorMask=0xFFFFFFF) override{ };
132 void CalculateRunningAverage() override { };
133
135 void ConstructHistograms(TDirectory *folder, TString &prefix) override;
136 void FillHistograms() override;
137
138
140 void ConstructBranchAndVector(TTree *tree, TString &prefix, QwRootTreeBranchVector &values) override;
141 void ConstructBranch(TTree *tree, TString &prefix) override;
142 void ConstructBranch(TTree *tree, TString &prefix, QwParameterFile& trim_file) override;
143 void FillTreeVector(QwRootTreeBranchVector &values) const override;
144
145#ifdef HAS_RNTUPLE_SUPPORT
146 using VQwSubsystem::ConstructNTupleAndVector;
147 void ConstructNTupleAndVector(std::unique_ptr<ROOT::RNTupleModel>& model, TString &prefix, std::vector<Double_t>& values, std::vector<std::shared_ptr<Double_t>>& fieldPtrs) override;
148 void FillNTupleVector(std::vector<Double_t>& values) const override;
149#endif // HAS_RNTUPLE_SUPPORT
150
151#ifdef __USE_DATABASE__
152 void FillDB(QwParityDB *db, TString type) override;
153 void FillErrDB(QwParityDB *db, TString datatype) override;
154#endif // __USE_DATABASE__
155
156 void Print() const;
157
159
160 virtual Bool_t IsGoodHelicity();
161
162/////
163 protected:
164 void CheckPatternNum(VQwSubsystem *value);
165 void MergeCounters(VQwSubsystem *value);
166
167
168 protected:
172
173 static const std::vector<UInt_t> kDefaultHelicityBitPattern;
174
175 std::vector<UInt_t> fHelicityBitPattern;
176
177 std::vector <QwWord> fWord;
178 std::vector < std::pair<Int_t, Int_t> > fWordsPerSubbank; // The indices of the first & last word in each subbank
179
181 // this variable is set at initialization in function QwHelicityBase::LoadChannelMap
182 // it allows one to customize the helicity decoding mode
183 // the helicity decoding mode will take one of the value of enum HelicityEncodingType
184
185
186
187 Int_t kUserbit;
188 // this is used to tagged the userbit info among all the fWords
189 // if we run the local helicity mode, the userbit contains the info
190 // about helicity, event number, pattern number etc.
192 // again this is used in the case we are running the local helicity mode
193 // the scalercounter counts how many events happened since the last reading
194 // should be one all the time if not the event is suspicious and not used for analysis
196
198
203 Int_t fActualPatternPolarity; ///< True polarity of the current pattern
204 Int_t fDelayedPatternPolarity; ///< Reported polarity of the current pattern
205 Int_t fPreviousPatternPolarity; ///< True polarity of the previous pattern.
207 // reported is what is registered in the coda file (it is the actual beam helicity fHelicityDelay pattern before this event)
208 // actual is the helicity of the beam for this event
209 // delayed is the expected reported helicity predicted by the random generator
210 // std::vector <Int_t> fCheckHelicityDelay;// this is obsolete
211 //this array keeps in memory the Actual helicity up to when it can be compared to the reported helicity
216
218 //allow one to select which types of histograms are created and filled
219 void SetHistoTreeSave(const TString &prefix);
220
221
222
223 static const Bool_t kDEBUG=kFALSE;
224 // local helicity is a special mode for encoding helicity info
225 // it is not the fullblown helicity encoding we want to use for the main
226 // data taking. For example this was used during the injector data taking
227 // in winter 2008-09 injector tests
228 static const Int_t kUndefinedHelicity= -9999;
229
230
231 /* Ntuple array indices */
234 UInt_t n_ranbits; //counts how many ranbits we have collected
235 UInt_t iseed_Actual; //stores the random seed for the helicity predictor
237 //stores the random seed to predict the reported helicity
239 //number of events the helicity is delayed by before being reported
240 //static const Int_t MaxPatternPhase =4;
243 Bool_t IsGoodPatternNumber();
244 Bool_t IsGoodEventNumber();
245 Bool_t MatchActualHelicity(Int_t actual);
246 Bool_t IsGoodPhaseNumber();
247 Bool_t IsContinuous();
248
249 virtual UInt_t GetRandbit(UInt_t& ranseed);
250 UInt_t GetRandbit24(UInt_t& ranseed);//for 24bit pattern
251 UInt_t GetRandbit30(UInt_t& ranseed);//for 30bit pattern
252 UInt_t GetRandomSeed(UShort_t* first24randbits);
253 virtual Bool_t CollectRandBits();
254 Bool_t CollectRandBits24();//for 24bit pattern
255 Bool_t CollectRandBits30();//for 30bit pattern
256
257
258 void ResetPredictor();
259
260 Bool_t Compare(VQwSubsystem *source);
261
262 Int_t fRandBits;//sets the random seed size 24bit/30bits
266
268
270
271
274
281
282 // Error counters
283 Int_t fNumMissedGates; // Total number of missed events
284 Int_t fNumMissedEventBlocks; // Number of groups of missed events
285 Int_t fNumMultSyncErrors; // Number of errors reading the multiplet sync
286 Int_t fNumHelicityErrors; // Number of errors predicting the helicity
287
289
290 /// Flag to disable the printing os missed MPS error messags during
291 /// online running
293
294 protected:
295
296 UInt_t BuildHelicityBitPattern(Int_t patternsize);
297
298 unsigned int parity(unsigned int v) {
299 // http://graphics.stanford.edu/~seander/bithacks.html#ParityParallel
300 v ^= v >> 16;
301 v ^= v >> 8;
302 v ^= v >> 4;
303 v &= 0xf;
304 return (0x6996 >> v) & 1;
305 }
306
307};
Word-level data manipulation and bit operations.
ULong64_t BankID_t
Definition QwTypes.h:21
UInt_t ROCID_t
Definition QwTypes.h:20
HelicityRootSavingType
@ kHelSaveMPS
@ kHelNoSave
Virtual base class for parity analysis subsystems.
Command-line and configuration file options processor.
Definition QwOptions.h:141
Configuration file parser with flexible tokenization and search capabilities.
A helper class to manage a vector of branch entries for ROOT trees.
Definition QwRootFile.h:55
Base class for subsystems implementing container-delegation pattern.
virtual void ConstructHistograms()
Construct the histograms for this subsystem.
static void DefineOptions()
Define options function (note: no virtual static functions in C++)
VQwSubsystem(const TString &name)
Constructor with name.
virtual void ConstructBranchAndVector(TTree *tree, TString &prefix, QwRootTreeBranchVector &values)=0
Construct the branch and tree vector.
void FillTreeVector(QwRootTreeBranchVector &values) const override
Fill the tree vector.
Bool_t CheckForBurpFail(const VQwSubsystem *ev_error) override
Report the number of events failed due to HW and event cut failures.
void SetHelicityBitPattern(TString hex)
void SetHelicityDelay(Int_t delay)
UInt_t GetRandomSeedDelayed()
static const std::vector< UInt_t > kDefaultHelicityBitPattern
void DeaccumulateRunningSum(VQwSubsystem *value, Int_t ErrorMask=0xFFFFFFF) override
remove one entry from the running sums for devices
Int_t GetMinPatternPhase()
std::vector< std::pair< Int_t, Int_t > > fWordsPerSubbank
void CheckPatternNum(VQwSubsystem *value)
static const Bool_t kDEBUG
std::vector< QwWord > fWord
Int_t GetMaxPatternPhase()
Bool_t CollectRandBits24()
UInt_t BuildHelicityBitPattern(Int_t patternsize)
VQwSubsystem & operator=(VQwSubsystem *value) override
Assignment Note: Must be called at the beginning of all subsystems routine call to operator=(VQwSubsy...
Int_t ProcessConfigurationBuffer(const ROCID_t roc_id, const BankID_t bank_id, UInt_t *buffer, UInt_t num_words) override
Int_t fActualPatternPolarity
True polarity of the current pattern.
UInt_t GetRandomSeed(UShort_t *first24randbits)
QwHelicityBase()
Private default constructor (not implemented, will throw linker error on use)
Bool_t IsGoodPatternNumber()
Bool_t fSuppressMPSErrorMsgs
Bool_t IsHelicityIgnored()
UInt_t GetEventcutErrorFlag() override
Return the error flag to the top level routines related to stability checks and ErrorFlag updates.
Int_t GetHelicityActual()
unsigned int parity(unsigned int v)
UInt_t GetRandbit24(UInt_t &ranseed)
virtual void ConstructHistograms()
Construct the histograms for this subsystem.
VQwSubsystem & operator+=(VQwSubsystem *value) override
Long_t GetPatternNumber()
virtual UInt_t GetRandbit(UInt_t &ranseed)
UInt_t kEventTypeHelMinus
Int_t fNumMissedEventBlocks
void CalculateRunningAverage() override
Calculate the average for all good events.
Int_t fPreviousPatternPolarity
True polarity of the previous pattern.
Int_t LoadInputParameters(TString pedestalfile) override
Mandatory parameter file definition.
void FillHistograms() override
Fill the histograms for this subsystem.
void IncrementErrorCounters() override
Increment the error counters.
virtual Bool_t CollectRandBits()
std::vector< UInt_t > fHelicityBitPattern
void Scale(Double_t factor) override
void UpdateErrorFlag(const VQwSubsystem *ev_error) override
update the error flag in the subsystem level from the top level routines related to stability checks....
void PrintErrorCounters() const override
virtual void ClearEventData() override
Bool_t IsGoodPhaseNumber()
Int_t GetHelicityReported()
UInt_t GetRandbit30(UInt_t &ranseed)
void Print() const
void SetEventPatternPhase(Int_t event, Int_t pattern, Int_t phase)
UInt_t GetRandomSeedActual()
Int_t fHelicityDecodingMode
Bool_t MatchActualHelicity(Int_t actual)
virtual Bool_t IsGoodHelicity()
void ConstructBranchAndVector(TTree *tree, TString &prefix, QwRootTreeBranchVector &values) override
Construct the branch and tree vector.
size_t fTreeArrayNumEntries
Int_t LoadChannelMap(TString mapfile) override
Mandatory map file definition.
virtual VQwSubsystem & operator-=(VQwSubsystem *value) override
Int_t LoadEventCuts(TString filename) override
Optional event cut file.
static const Int_t kUndefinedHelicity
Int_t fDelayedPatternPolarity
Reported polarity of the current pattern.
Int_t fPatternPhaseNumberOld
void ConstructBranch(TTree *tree, TString &prefix) override
Construct the branch and tree vector.
void SetFirstBits(UInt_t nbits, UInt_t firstbits)
Long_t GetEventNumber()
void SetHistoTreeSave(const TString &prefix)
void ProcessOptions(QwOptions &options) override
Process the command line options.
Int_t ProcessEvBuffer(const ROCID_t roc_id, const BankID_t bank_id, UInt_t *buffer, UInt_t num_words)
TODO: The non-event-type-aware ProcessEvBuffer routine should be replaced with the event-type-aware v...
Bool_t Compare(VQwSubsystem *source)
void ClearErrorCounters()
Bool_t ApplySingleEventCuts() override
Apply the single event cuts.
Int_t GetHelicityDelayed()
void MergeCounters(VQwSubsystem *value)
Bool_t IsGoodEventNumber()
Bool_t CollectRandBits30()
void AccumulateRunningSum(VQwSubsystem *value, Int_t count=0, Int_t ErrorMask=0xFFFFFFF) override
Update the running sums for devices.
void Ratio(VQwSubsystem *numer, VQwSubsystem *denom) override
virtual ~QwHelicityBase() override
Virtual destructor.
VQwSubsystemParity()
Private default constructor (not implemented, will throw linker error on use)
virtual void FillDB(QwParityDB *, TString)
Fill the database.
virtual void FillErrDB(QwParityDB *, TString)