JAPAn
Just Another Parity Analyzer
Loading...
Searching...
No Matches
QwEventRing.cc
Go to the documentation of this file.
1/**
2 * QwEventRing.cc
3 *
4 * Event ring buffer for burp detection and stability monitoring.
5 * Maintains a circular buffer of recent events, applies rolling averages
6 * for stability cuts, and implements burp detection with configurable
7 * extent and precut parameters. Documentation-only edits; runtime
8 * behavior unchanged.
9 */
10
11#include "QwEventRing.h"
12
13/** Constructor: initialize ring buffer with specified size and options. */
15 : fRollingAvg(event), fBurpAvg(event)
16{
17 ProcessOptions(options);
18
19 fEvent_Ring.resize(fRING_SIZE,event);
20
21 bRING_READY = kFALSE;
22 bEVENT_READY = kTRUE;
23
26 fNextToBeRead = 0;
27 countdown = 0;
28
29 //open the log file
30 if (bDEBUG_Write)
31 out_file = fopen("Ring_log.txt", "wt");
32}
33
34
35/** Define command-line options for ring buffer and burp detection parameters. */
37{
38 // Define the execution options
39 options.AddOptions()("ring.size",
40 po::value<int>()->default_value(4800),
41 "QwEventRing: ring/buffer size");
42
43 options.AddOptions()("burp.extent",
44 po::value<int>()->default_value(10),
45 "QwEventRing: burp extent");
46
47 options.AddOptions()("burp.precut",
48 po::value<int>()->default_value(5),
49 "QwEventRing: burp precut");
50
51 options.AddOptions()("burp.holdoff",
52 po::value<int>()->default_value(10),
53 "QwEventRing: burp holdoff");
54
55 options.AddOptions()("ring.stability_cut",
56 po::value<double>()->default_value(1),
57 "QwEventRing: Stability ON/OFF");
58 options.AddOptions()("ring.print-after-unwind",
59 po::value<bool>()->default_bool_value(false),
60 "QwEventRing: print rolling avg after unwind");
61 options.AddOptions()("ring.holdoff",
62 po::value<int>()->default_value(200),
63 "QwEventRing: number of events ignored after the beam trips");
64}
65
66/** Process options and validate ring/burp parameter consistency. */
68{
69 // Reads Event Ring parameters from cmd
70 Double_t stability = 0.0;
71 if (gQwOptions.HasValue("ring.size"))
72 fRING_SIZE=gQwOptions.GetValue<int>("ring.size");
73
74 if (gQwOptions.HasValue("burp.extent"))
75 fBurpExtent=gQwOptions.GetValue<int>("burp.extent");
76
77 if (gQwOptions.HasValue("burp.precut"))
78 fBurpPrecut=gQwOptions.GetValue<int>("burp.precut");
79
80 int tmpval = fBurpExtent;
82 QwWarning << "The burp precut ("<<fBurpPrecut
83 << ") is larger than the burp extent ("
85 << "; this may not be what you meant to do."
86 << QwLog::endl;
87 tmpval = fBurpPrecut;
88 }
89 tmpval += 2;
90 if (fRING_SIZE<tmpval){
91 QwWarning << "Forcing ring size to be " << tmpval
92 << " to accommodate a burp extent of " << fBurpExtent
93 << " and a burp precut of " << fBurpPrecut
94 << "; it had been " << fRING_SIZE << "." << QwLog::endl;
95 fRING_SIZE = tmpval;
96 }
97
98 if (gQwOptions.HasValue("burp.holdoff"))
99 VQwHardwareChannel::SetBurpHoldoff(gQwOptions.GetValue<int>("burp.holdoff"));
100
101 if (gQwOptions.HasValue("ring.stability_cut"))
102 stability=gQwOptions.GetValue<double>("ring.stability_cut");
103
104 if(gQwOptions.HasValue("ring.holdoff"))
105 holdoff=gQwOptions.GetValue<int>("ring.holdoff");
106
107 if (stability>0.0)
108 bStability=kTRUE;
109 else
110 bStability=kFALSE;
111
112 fPrintAfterUnwind = gQwOptions.GetValue<bool>("ring.print-after-unwind");
113}
114/**
115 * Add an event to the ring buffer, applying stability cuts and burp detection.
116 * Updates rolling averages and marks events with beam trip or stability errors.
117 */
119{
120 if (bDEBUG) QwMessage << "QwEventRing::push: BEGIN" <<QwLog::endl;
121
122
123
124 if (bEVENT_READY){
125 Int_t thisevent = fNextToBeFilled;
126 Int_t prevevent = (thisevent+fRING_SIZE-1)%fRING_SIZE;
127 fEvent_Ring[thisevent]=event;//copy the current good event to the ring
128 if (bStability){
129 fRollingAvg.AccumulateAllRunningSum(event);
130 }
131
132
133 if (bDEBUG) QwMessage<<" Filled at "<<thisevent;//<<"Ring count "<<fRing_Count<<QwLog::endl;
134 if (bDEBUG_Write) fprintf(out_file," Filled at %d ",thisevent);
135
136 // Increment fill index
138 fNextToBeFilled = (thisevent + 1) % fRING_SIZE;
139
140 if(fNextToBeFilled == 0){
141 //then we have RING_SIZE events to process
142 if (bDEBUG) QwMessage<<" RING FILLED "<<thisevent; //<<QwLog::endl;
143 if (bDEBUG_Write) fprintf(out_file," RING FILLED ");
144 bRING_READY=kTRUE;//ring is filled with good multiplets
145 fNextToBeFilled=0;//next event to be filled is the first element
146 }
147
148
149 //check for current ramps
150 if (bRING_READY && bStability){
151 fRollingAvg.CalculateRunningAverage();
152 /*
153 //The fRollingAvg dose not contain any regular errorcodes since it only accumulate rolling sum for errorflag==0 event.
154 //The only errorflag it generates is the stability cut failure error when the rolling avg is computed.
155 //Therefore when fRollingAvg.GetEventcutErrorFlag() is called it will return non-zero error code only if a global stability cut has failed
156 //When fRollingAvg.GetEventcutErrorFlag() is called the fErrorFlag of the subsystemarrayparity object will be updated with any global
157 //stability cut failures
158 */
159 fRollingAvg.UpdateErrorFlag(); //to update the global error code in the fRollingAvg
160 if ( fRollingAvg.GetEventcutErrorFlag() != 0 ) {
161 // This test really needs to determine in any of the subelements
162 // might have a local stability cut failure, instead of just this
163 // global stability cut failure.
164 for(Int_t i=0;i<fRING_SIZE;i++){
165 fEvent_Ring[i].UpdateErrorFlag(fRollingAvg);
166 fEvent_Ring[i].UpdateErrorFlag();
167 }
168 }
169 if ((fEvent_Ring[thisevent].GetEventcutErrorFlag() & kBCMErrorFlag)!=0 &&
170 (fEvent_Ring[prevevent].GetEventcutErrorFlag() & kBCMErrorFlag)!=0){
172 }
173 if (countdown > 0) {
174 --countdown;
175 for(Int_t i=0;i<fRING_SIZE;i++){
176 fEvent_Ring[i].UpdateErrorFlag(kBeamTripError);
177 }
178 }
179 }
180 //ring processing is done at a separate location
181
182 this->CheckBurpCut(thisevent);
183 }
184}
185
186
187/**
188 * Retrieve the next event from the ring buffer and deaccumulate from
189 * rolling average if stability monitoring is enabled.
190 *
191 * @return Reference to the retrieved event.
192 */
194 Int_t tempIndex;
195 tempIndex=fNextToBeRead;
196 if (bDEBUG) QwMessage<<" Read at "<<fNextToBeRead<<QwLog::endl;
197 if (bDEBUG_Write) fprintf(out_file," Read at %d \n",fNextToBeRead);
198
199 if (fNextToBeRead==(fRING_SIZE-1)){
200 bRING_READY=kFALSE;//setting to false is an extra measure of security to prevent reading a NULL value.
201 }
202 if (bStability){
203 fRollingAvg.DeaccumulateRunningSum(fEvent_Ring[tempIndex]);
204 }
205
206 // Increment read index
209
210 // Return the event
211 return fEvent_Ring[tempIndex];
212}
213
214
215/** Check if the ring buffer has events ready for retrieval. */
216Bool_t QwEventRing::IsReady(){ //Check for readiness to read data from the ring using the pop() routine
217 return bRING_READY;
218}
219
220/**
221 * Perform burp detection for the specified event, marking preceding events
222 * if a burp is detected and maintaining the burp average window.
223 */
224void QwEventRing::CheckBurpCut(Int_t thisevent)
225{
226 if (bRING_READY || thisevent>fBurpExtent){
227 if (fBurpAvg.CheckForBurpFail(fEvent_Ring[thisevent])){
228 Int_t precut_start = (thisevent+fRING_SIZE-fBurpPrecut)%fRING_SIZE;
229 for(Int_t i=precut_start;i!=(thisevent+1)%fRING_SIZE;i=(i+1)%fRING_SIZE){
230 fEvent_Ring[i].UpdateErrorFlag(fBurpAvg);
231 fEvent_Ring[i].UpdateErrorFlag();
232 }
233 }
234 Int_t beforeburp = (thisevent+fRING_SIZE-fBurpExtent-1)%fRING_SIZE;
235 fBurpAvg.DeaccumulateRunningSum(fEvent_Ring[beforeburp], kPreserveError);
236 }
237 fBurpAvg.AccumulateAllRunningSum(fEvent_Ring[thisevent], 0, kPreserveError);
238
239}
#define gQwOptions
Definition QwOptions.h:31
#define QwWarning
Predefined log drain for warnings.
Definition QwLog.h:44
#define QwMessage
Predefined log drain for regular messages.
Definition QwLog.h:49
static const UInt_t kBCMErrorFlag
Definition QwTypes.h:168
static const UInt_t kPreserveError
Definition QwTypes.h:186
static const UInt_t kBeamTripError
Definition QwTypes.h:181
Event ring buffer for burp detection and stability monitoring.
static std::ostream & endl(std::ostream &)
End of the line.
Definition QwLog.cc:297
Command-line and configuration file options processor.
Definition QwOptions.h:141
po::options_description_easy_init AddOptions(const std::string &blockname="Specialized options")
Add an option to a named block or create new block.
Definition QwOptions.h:170
static void SetBurpHoldoff(Int_t holdoff)
QwSubsystemArrayParity & pop()
Return the last subsystem in the ring.
Int_t fNextToBeFilled
Definition QwEventRing.h:81
FILE * out_file
Definition QwEventRing.h:96
QwSubsystemArrayParity fRollingAvg
Definition QwEventRing.h:93
QwSubsystemArrayParity fBurpAvg
Bool_t bEVENT_READY
Definition QwEventRing.h:86
std::vector< QwSubsystemArrayParity > fEvent_Ring
Definition QwEventRing.h:91
Bool_t bRING_READY
Definition QwEventRing.h:89
void push(QwSubsystemArrayParity &event)
Add the subsystem to the ring.
Bool_t IsReady()
Return the read status of the ring.
void ProcessOptions(QwOptions &options)
Process options.
Bool_t fPrintAfterUnwind
Definition QwEventRing.h:84
Int_t fNumberOfEvents
Definition QwEventRing.h:79
static const Bool_t bDEBUG_Write
Definition QwEventRing.h:98
Int_t fNextToBeRead
Definition QwEventRing.h:82
void CheckBurpCut(Int_t thisevent)
static void DefineOptions(QwOptions &options)
Define options.
Bool_t bStability
Int_t fBurpPrecut
static const Bool_t bDEBUG
Definition QwEventRing.h:97
Int_t fBurpExtent
Int_t fRING_SIZE
Definition QwEventRing.h:77
Subsystem array container specialized for parity analysis with asymmetry calculations.