source: trunk/MagicSoft/Mars/manalysis/MEventRateCalc.cc@ 6924

Last change on this file since 6924 was 6455, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 7.0 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 11/2003 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2002-2004
21!
22!
23\* ======================================================================== */
24
25//////////////////////////////////////////////////////////////////////////////
26//
27// MEventRateCalc
28//
29// This task calculates the event rates from the event times and numbers.
30//
31// The algorithm is explained in Process()
32//
33//
34// rate = Number of events / time in which the events were recorded
35// r = N / t
36// s = t / N = 1 / r mean time interval between consecutive events
37//
38// for an exponential distribution of the time differences d between
39// consecutive events:
40//
41// s = <d>
42// sigma(d) = <d> = s
43// delta(s) = sigma(d) /sqrt(N) = s / sqrt(N)
44// delta(s) / s = 1 / sqrt(N)
45//
46// delta(r) / r = delta(s) / s = 1 / sqrt(N)
47//
48//
49// In addition the difference between the event time of the current event
50// and the last event is written into a MParameterD calles MTimeDiff.
51//
52//
53// Input Containers:
54// MTime
55//
56// Output Containers:
57// MEventRate
58// MTimeDiff [MParameterD]
59// MTimeRate [MTime] (missing)
60//
61// FIXME: For convinience we could implement a mode which always takes
62// n events to calculate the event rate and sets the corresponding
63// time. This mode could be used to UPADTE files with the event
64// rate.
65//
66//////////////////////////////////////////////////////////////////////////////
67#include "MEventRateCalc.h"
68
69#include <fstream>
70
71#include "MLog.h"
72#include "MLogManip.h"
73
74#include "MParList.h"
75
76#include "MTime.h"
77#include "MEventRate.h"
78#include "MParameters.h"
79
80ClassImp(MEventRateCalc);
81
82using namespace std;
83
84const TString MEventRateCalc::gsDefName = "MEventRateCalc";
85const TString MEventRateCalc::gsDefTitle = "Calculate event rate";
86
87const TString MEventRateCalc::gsNameTime = "MTime";
88const TString MEventRateCalc::gsNameEventRate = "MEventRate";
89const TString MEventRateCalc::gsNameTimeDiff = "MTimeDiff";
90const TString MEventRateCalc::gsNameTimeRate = "MTimeRate";
91
92const Int_t MEventRateCalc::gsNumEvents = 1000;
93
94// --------------------------------------------------------------------------
95//
96// Default constructor.
97//
98MEventRateCalc::MEventRateCalc(const char *name, const char *title)
99 : fNameEventRate(gsNameEventRate), fNameTime(gsNameTime),
100 fNameTimeRate(gsNameTimeRate), fNameTimeDiff(gsNameTimeDiff),
101 fTimes(gsNumEvents)
102{
103 fName = name ? name : gsDefName.Data();
104 fTitle = title ? title : gsDefTitle.Data();
105}
106
107// --------------------------------------------------------------------------
108//
109// The PreProcess searches for the following input containers:
110// MTime
111//
112// The PreProcess searches for the following input containers:
113// MEventRate
114//
115// Reset all times in the buffer
116//
117Int_t MEventRateCalc::PreProcess(MParList *pList)
118{
119 fTime = (MTime*)pList->FindObject(AddSerialNumber(fNameTime), "MTime");
120 if (!fTime)
121 {
122 *fLog << err << AddSerialNumber(fNameTime) << " [MTime] not found... aborting." << endl;
123 return kFALSE;
124 }
125
126 fRate = (MEventRate*)pList->FindCreateObj("MEventRate", AddSerialNumber(fNameEventRate));
127 if (!fRate)
128 return kFALSE;
129
130 fTimeRate = (MTime*)pList->FindCreateObj("MTime", AddSerialNumber(fNameTimeRate));
131 if (!fTimeRate)
132 return kFALSE;
133
134 fTimeDiff = (MParameterD*)pList->FindCreateObj("MParameterD", AddSerialNumber(fNameTimeDiff));
135 if (!fTimeDiff)
136 return kFALSE;
137
138 fTimes.Reset();
139
140 return kTRUE;
141}
142
143Bool_t MEventRateCalc::ReInit(MParList *pList)
144{
145 fNumFirstEvent = GetNumExecutions();
146 return kTRUE;
147}
148
149// --------------------------------------------------------------------------
150//
151// Calculate the events rate as (t1-t0)/n while t1 is the n-th event after
152// t0. If there are not yet enough events in the buffer n is the number
153// of available events. Otherwise the number setup in SetNumEvents.
154//
155Int_t MEventRateCalc::Process()
156{
157 const ULong_t exec = GetNumExecutions()-1;
158
159 //*fLog << all << fNumFirstEvent << " " << exec << endl;
160
161 // Calculate the rate
162 const UInt_t n = fTimes.GetSize();
163
164 // Get the positon in the ring-buffer
165 const UInt_t n1 = exec;
166 const UInt_t n2 = exec>=n ? exec+1 : 0;
167
168 // Store the current event time
169 fTimes[n1%n] = *fTime;
170
171 // Get the number of events
172 const UInt_t cnt = n1<n2 ? n : n1-n2;
173
174 // Calculate the rate
175 const Double_t rate = (Double_t)cnt/(fTimes[n1%n]-fTimes[n2%n]);
176
177 // Store the time difference between two consecutive events
178 fTimeDiff->SetVal(exec==0 ? -1 : fTimes[n1%n] - fTimes[(n1+n-1)%n]);
179 fTimeDiff->SetReadyToSave();
180
181 // Make sure, that the beginning of data-takeing (open
182 // a new file) doesn't effect the rate too much
183 if (exec<fNumFirstEvent+n)
184 {
185 fRate->SetRate(-1, 0);
186 return kTRUE;
187 }
188
189 // Store the rate
190 fRate->SetRate(exec>1?rate:0, cnt);
191 fRate->SetReadyToSave();
192
193 // Store the corresponding time
194 if (exec==fNumFirstEvent+n)
195 fTimeRate->SetMean(fTimes[n2%n], fTimes[n2%n]);
196 else
197 fTimeRate->SetMean(fTimes[n1%n], fTimes[n2%n]);
198
199 return kTRUE;
200}
201
202// --------------------------------------------------------------------------
203//
204// Implementation of SavePrimitive. Used to write the call to a constructor
205// to a macro. In the original root implementation it is used to write
206// gui elements to a macro-file.
207//
208void MEventRateCalc::StreamPrimitive(ofstream &out) const
209{
210 out << " MEventRateCalc " << GetUniqueName() << "(";
211 if (fName!=gsDefName || fTitle!=gsDefTitle)
212 {
213 out << "\"" <<fName << "\"";
214 if (fTitle!=gsDefTitle)
215 out << ", \"" << fTitle << "\"";
216 }
217 out << ");" << endl;
218
219 if (fTimes.GetSize()!=gsNumEvents)
220 out << " " << GetUniqueName() << ".SetNumEvents(" << fTimes.GetSize() << ");" << endl;
221}
222
223// --------------------------------------------------------------------------
224//
225// Read the setup from a TEnv, eg:
226// MEventRateCalc.NumEvents: 1000
227//
228Int_t MEventRateCalc::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
229{
230 Bool_t rc = kFALSE;
231 if (IsEnvDefined(env, prefix, "NumEvents", print))
232 {
233 rc = kTRUE;
234 SetNumEvents(GetEnvValue(env, prefix, "NumEvents", fTimes.GetSize()));
235 }
236
237 return rc;
238}
Note: See TracBrowser for help on using the repository browser.