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