ROOTANA
manalyzer.h
Go to the documentation of this file.
1 // manalyzer.h
2 
3 #ifndef MANALYZER_H
4 #define MANALYZER_H
5 
6 #include <string>
7 #include <vector>
8 #include <deque>
9 
10 #include <thread>
11 #include <mutex>
12 
13 #include "midasio.h"
14 #include "mvodb.h"
15 
16 class TARootHelper;
18 class TAFlowEvent;
19 
20 class TARunInfo
21 {
22  public:
23  int fRunNo = 0;
24  std::string fFileName;
25  MVOdb* fOdb = NULL;
28  std::vector<std::string> fArgs;
29  static std::vector<std::string> fgFileList;
30  static int fgCurrentFileIndex; // this global variable is initialized in manalyzer.cxx
31 
32  public:
33  TARunInfo(int runno, const char* filename, const std::vector<std::string>& args);
34  ~TARunInfo();
35 
36  public:
39 
40  private:
41  TARunInfo() {}; // hidden default constructor
42 
43  private:
44  std::deque<TAFlowEvent*> fFlowQueue;
45 };
46 
48 {
49  public:
50  TAFlowEvent* fNext = NULL;
51 
52  public:
54  virtual ~TAFlowEvent();
55 
56  template<class T> T* Find()
57  {
58  TAFlowEvent* f = this;
59  while (f) {
60  T *ptr = dynamic_cast<T*>(f);
61  if (ptr) return ptr;
62  f = f->fNext;
63  }
64  return NULL;
65  }
66 
67  private:
68  TAFlowEvent() {}; // hidden default constructor
69 };
70 
71 typedef int TAFlags;
72 
73 #define TAFlag_OK 0
74 #define TAFlag_SKIP (1<<0)
75 #define TAFlag_QUIT (1<<1)
76 #define TAFlag_WRITE (1<<2)
77 #define TAFlag_DISPLAY (1<<3)
78 #define TAFlag_SKIP_PROFILE (1<<4)
79 
81 {
82  public:
83  std::string fModuleName;
84 
85  public:
86  TARunObject(TARunInfo* runinfo); // ctor
87  virtual ~TARunObject() {}; // dtor
88 
89  public:
90  virtual void BeginRun(TARunInfo* runinfo); // begin of run
91  virtual void EndRun(TARunInfo* runinfo); // end of run
92  virtual void NextSubrun(TARunInfo* runinfo); // next subrun file
93 
94  virtual void PauseRun(TARunInfo* runinfo); // pause of run (if online)
95  virtual void ResumeRun(TARunInfo* runinfo); // resume of run (if online)
96 
97  virtual void PreEndRun(TARunInfo* runinfo); // generate flow events before end of run
98 
99  virtual TAFlowEvent* Analyze(TARunInfo* runinfo, TMEvent* event, TAFlags* flags, TAFlowEvent* flow);
100  virtual TAFlowEvent* AnalyzeFlowEvent(TARunInfo* runinfo, TAFlags* flags, TAFlowEvent* flow);
101  virtual void AnalyzeSpecialEvent(TARunInfo* runinfo, TMEvent* event);
102 
103  private:
104  TARunObject(); // hidden default constructor
105 };
106 
108 {
109  public:
110  TAFactory() {}; // ctor
111  virtual ~TAFactory() {}; // dtor
112 
113  public:
114  virtual TARunObject* NewRunObject(TARunInfo* runinfo) = 0; // factory for Run objects
115 
116  public:
117  virtual void Usage(); // Display usage (flags to pass to init etc)
118  virtual void Init(const std::vector<std::string> &args); // start of analysis
119  virtual void Finish(); // end of analysis
120 };
121 
122 template<class T> class TAFactoryTemplate: public TAFactory
123 {
124  T* NewRunObject(TARunInfo* runinfo)
125  {
126  return new T(runinfo);
127  }
128 };
129 
131 {
132  public:
133  TARegister(TAFactory* m);
134  //static void Register(TAModuleInterface* m);
135  //static std::vector<TAModuleInterface*>* fgModules;
136  //static std::vector<TAModuleInterface*>* Get() const;
137 };
138 
139 #ifdef HAVE_ROOT
140 
141 #include "TFile.h"
142 #include "TDirectory.h"
143 #include "TApplication.h"
144 
145 class XmlServer;
146 class THttpServer;
147 
149 {
150  public:
151  static std::string fOutputDirectory;
152  static std::string fOutputFileName;
153  TFile* fOutputFile = NULL;
154  static TDirectory* fgDir;
155  static TApplication* fgApp;
156  static THttpServer* fgHttpServer;
157 
158  public:
159  TARootHelper(const TARunInfo*);
160  ~TARootHelper(); // dtor
161 
162  private:
163  TARootHelper() { }; // hidden default constructor
164 };
165 #endif
166 
167 typedef std::deque<TAFlowEvent*> TAFlowEventQueue;
168 typedef std::deque<TAFlags*> TAFlagsQueue;
169 
171 {
172 public: // per-module queues and threads
173  std::vector<std::mutex> fMtFlowQueueMutex; // queue lock
174  std::vector<TAFlowEventQueue> fMtFlowQueue; // event queue
175  std::vector<TAFlagsQueue> fMtFlagQueue; // flags queue
176  std::vector<std::thread*> fMtThreads; // threads
177  std::vector<bool> fMtThreadIsRunning; // "thread is running" flag
178  std::vector<bool> fMtThreadIsBusy; // "thread is analyzing an event" flag
179 
180 public: // shutdown and quit control
181  bool fMtShutdownRequested = false; // flag to shutdown all threads
182  bool fMtQuitRequested = false; // flag TAFlag_QUIT from per-module threads
183 
184 public: // queue settings
185  int fMtQueueDepth = 0; // maximum number of flow events to queue
186  int fMtQueueFullUSleepTime = 0; // u seconds
187  int fMtQueueEmptyUSleepTime = 0; // u seconds
188 
189 public: // globals
190  static bool gfMultithread;
191  static int gfMtMaxBacklog;
192  static std::mutex gfLock; // Lock for modules to execute code that is not thread safe (many root fitting libraries)
193 
194 public:
195  TAMultithreadHelper(); // ctor
196  ~TAMultithreadHelper(); // dtor
197 };
198 
199 // flag that this version of manalyzer implements the profiler
200 
201 #define HAVE_MANALYZER_PROFILER 1
202 
203 // virtualized clock
204 
205 #include <chrono>
206 typedef std::chrono::high_resolution_clock::time_point TAClock;
207 typedef std::chrono::duration<double> TAClockDuration;
208 inline TAClock TAClockNow() { return std::chrono::high_resolution_clock::now(); };
209 
210 // user-controlled profiler
211 
213 {
214 public:
217 
218 public:
219  const std::string fModuleName;
220 
221 public:
222  TAUserProfilerFlow(TAFlowEvent* flow, const char* name, const TAClock& start); // ctor
223  ~TAUserProfilerFlow(); // dtor
224  double GetTimer() const;
225 };
226 
227 // main program
228 
229 int manalyzer_main(int argc, char* argv[]);
230 
231 #endif
232 
233 /* emacs
234  * Local Variables:
235  * tab-width: 8
236  * c-basic-offset: 3
237  * indent-tabs-mode: nil
238  * End:
239  */
240 
Definition: mvodb.h:21
virtual void Finish()
Definition: manalyzer.cxx:185
virtual void Init(const std::vector< std::string > &args)
Definition: manalyzer.cxx:179
virtual void Usage()
Definition: manalyzer.cxx:173
virtual TARunObject * NewRunObject(TARunInfo *runinfo)=0
virtual ~TAFactory()
Definition: manalyzer.h:111
T * NewRunObject(TARunInfo *runinfo)
Definition: manalyzer.h:124
T * Find()
Definition: manalyzer.h:56
virtual ~TAFlowEvent()
Definition: manalyzer.cxx:90
TAFlowEvent * fNext
Definition: manalyzer.h:50
std::vector< TAFlagsQueue > fMtFlagQueue
Definition: manalyzer.h:175
std::vector< std::mutex > fMtFlowQueueMutex
Definition: manalyzer.h:173
std::vector< bool > fMtThreadIsBusy
Definition: manalyzer.h:178
std::vector< std::thread * > fMtThreads
Definition: manalyzer.h:176
static int gfMtMaxBacklog
Definition: manalyzer.h:191
static bool gfMultithread
Definition: manalyzer.h:190
std::vector< TAFlowEventQueue > fMtFlowQueue
Definition: manalyzer.h:174
std::vector< bool > fMtThreadIsRunning
Definition: manalyzer.h:177
static std::mutex gfLock
Definition: manalyzer.h:192
TARegister(TAFactory *m)
Definition: manalyzer.cxx:457
static std::string fOutputDirectory
Definition: manalyzer.h:151
static TApplication * fgApp
Definition: manalyzer.h:155
static std::string fOutputFileName
Definition: manalyzer.h:152
static THttpServer * fgHttpServer
Definition: manalyzer.h:156
TFile * fOutputFile
Definition: manalyzer.h:153
static TDirectory * fgDir
Definition: manalyzer.h:154
void AddToFlowQueue(TAFlowEvent *)
Definition: manalyzer.cxx:1283
TAFlowEvent * ReadFlowQueue()
Definition: manalyzer.cxx:1273
std::string fFileName
Definition: manalyzer.h:24
std::vector< std::string > fArgs
Definition: manalyzer.h:28
MVOdb * fOdb
Definition: manalyzer.h:25
int fRunNo
Definition: manalyzer.h:23
std::deque< TAFlowEvent * > fFlowQueue
Definition: manalyzer.h:41
static std::vector< std::string > fgFileList
Definition: manalyzer.h:29
TARunInfo()
Definition: manalyzer.h:41
TARootHelper * fRoot
Definition: manalyzer.h:26
TAMultithreadHelper * fMtInfo
Definition: manalyzer.h:27
static int fgCurrentFileIndex
Definition: manalyzer.h:30
virtual void EndRun(TARunInfo *runinfo)
Definition: manalyzer.cxx:117
virtual void ResumeRun(TARunInfo *runinfo)
Definition: manalyzer.cxx:135
virtual void AnalyzeSpecialEvent(TARunInfo *runinfo, TMEvent *event)
Definition: manalyzer.cxx:161
virtual void NextSubrun(TARunInfo *runinfo)
Definition: manalyzer.cxx:123
virtual TAFlowEvent * Analyze(TARunInfo *runinfo, TMEvent *event, TAFlags *flags, TAFlowEvent *flow)
Definition: manalyzer.cxx:147
virtual void PauseRun(TARunInfo *runinfo)
Definition: manalyzer.cxx:129
virtual void BeginRun(TARunInfo *runinfo)
Definition: manalyzer.cxx:111
virtual ~TARunObject()
Definition: manalyzer.h:87
std::string fModuleName
Definition: manalyzer.h:83
virtual TAFlowEvent * AnalyzeFlowEvent(TARunInfo *runinfo, TAFlags *flags, TAFlowEvent *flow)
Definition: manalyzer.cxx:154
virtual void PreEndRun(TARunInfo *runinfo)
Definition: manalyzer.cxx:141
TAUserProfilerFlow(TAFlowEvent *flow, const char *name, const TAClock &start)
Definition: manalyzer.cxx:479
const std::string fModuleName
Definition: manalyzer.h:219
double GetTimer() const
Definition: manalyzer.cxx:489
std::chrono::high_resolution_clock::time_point TAClock
Definition: manalyzer.h:206
int TAFlags
Definition: manalyzer.h:71
int manalyzer_main(int argc, char *argv[])
Definition: manalyzer.cxx:2614
std::chrono::duration< double > TAClockDuration
Definition: manalyzer.h:207
TAClock TAClockNow()
Definition: manalyzer.h:208
std::deque< TAFlowEvent * > TAFlowEventQueue
Definition: manalyzer.h:167
std::deque< TAFlags * > TAFlagsQueue
Definition: manalyzer.h:168