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

Last change on this file since 4858 was 4833, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 5.9 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";
90
91const Int_t MEventRateCalc::gsNumEvents = 1000;
92
93// --------------------------------------------------------------------------
94//
95// Default constructor.
96//
97MEventRateCalc::MEventRateCalc(const char *name, const char *title)
98 : fNameTime(gsNameTime), fNameEventRate(gsNameEventRate),
99 fNameTimeDiff(gsNameTimeDiff), fTimes(gsNumEvents)
100{
101 fName = name ? name : gsDefName.Data();
102 fTitle = title ? title : gsDefTitle.Data();
103}
104
105// --------------------------------------------------------------------------
106//
107// The PreProcess searches for the following input containers:
108// MTime
109//
110// The PreProcess searches for the following input containers:
111// MEventRate
112//
113// Reset all times in the buffer
114//
115Int_t MEventRateCalc::PreProcess(MParList *pList)
116{
117 fTime = (MTime*)pList->FindObject(AddSerialNumber(fNameTime), "MTime");
118 if (!fTime)
119 {
120 *fLog << err << "MTime not found... aborting." << endl;
121 return kFALSE;
122 }
123
124 fRate = (MEventRate*)pList->FindCreateObj("MEventRate", AddSerialNumber(fNameEventRate));
125 if (!fRate)
126 return kFALSE;
127
128 fTimeDiff = (MParameterD*)pList->FindCreateObj("MParameterD", AddSerialNumber(fNameTimeDiff));
129 if (!fTimeDiff)
130 return kFALSE;
131
132 fTimes.Reset();
133
134 return kTRUE;
135}
136
137// --------------------------------------------------------------------------
138//
139// Calculate the events rate as (t1-t0)/n while t1 is the n-th event after
140// t0. If there are not yet enough events in the buffer n is the number
141// of available events. Otherwise the number setup in SetNumEvents.
142//
143Int_t MEventRateCalc::Process()
144{
145 const ULong_t exec = GetNumExecutions()-1;
146
147 const UInt_t n = fTimes.GetSize();
148
149 const UInt_t n1 = exec;
150 const UInt_t n2 = exec>=n ? exec+1 : 0;
151
152 fTimes[n1%n] = *fTime;
153
154 const UInt_t cnt = n1<n2 ? n : n1-n2;
155
156 const Double_t rate = (Double_t)cnt/(fTimes[n1%n]-fTimes[n2%n]);
157
158 fRate->SetRate(exec>1?rate:0, cnt);
159 fRate->SetReadyToSave();
160
161 fTimeDiff->SetVal(exec==0 ? -1 : fTimes[n1%n] - fTimes[(n1+n-1)%n]);
162 fTimeDiff->SetReadyToSave();
163
164 return kTRUE;
165}
166
167// --------------------------------------------------------------------------
168//
169// Implementation of SavePrimitive. Used to write the call to a constructor
170// to a macro. In the original root implementation it is used to write
171// gui elements to a macro-file.
172//
173void MEventRateCalc::StreamPrimitive(ofstream &out) const
174{
175 out << " MEventRateCalc " << GetUniqueName() << "(";
176 if (fName!=gsDefName || fTitle!=gsDefTitle)
177 {
178 out << "\"" <<fName << "\"";
179 if (fTitle!=gsDefTitle)
180 out << ", \"" << fTitle << "\"";
181 }
182 out << ");" << endl;
183
184 if (fTimes.GetSize()!=gsNumEvents)
185 out << " " << GetUniqueName() << ".SetNumEvents(" << fTimes.GetSize() << ");" << endl;
186}
187
188// --------------------------------------------------------------------------
189//
190// Read the setup from a TEnv, eg:
191// MEventRateCalc.NumEvents: 1000
192//
193Int_t MEventRateCalc::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
194{
195 Bool_t rc = kFALSE;
196 if (IsEnvDefined(env, prefix, "NumEvents", print))
197 {
198 rc = kTRUE;
199 SetNumEvents(GetEnvValue(env, prefix, "NumEvents", fTimes.GetSize()));
200 }
201
202 return rc;
203}
Note: See TracBrowser for help on using the repository browser.