Hall A ROOT/C++ Analyzer (podd)
Loading...
Searching...
No Matches
THaDebugModule.cxx
Go to the documentation of this file.
1//*-- Author : Ole Hansen 26-Mar-03
2
4//
5// THaDebugModule
6//
7// Diagnostic class to help troubleshoot physics modules.
8// Prints one or more global variables for every event.
9//
11
12#include "THaDebugModule.h"
13#include "THaVarList.h"
14#include "THaCutList.h"
15#include "THaCut.h"
16#include "THaGlobals.h"
17#include "THaEvData.h"
18#include "TRegexp.h"
19#include "TClass.h"
20
21#include <cstring>
22#include <cctype>
23#include <iostream>
24
25using namespace std;
26
27enum EFlags {
28 kStop, // Wait for key press after every event
29 kCount, // Run for fCount events
30 kQuiet // Run quietly (don't print variables)
31};
32
33//_____________________________________________________________________________
34THaDebugModule::THaDebugModule( const char* var_list, const char* test )
35 : THaPhysicsModule("DebugModule", var_list)
36 , fVarString(var_list)
37 , fFlags(0)
38 , fCount(0)
39 , fTestExpr(test)
40 , fTest(nullptr)
41{
42 // Normal constructor.
43
45 fIsSetup = false;
46}
47
48//_____________________________________________________________________________
50{
51 // Destructor
52 delete fTest;
53}
54
55//_____________________________________________________________________________
57{
58 // Initialize the module. This just clears the list of variables.
59 // The actual initialization takes place in ParseList()
60
61 fVars.clear();
62
63 if( THaPhysicsModule::Init(run_time) )
64 return fStatus;
65
66 // fIsSetup is set by the default DefineVariables, but we use it as a flag
67 // to trigger lazy initialization of our variables/cuts list in Process(),
68 // so we need to unset it explicitly here.
69 fIsSetup = false;
70
71 return fStatus = kOK;
72}
73
74
75//_____________________________________________________________________________
76Int_t THaDebugModule::THaDebugModule::ParseList()
77{
78 // Parse the list of variable/cut names and store pointers
79 // to the found objects. Called from Process() the first time
80 // this module is used.
81
82 Int_t nopt = fVarString.GetNOptions();
83 if( nopt > 0 ) {
84 for( Int_t i = 0; i < nopt; i++ ) {
85 const char* opt = fVarString.GetOption(i);
86 if( !strcmp(opt, "NOSTOP") )
87 CLRBIT(fFlags, kStop);
88 else {
89 // Regexp matching
90 bool found = false;
91 TRegexp re(opt, true);
92 // We can inspect analysis variables and cuts/tests
93 if( gHaVars ) {
94 TIter next(gHaVars);
95 while( TObject* obj = next() ) {
96 TString s = obj->GetName();
97 if( s.Index(re) != kNPOS ) {
98 found = true;
99 fVars.push_back(obj);
100 }
101 }
102 }
103 if( gHaCuts ) {
104 const TList* lst = gHaCuts->GetCutList();
105 if( lst ) {
106 TIter next(lst);
107 while( TObject* obj = next() ) {
108 TString s = obj->GetName();
109 if( s.Index(re) != kNPOS ) {
110 found = true;
111 fVars.push_back(obj);
112 }
113 }
114 }
115 }
116 if( !found ) {
117 Warning(Here("ParseList"),
118 "Global variable or cut %s not found. Skipped.", opt);
119 }
120 }
121 }
122 }
123 return 0;
124}
125
126//_____________________________________________________________________________
128{
129 // Print details of the defined variables
130
132 if( fIsSetup ) {
133 if( !fTestExpr.IsNull() ) {
134 if( fTest )
135 fTest->Print();
136 else
137 cout << "Test name: " << fTestExpr << " (undefined)\n";
138 }
139 cout << "Number of variables: " << fVars.size() << endl;
140 for( const auto* obj: fVars ) {
141 cout << obj->GetName() << " ";
142 }
143 cout << endl;
144 } else {
145 if( !fTestExpr.IsNull() )
146 cout << "Test name: " << fTestExpr << endl;
147 cout << "(Module not initialized)\n";
148 }
149}
150
151//_____________________________________________________________________________
152void THaDebugModule::PrintEvNum( const THaEvData& evdata ) const
153{
154 // Print current event number
155 cout << "======>>>>>> Event " << (UInt_t) evdata.GetEvNum() << endl;
156}
157
158//_____________________________________________________________________________
160{
161 // Print the variables for every event and wait for user input.
162
163 // We have to set up the test here because physics modules' Init() is
164 // called before the analyzer's tests are loaded, and we want to be
165 // able to use existing tests.
166 if( !fIsSetup ) {
167 ParseList();
168 if( !fTestExpr.IsNull() ) {
169 fTest = new THaCut(fName + "_Test", fTestExpr, fName + "_Block");
170 // Expression error?
171 if( !fTest || fTest->IsZombie() ) {
172 delete fTest;
173 fTest = nullptr;
174 }
175 }
176 fIsSetup = true;
177 }
178 bool good = !fTest || fTest->EvalCut();
179
180 // Print() the variables
181 if( good && !TESTBIT(fFlags, kQuiet) ) {
182 PrintEvNum(evdata);
183 for( const auto& var: fVars ) {
184 const char* opt = "";
185 if( var->IsA()->InheritsFrom("THaVar") )
186 opt = "FULL";
187 var->Print(opt);
188 }
189 }
190
191 if( TESTBIT(fFlags, kCount) && --fCount <= 0 ) {
194 if( !good )
195 PrintEvNum(evdata);
196 good = true;
197 }
198 if( good && TESTBIT(fFlags, kStop) ) {
199 // Wait for user input
200 cout << "RETURN: continue, H: run 100 events, R: run to end, F: finish quietly, Q: quit\n";
201 char c = 0;
202 cin.clear();
203 while( !cin.eof() && cin.get(c) && !strchr("\nqQhHrRfF", c) ) {}
204 if( c != '\n' )
205 while( !cin.eof() && cin.get() != '\n' ) {}
206 if( tolower(c) == 'q' )
207 return kTerminate;
208 else if( tolower(c) == 'h' ) {
211 fCount = 100;
212 } else if( tolower(c) == 'r' )
214 else if( tolower(c) == 'f' ) {
217 }
218 }
219
220 return 0;
221}
222
int Int_t
unsigned int UInt_t
unsigned int fFlags
#define c(i)
const char Option_t
#define SETBIT(n, i)
#define TESTBIT(n, i)
#define CLRBIT(n, i)
@ kQuiet
@ kStop
@ kCount
R__EXTERN class THaVarList * gHaVars
Definition THaGlobals.h:11
R__EXTERN class THaCutList * gHaCuts
Definition THaGlobals.h:12
const THashList * GetCutList() const
Definition THaCutList.h:61
Bool_t EvalCut()
Definition THaCut.h:31
virtual void Print(Option_t *opt="") const
Definition THaCut.cxx:200
std::vector< const TObject * > fVars
void PrintEvNum(const THaEvData &) const
THaDebugModule(const char *var_list, const char *test="")
virtual ~THaDebugModule()
Int_t ParseList()
virtual Int_t Process(const THaEvData &evdata)
virtual void Print(Option_t *opt="") const
UInt_t GetEvNum() const
Definition THaEvData.h:56
void Print(Option_t *option="") const override
TString fName
virtual Option_t * GetOption() const
R__ALWAYS_INLINE Bool_t IsZombie() const
Bool_t IsNull() const
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
void Warning(const char *location, const char *fmt,...)
STL namespace.
ClassImp(TPyArg)