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