00001 #ifndef BASE_STREAMPROC_H
00002 #define BASE_STREAMPROC_H
00003
00004 #include <vector>
00005
00006 #include <string>
00007
00008 #include "base/Buffer.h"
00009 #include "base/SubEvent.h"
00010
00011 namespace base {
00012
00018 class ProcMgr;
00019 class Event;
00020
00021 typedef void* H1handle;
00022 typedef void* H2handle;
00023 typedef void* C1handle;
00024
00025
00026 struct SyncMarker {
00027 unsigned uniqueid;
00028 unsigned localid;
00029 LocalStamp_t local_stamp;
00030 GlobalTime_t localtm;
00031 GlobalTime_t globaltm;
00032 unsigned bufid;
00033
00034 SyncMarker() : uniqueid(0), localid(0), local_stamp(0), localtm(0.), globaltm(0.), bufid(0) {}
00035 };
00036
00037
00038 struct LocalTriggerMarker {
00039 GlobalTime_t localtm;
00040 unsigned localid;
00041 unsigned bufid;
00042
00043 LocalTriggerMarker() : localtm(0.), localid(0), bufid(0) {}
00044 };
00045
00046
00047 struct GlobalTriggerMarker {
00048 GlobalTime_t globaltm;
00049 GlobalTime_t lefttm;
00050 GlobalTime_t righttm;
00051
00052 SubEvent* subev;
00053 bool isflush;
00054
00055 GlobalTriggerMarker(GlobalTime_t tm = 0.) :
00056 globaltm(tm), lefttm(0.), righttm(0.), subev(0), isflush(false) {}
00057
00058 GlobalTriggerMarker(const GlobalTriggerMarker& src) :
00059 globaltm(src.globaltm), lefttm(src.lefttm), righttm(src.righttm), subev(src.subev), isflush(src.isflush) {}
00060
00061 ~GlobalTriggerMarker() { }
00062
00063 bool null() const { return globaltm<=0.; }
00064 void reset() { globaltm = 0.; isflush = false; }
00065
00067 bool normal() const { return !isflush; }
00068
00069 void SetInterval(double left, double right);
00070
00071 int TestHitTime(const GlobalTime_t& hittime, double* dist = 0);
00072 };
00073
00074
00075 typedef std::vector<base::GlobalTriggerMarker> GlobalTriggerMarksQueue;
00076
00077
00078 class StreamProc {
00079 friend class ProcMgr;
00080
00081 protected:
00082
00083 typedef std::vector<base::Buffer> BuffersQueue;
00084
00085 typedef std::vector<base::SyncMarker> SyncMarksQueue;
00086
00087 typedef std::vector<base::LocalTriggerMarker> LocalTriggerMarksQueue;
00088
00089 std::string fName;
00090
00091 ProcMgr* fMgr;
00092
00093 BuffersQueue fQueue;
00094
00095 unsigned fQueueScanIndex;
00096 unsigned fQueueScanIndexTm;
00097
00098 bool fIsSynchronisationRequired;
00099 SyncMarksQueue fSyncs;
00100 unsigned fSyncScanIndex;
00101 bool fSyncFlag;
00102
00103 LocalTriggerMarksQueue fLocalTrig;
00104
00105 GlobalTriggerMarksQueue fGlobalTrig;
00106
00107 unsigned fGlobalTrigScanIndex;
00108 unsigned fGlobalTrigRightIndex;
00109
00110 bool fTimeSorting;
00111
00112 std::string fPrefix;
00113 std::string fSubPrefixD;
00114 std::string fSubPrefixN;
00115
00116 base::H1handle fTriggerTm;
00117 base::H1handle fMultipl;
00118
00119 base::C1handle triggerWindow;
00120
00122 StreamProc(const char* name = "", int indx = -1);
00123
00125 void SetSubPrefix(const char* subname = "", int indx = -1, const char* subname2 = "", int indx2 = -1);
00126
00127 H1handle MakeH1(const char* name, const char* title, int nbins, double left, double right, const char* xtitle = 0);
00128 void FillH1(H1handle h1, double x, double weight = 1.);
00129
00130 H2handle MakeH2(const char* name, const char* title, int nbins1, double left1, double right1, int nbins2, double left2, double right2, const char* options = 0);
00131 void FillH2(H1handle h2, double x, double y, double weight = 1.);
00132
00133 C1handle MakeC1(const char* name, double left, double right, H1handle h1 = 0);
00134 void ChangeC1(C1handle c1, double left, double right);
00135 int TestC1(C1handle c1, double value, double* dist = 0);
00136 double GetC1Limit(C1handle c1, bool isleft = true);
00137
00138 void AddSyncMarker(SyncMarker& marker);
00139
00140 void AddTriggerMarker(LocalTriggerMarker& marker);
00141
00142 double local_time_dist(GlobalTime_t tm1, GlobalTime_t tm2) { return tm2-tm1; }
00143
00146 GlobalTime_t LocalToGlobalTime(GlobalTime_t localtm, unsigned* lastindx = 0);
00147
00149 virtual bool doTriggerSelection() const { return false; }
00150
00152 virtual GlobalTime_t ProvidePotentialFlushTime(GlobalTime_t last_marker);
00153
00155 bool VerifyFlushTime(const base::GlobalTime_t& flush_time);
00156
00158 virtual double MaximumDisorderTm() const { return 0.; }
00159
00161 unsigned TestHitTime(const base::GlobalTime_t& hittime, bool normal_hit);
00162
00163 virtual unsigned GetTriggerMultipl(unsigned indx) { return 0; }
00164
00165 virtual void SortDataInSubEvent(base::SubEvent*) {}
00166
00167 public:
00168
00169 virtual ~StreamProc();
00170
00171
00172 ProcMgr* mgr() const { return fMgr; }
00173
00174 const std::string GetProcName() const { return fName; }
00175
00177 void SetTimeSorting(bool on) { fTimeSorting = on; }
00178 bool IsTimeSorting() const { return fTimeSorting; }
00179
00180
00181 void SetTriggerWindow(double left, double right)
00182 {
00183 ChangeC1(triggerWindow, left, right);
00184 }
00185
00186
00191 bool IsSynchronisationRequired() const { return fIsSynchronisationRequired; }
00192
00194 virtual bool AddNextBuffer(const Buffer& buf);
00195
00197 virtual bool ScanNewBuffers();
00198
00200 virtual bool ScanNewBuffersTm();
00201
00203 virtual void SkipAllData();
00204
00206 virtual bool SkipBuffers(unsigned cnt);
00207
00208
00215 virtual bool FirstBufferScan(const base::Buffer& buf) { return false; }
00216
00219 virtual bool SecondBufferScan(const base::Buffer& buf) { return false; }
00220
00222 unsigned numSyncs() const { return fSyncs.size(); }
00223 SyncMarker& getSync(unsigned n) { return fSyncs[n]; }
00224 SyncMarker& lastSync() { return fSyncs[fSyncs.size()-1]; }
00226 bool eraseSyncAt(unsigned indx);
00227
00229 virtual bool CollectTriggers(GlobalTriggerMarksQueue& queue);
00230
00232 virtual bool DistributeTriggers(const GlobalTriggerMarksQueue& queue);
00233
00235 virtual bool ScanDataForNewTriggers();
00236
00238 unsigned NumReadySubevents() const { return fGlobalTrigScanIndex; }
00239
00241 virtual bool AppendSubevent(base::Event* evt);
00242 };
00243
00244 }
00245
00246 #endif