source: tags/Mars-V1.2/manalysis/MEventRateCalc.cc

Last change on this file was 7804, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 7.2 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()-fNumFirstEvent-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 // Store the time difference between two consecutive events
175 fTimeDiff->SetVal(exec==0 ? -1 : fTimes[n1%n] - fTimes[(n1+n-1)%n]);
176 fTimeDiff->SetReadyToSave();
177
178 // Make sure, that the beginning of data-takeing (open
179 // a new file) doesn't effect the rate too much
180 if (cnt<n/10)
181 return kTRUE;
182
183 // Calculate the rate
184 const Double_t rate = (Double_t)cnt/(fTimes[n1%n]-fTimes[n2%n]);
185
186 // Store the rate
187 fRate->SetRate(exec>1?rate:0, cnt);
188 fRate->SetReadyToSave();
189
190 // Calculate and store the corresponding time
191 const Double_t diff = fTimes[n1%n] - fTimes[n2%n];
192 const Double_t time = fTimes[n2%n] + (cnt-n/10.)/(n-n/10.)*diff/2;
193
194 fTimeRate->SetMean(time, time);
195 fTimeRate->SetReadyToSave();
196
197 /*
198 // Store the corresponding time
199 if (exec==fNumFirstEvent+n)
200 fTimeRate->SetMean(fTimes[n2%n], fTimes[n2%n]);
201 else
202 fTimeRate->SetMean(fTimes[n1%n], fTimes[n2%n]);
203 */
204
205 return kTRUE;
206}
207
208// --------------------------------------------------------------------------
209//
210// Implementation of SavePrimitive. Used to write the call to a constructor
211// to a macro. In the original root implementation it is used to write
212// gui elements to a macro-file.
213//
214void MEventRateCalc::StreamPrimitive(ostream &out) const
215{
216 out << " MEventRateCalc " << GetUniqueName() << "(";
217 if (fName!=gsDefName || fTitle!=gsDefTitle)
218 {
219 out << "\"" <<fName << "\"";
220 if (fTitle!=gsDefTitle)
221 out << ", \"" << fTitle << "\"";
222 }
223 out << ");" << endl;
224
225 if (fTimes.GetSize()!=gsNumEvents)
226 out << " " << GetUniqueName() << ".SetNumEvents(" << fTimes.GetSize() << ");" << endl;
227}
228
229// --------------------------------------------------------------------------
230//
231// Read the setup from a TEnv, eg:
232// MEventRateCalc.NumEvents: 1000
233//
234Int_t MEventRateCalc::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
235{
236 Bool_t rc = kFALSE;
237 if (IsEnvDefined(env, prefix, "NumEvents", print))
238 {
239 rc = kTRUE;
240 SetNumEvents(GetEnvValue(env, prefix, "NumEvents", fTimes.GetSize()));
241 }
242
243 return rc;
244}
Note: See TracBrowser for help on using the repository browser.