source: branches/Mars_MC/manalysis/MEventRateCalc.cc@ 17648

Last change on this file since 17648 was 9270, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 7.4 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
143// --------------------------------------------------------------------------
144//
145// This resets the calculation whenever a new file is opened.
146//
147Bool_t MEventRateCalc::ReInit(MParList *pList)
148{
149 fNumFirstEvent = GetNumExecutions();
150 return kTRUE;
151}
152
153// --------------------------------------------------------------------------
154//
155// Calculate the events rate as (t1-t0)/n while t1 is the n-th event after
156// t0. If there are not yet enough events in the buffer n is the number
157// of available events. Otherwise the number setup in SetNumEvents.
158//
159Int_t MEventRateCalc::Process()
160{
161 const ULong_t exec = GetNumExecutions()-fNumFirstEvent-1;
162
163 //*fLog << all << fNumFirstEvent << " " << exec << endl;
164
165 // Calculate the rate
166 const UInt_t n = fTimes.GetSize();
167
168 // Get the positon in the ring-buffer
169 const UInt_t n1 = exec;
170 const UInt_t n2 = exec>=n ? exec+1 : 0;
171
172 // Store the current event time
173 fTimes[n1%n] = *fTime;
174
175 // Get the number of events
176 const UInt_t cnt = n1<n2 ? n : n1-n2;
177
178 // Store the time difference between two consecutive events
179 fTimeDiff->SetVal(exec==0 ? -1 : fTimes[n1%n] - fTimes[(n1+n-1)%n]);
180 fTimeDiff->SetReadyToSave();
181
182 // Make sure, that the beginning of data-takeing (open
183 // a new file) doesn't effect the rate too much
184 if (cnt<n/10)
185 return kTRUE;
186
187 // Calculate the rate
188 const Double_t rate = (Double_t)cnt/(fTimes[n1%n]-fTimes[n2%n]);
189
190 // Store the rate
191 fRate->SetRate(exec>1?rate:0, cnt);
192 fRate->SetReadyToSave();
193
194 // Calculate and store the corresponding time
195 const Double_t diff = fTimes[n1%n] - fTimes[n2%n];
196 const Double_t time = fTimes[n2%n] + (cnt-n/10.)/(n-n/10.)*diff/2;
197
198 fTimeRate->SetMean(time, time);
199 fTimeRate->SetReadyToSave();
200
201 /*
202 // Store the corresponding time
203 if (exec==fNumFirstEvent+n)
204 fTimeRate->SetMean(fTimes[n2%n], fTimes[n2%n]);
205 else
206 fTimeRate->SetMean(fTimes[n1%n], fTimes[n2%n]);
207 */
208
209 return kTRUE;
210}
211
212// --------------------------------------------------------------------------
213//
214// Implementation of SavePrimitive. Used to write the call to a constructor
215// to a macro. In the original root implementation it is used to write
216// gui elements to a macro-file.
217//
218void MEventRateCalc::StreamPrimitive(ostream &out) const
219{
220 out << " MEventRateCalc " << GetUniqueName() << "(";
221 if (fName!=gsDefName || fTitle!=gsDefTitle)
222 {
223 out << "\"" <<fName << "\"";
224 if (fTitle!=gsDefTitle)
225 out << ", \"" << fTitle << "\"";
226 }
227 out << ");" << endl;
228
229 if (fTimes.GetSize()!=gsNumEvents)
230 out << " " << GetUniqueName() << ".SetNumEvents(" << fTimes.GetSize() << ");" << endl;
231}
232
233// --------------------------------------------------------------------------
234//
235// Read the setup from a TEnv, eg:
236// MEventRateCalc.NumEvents: 1000
237//
238Int_t MEventRateCalc::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
239{
240 Bool_t rc = kFALSE;
241 if (IsEnvDefined(env, prefix, "NumEvents", print))
242 {
243 rc = kTRUE;
244 SetNumEvents(GetEnvValue(env, prefix, "NumEvents", fTimes.GetSize()));
245 }
246
247 return rc;
248}
Note: See TracBrowser for help on using the repository browser.