ROOTANA
TRootanaEventLoop.hxx
Go to the documentation of this file.
1 #ifndef TRootanaEventLoop_hxx_seen
2 #define TRootanaEventLoop_hxx_seen
3 
4 // C++ includes
5 #include <stdio.h>
6 #include <sys/time.h>
7 #include <iostream>
8 #include <assert.h>
9 #include <typeinfo>
10 
11 // ROOTANA includes
12 //#include "TMidasFile.h"
13 //#include "TMidasOnline.h"
14 //#include "TMidasEvent.h"
15 //#include "VirtualOdb.h"
16 #include "mvodb.h"
17 #include "TDataContainer.hxx"
18 
19 // ROOT includes
20 #include "TApplication.h"
21 #include "TDirectory.h"
22 #include <TTimer.h>
23 #include <TFile.h>
24 #ifdef HAVE_THTTP_SERVER
25 #include "THttpServer.h"
26 #endif
27 
28 /// This is a base class for event loops that are derived from rootana.
29 ///
30 /// The user should create a class that derives from this TRootanaEventLoop class
31 /// and then fill in the methods that they want to implement.
32 ///
33 /// The user must implement the method ProcessMidasEvent(), which will get executed
34 /// on each event.
35 ///
36 /// The user can also implement methods like Initialize, BeginRun, EndRun, Finalize
37 /// if there are actions they want to execute at certain points.
38 ///
39 /// The event loop will work in both offline and online mode (online only if the user has MIDAS installed).
40 ///
41 /// In example of this type of event loop is shown in examples/analyzer_example.cxx
42 ///
44 
45 public:
46  virtual ~TRootanaEventLoop ();
47 
48 
49  static TRootanaEventLoop& Get(void);
50 
51  /// Method to get the data container that event loop owns.
53 
54 
55  /// The main method, called for each event. Users must implement this
56  /// function!
57  virtual bool ProcessMidasEvent(TDataContainer& dataContainer) = 0;
58  //virtual bool ProcessEvent(TMidasEvent& event) = 0;
59 
60 
61  /// Called after the arguments are processes but before reading the first
62  /// event is read
63  virtual void Initialize(void);
64 
65  /// Called before the first event of a file is read, but you should prefer
66  /// Initialize() for general initialization. This method will be called
67  /// once for each input file.
68  virtual void BeginRun(int transition,int run,int time);
69 
70  /// Called after the last event of a file is read, but you should prefer
71  /// Finalize() for general finalization. This method will be called once
72  /// for each input file.
73  virtual void EndRun(int transition,int run,int time);
74 
75  /// Special version of Init method, to be used only by TRootanaDisplay
76  virtual void InitializeRAD(void){};
77  /// Special version of BOR method, to be used only by TRootanaDisplay
78  /// This is just so that users still have ability to set their own
79  /// BOR methods, in addition to what TRootanaDisplay needs to do at BOR
80  virtual void BeginRunRAD(int transition,int run,int time){};
81  /// Also special version of EOR method, to be used only by TRootanaDisplay
82  virtual void EndRunRAD(int transition,int run,int time){};
83 
84  /// Called after the last event has been processed, but before any open
85  /// output files are closed.
86  virtual void Finalize();
87 
88  /// Called when there is a usage error. This code should print a usage
89  /// message and then return.
90  virtual void Usage(void);
91 
92  /// Check an option and return true if it is valid.
93  /// The return value is used to flag errors during option handling. If
94  /// the options are valid, then CheckOption should return true to indicate
95  /// success. If there is a problem processing the options, then CheckOption
96  /// should return false. If this returns false, then the event loop will
97  /// print the Usage message and exit with a non zero value (i.e. indicate
98  /// failure).
99  virtual bool CheckOption(std::string option);
100 
101  /// The PreFilter method allows user to specify whether to ignore a particular event.
102  /// Specifically, if PreFilter returns
103  ///
104  /// true -> then ProcessMidasEvent will be called
105  /// or
106  /// false -> then ProcessMidasEvent will not be called
107  ///
108  /// This is particularly useful for the RootanaDisplay, where you might
109  /// want to only process and plot certain events.
110  virtual bool PreFilter(TDataContainer& dataContainer){return true;}
111 
112  /// Are we processing online data?
113  bool IsOnline() const {return !fIsOffline;};
114 
115  /// Are we processing offline data?
116  bool IsOffline() const {return fIsOffline;};
117 
118  /// Current Run Number
119  int GetCurrentRunNumber() const {return fCurrentRunNumber;};
120 
121  /// Current Run Number
122  void SetCurrentRunNumber(int run) {fCurrentRunNumber = run;};
123 
124  /// Method to actually process the Midas information, either as file or online.
125  int ExecuteLoop(int argc, char *argv[]);
126 
127  int ProcessMidasFile(TApplication*app,const char*fname);
128 
129 #ifdef HAVE_MIDAS
130  int ProcessMidasOnline(TApplication*app, const char* hostname, const char* exptname);
131 #endif
132 
133 
134  /// This static templated function will make it a little easier
135  /// for users to create the singleton instance.
136  template<typename T>
137  static void CreateSingleton()
138  {
140  std::cout << "Singleton has already been created" << std::endl;
141  else
142  fTRootanaEventLoop = new T();
143  }
144 
145 
146  /// Disable automatic creation of MainWindow
148 
149  /// Use a batch mode, where we don't check ROOT status
150  void UseBatchMode(){ fUseBatchMode = true;}
151 
152  /// Get pointer to ODB variables
153  MVOdb* GetODB(){return fODB;}
154 
155 
156  /// Open output ROOT file
157  void OpenRootFile(int run, std::string midasFilename = std::string(""));
158 
159  /// Cloe output ROOT file
160  void CloseRootFile();
161 
162  /// Check if output ROOT file is valid and open
164  if(fOutputFile) return true;
165  return false;
166  }
167 
168 
169  void DisableRootOutput(bool disable=true){fDisableRootOutput = disable;};
170 
172 
173  /// Set the output filename.
174  /// File name will be $(fOutputFilename)XXX.root, where XXX is run number
175  void SetOutputFilename(std::string name){fOutputFilename = name;};
176 
177  /// This is an alternative, more complicated way of setting the output ROOT filename.
178  /// In this case the user is given the run number and the midas file name and,
179  /// from that information, constructs the output ROOT filename themselves.
180  virtual std::string SetFullOutputFileName(int run, std::string midasFilename){
181  return std::string("");
182  }
183 
184  void SetOnlineName(std::string name){fOnlineName = name;};
185 
186  /// Provide a way to force program to only process certain event IDs.
187  /// This method can be called repeatedly to specify several different event IDs to accept.
188  /// If the method is not called then all eventIDs are accepted.
189  void ProcessThisEventID(int eventID){
190  fProcessEventIDs.push_back(eventID);
191  };
192 
193  /// Little helper method to check if EventID matchs requested EventID list.
194  bool CheckEventID(int eventId);
195 
196  /// Suppress the warning methods regarding old timestamp events for online
197  /// ie warnings about analyzer falling behind data taking.
199 
200  /// Suppress timestamp warnings? true = suppress warnings
202 
203  /// Method to set whether analyzer should operate in GET_RECENT mode,
204  /// where we only process data that is less than 1 second old (this is not default).
205  /// Setting true will use this option.
206  void UseOnlyRecent(bool setting = true);//{ fUseOnlyRecent = setting;};
207 
208  // Set ReadWrite mode fot THttpServer (to allow operation on histograms through web; like histogram reset).
209  void SetTHttpServerReadWrite(bool readwrite = true);
210 
211 protected:
212 
213  bool CreateOutputFile(std::string name, std::string options = "RECREATE"){
214 
215  fOutputFile = new TFile(name.c_str(),options.c_str());
216 
217  return true;
218  }
219 
220 
222 
223  /// The static pointer to the singleton instance.
225 
226  /// TDirectory for online histograms.
228 
229  /// This is a special version of CheckOption that is only used by TRootanaDisplay.
230  /// This is just so that users still have the ability to set options for
231  /// executables derived from TRootanaDisplay.
232  virtual bool CheckOptionRAD(std::string option);
233 
234  /// Also a special version of usage for TRootanaDisplay. See CheckOptionRAD
235  virtual void UsageRAD(void);
236 
237  /// Get pointer to THttpServer, in order to further configure it.
238  /// Warning: pointer will be zero if THttpServer not initialized
239 #ifdef HAVE_THTTP_SERVER
240  THttpServer* GetTHttpServer();
241 #endif
242 
243 private:
244 
245  /// Help Message
246  void PrintHelp();
247 
248  /// Output ROOT file
249  TFile *fOutputFile;
250 
251  /// Base part of the output filename
252  /// File name will be $(fOutputFilename)XXX.root, where XXX is run number
253  std::string fOutputFilename;
254 
255  /// Variable for disabling/enabling Root output
257 
258  /// Pointer to the ODB access instance
260 
261  /// Are we processing offline or online data?
263 
264  /// Current run number
266 
267 
268  /// Pointer to the physics event; the physics event is what we pass to user.
269  /// The midas event is accessible through physics event.
270  /// We make a single instance of the physics event for whole execution,
271  /// because sometimes the decoded information needs to persist
272  /// across multiple midas events.
274 
275  /// This is the set of eventIDs to process
276  std::vector<int> fProcessEventIDs;
277 
278  // ________________________________________________
279  // Variables for online analysis
280 
281  /// Buffer to connect to
282  std::string fBufferName;
283 
284  /// Name of program, as seen by MIDAS.
285  std::string fOnlineName;
286 
287  /// Bool for suppressing the warnings about old timestamps.
289 
290  // ________________________________________________
291  // Variables for offline analysis
293 
294  // The TApplication...
295  TApplication *fApp;
296 
297  // Should we automatically create a Main Window?
299 
300  // Use a batch mode.
302 
303 
304 
305 
306 };
307 #endif
Definition: mvodb.h:21
void DisableAutoMainWindow()
Disable automatic creation of MainWindow.
void ProcessThisEventID(int eventID)
TFile * fOutputFile
Output ROOT file.
bool fDisableRootOutput
Variable for disabling/enabling Root output.
std::string fBufferName
Buffer to connect to.
bool GetSuppressTimestampWarnings()
Suppress timestamp warnings? true = suppress warnings.
static TRootanaEventLoop & Get(void)
bool fIsOffline
Are we processing offline or online data?
virtual void Initialize(void)
virtual bool CheckOptionRAD(std::string option)
virtual bool ProcessMidasEvent(TDataContainer &dataContainer)=0
void DisableRootOutput(bool disable=true)
int GetCurrentRunNumber() const
Current Run Number.
void SetOutputFilename(std::string name)
void PrintHelp()
Help Message.
void SetCurrentRunNumber(int run)
Current Run Number.
TDataContainer * fDataContainer
bool IsRootFileValid()
Check if output ROOT file is valid and open.
bool CreateOutputFile(std::string name, std::string options="RECREATE")
MVOdb * GetODB()
Get pointer to ODB variables.
void UseOnlyRecent(bool setting=true)
void UseBatchMode()
Use a batch mode, where we don't check ROOT status.
void OpenRootFile(int run, std::string midasFilename=std::string(""))
Open output ROOT file.
std::vector< int > fProcessEventIDs
This is the set of eventIDs to process.
virtual void BeginRun(int transition, int run, int time)
virtual std::string SetFullOutputFileName(int run, std::string midasFilename)
virtual bool CheckOption(std::string option)
int fCurrentRunNumber
Current run number.
int ExecuteLoop(int argc, char *argv[])
Method to actually process the Midas information, either as file or online.
bool CheckEventID(int eventId)
Little helper method to check if EventID matchs requested EventID list.
static TRootanaEventLoop * fTRootanaEventLoop
The static pointer to the singleton instance.
void CloseRootFile()
Cloe output ROOT file.
virtual void UsageRAD(void)
Also a special version of usage for TRootanaDisplay. See CheckOptionRAD.
bool fSuppressTimestampWarnings
Bool for suppressing the warnings about old timestamps.
MVOdb * fODB
Pointer to the ODB access instance.
virtual void EndRun(int transition, int run, int time)
bool IsOffline() const
Are we processing offline data?
bool IsOnline() const
Are we processing online data?
virtual void BeginRunRAD(int transition, int run, int time)
virtual void InitializeRAD(void)
Special version of Init method, to be used only by TRootanaDisplay.
virtual void EndRunRAD(int transition, int run, int time)
Also special version of EOR method, to be used only by TRootanaDisplay.
int ProcessMidasOnline(TApplication *app, const char *hostname, const char *exptname)
virtual void Finalize()
virtual void Usage(void)
void SetOnlineName(std::string name)
static void CreateSingleton()
TDataContainer * GetDataContainer()
Method to get the data container that event loop owns.
std::string fOnlineName
Name of program, as seen by MIDAS.
void SetTHttpServerReadWrite(bool readwrite=true)
TDirectory * fOnlineHistDir
TDirectory for online histograms.
virtual bool PreFilter(TDataContainer &dataContainer)
int ProcessMidasFile(TApplication *app, const char *fname)