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

Last change on this file since 5232 was 4887, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 6.5 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// Calculate the events rate as (t1-t0)/n while t1 is the n-th event after
146// t0. If there are not yet enough events in the buffer n is the number
147// of available events. Otherwise the number setup in SetNumEvents.
148//
149Int_t MEventRateCalc::Process()
150{
151 const ULong_t exec = GetNumExecutions()-1;
152
153 // Calculate the rate
154 const UInt_t n = fTimes.GetSize();
155
156 // Get the positon in the ring-buffer
157 const UInt_t n1 = exec;
158 const UInt_t n2 = exec>=n ? exec+1 : 0;
159
160 // Store the current event time
161 fTimes[n1%n] = *fTime;
162
163 // Get the number of events
164 const UInt_t cnt = n1<n2 ? n : n1-n2;
165
166 // Calculate the rate
167 const Double_t rate = (Double_t)cnt/(fTimes[n1%n]-fTimes[n2%n]);
168
169 // Store the time difference between two consecutive events
170 fTimeDiff->SetVal(exec==0 ? -1 : fTimes[n1%n] - fTimes[(n1+n-1)%n]);
171 fTimeDiff->SetReadyToSave();
172
173 // Store the rate
174 fRate->SetRate(exec>1?rate:0, cnt);
175 fRate->SetReadyToSave();
176
177 // Store the corresponding time
178 fTimeRate->SetMean(fTimes[n1%n], fTimes[n2%n]);
179
180 return kTRUE;
181}
182
183// --------------------------------------------------------------------------
184//
185// Implementation of SavePrimitive. Used to write the call to a constructor
186// to a macro. In the original root implementation it is used to write
187// gui elements to a macro-file.
188//
189void MEventRateCalc::StreamPrimitive(ofstream &out) const
190{
191 out << " MEventRateCalc " << GetUniqueName() << "(";
192 if (fName!=gsDefName || fTitle!=gsDefTitle)
193 {
194 out << "\"" <<fName << "\"";
195 if (fTitle!=gsDefTitle)
196 out << ", \"" << fTitle << "\"";
197 }
198 out << ");" << endl;
199
200 if (fTimes.GetSize()!=gsNumEvents)
201 out << " " << GetUniqueName() << ".SetNumEvents(" << fTimes.GetSize() << ");" << endl;
202}
203
204// --------------------------------------------------------------------------
205//
206// Read the setup from a TEnv, eg:
207// MEventRateCalc.NumEvents: 1000
208//
209Int_t MEventRateCalc::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
210{
211 Bool_t rc = kFALSE;
212 if (IsEnvDefined(env, prefix, "NumEvents", print))
213 {
214 rc = kTRUE;
215 SetNumEvents(GetEnvValue(env, prefix, "NumEvents", fTimes.GetSize()));
216 }
217
218 return rc;
219}
Note: See TracBrowser for help on using the repository browser.