source: trunk/MagicSoft/Mars/mhist/MHEffOnTime.cc@ 1599

Last change on this file since 1599 was 1411, checked in by wittek, 22 years ago
*** empty log message ***
File size: 13.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): Wolfgang Wittek 5/2002 <mailto:wittek@mppmu.mpg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2002
21!
22!
23\* ======================================================================== */
24
25//////////////////////////////////////////////////////////////////////////////
26// //
27// MHEffOnTime //
28// //
29// calculates the effective on time for each bin in the variable Var //
30// (Var may be time or Theta) //
31// //
32// Important : The sample of events used for this should be as close //
33// to the sample of recorded events as possible. //
34// This means that NO additional cuts (be it quality cuts or //
35// gamma/hadron cuts) should be applied (see MAGIC-TDAS 02-02).//
36// //
37//////////////////////////////////////////////////////////////////////////////
38
39#include "MHEffOnTime.h"
40
41#include <TStyle.h>
42
43#include <TMinuit.h>
44#include <TFitter.h>
45
46#include <TF1.h>
47#include <TH2.h>
48#include <TCanvas.h>
49
50#include "MBinning.h"
51#include "MParList.h"
52
53#include "MLog.h"
54#include "MLogManip.h"
55
56ClassImp(MHEffOnTime);
57
58
59// --------------------------------------------------------------------------
60//
61// Default Constructor. It sets the name of the variable ("Time" or "Theta")
62// and the units of the variable ("[s]" or "[\\circ])")
63//
64MHEffOnTime::MHEffOnTime(const char *varname, const char *unit)
65 : fHEffOn(), fHProb(), fHLambda(), fHRdead()
66{
67 fVarname = varname;
68 fUnit = unit;
69
70 TString strg(fVarname);
71 strg += " ";
72 strg += fUnit;
73
74 //
75 // set the name and title of this object
76 //
77 TString strg1("MHEffOnTime-");
78 strg1 += fVarname;
79 fName = strg1;
80 fTitle = "1-D histogram of Eff On Time";
81
82 // effective on time versus Var
83 fHEffOn.SetName("EffOn");
84 TString strg2("effective On Time vs. ");
85 strg2 += fVarname;
86 fHEffOn.SetTitle(strg2);
87
88 fHEffOn.SetDirectory(NULL);
89
90 fHEffOn.SetXTitle(strg);
91 fHEffOn.SetYTitle("effective ontime [s]");
92
93 // chi2-probability versus Var
94 fHProb.SetName("Chi2-prob");
95 TString strg3("Chi2-prob of OnTimeFit vs. ");
96 strg3 += fVarname;
97 fHProb.SetTitle(strg3);
98
99 fHProb.SetDirectory(NULL);
100
101 fHProb.SetXTitle(strg);
102 fHProb.SetYTitle("chi2-probability");
103
104 // lambda versus Var
105 fHLambda.SetName("lambda");
106 TString strg4("lambda from OnTimeFit vs. ");
107 strg4 += fVarname;
108 fHLambda.SetTitle(strg4);
109
110 fHLambda.SetDirectory(NULL);
111
112 fHLambda.SetXTitle(strg);
113 fHLambda.SetYTitle("\\lambda [Hz]");
114
115 // Rdead versus Var
116 // Rdead is the fraction from real time lost by the dead time
117 fHRdead.SetName("Rdead");
118 TString strg5("Rdead of OnTimeFit vs. ");
119 strg5 += fVarname;
120 fHRdead.SetTitle(strg5);
121
122 fHRdead.SetDirectory(NULL);
123
124 fHRdead.SetXTitle(strg);
125 fHRdead.SetYTitle("Rdead");
126
127}
128
129// -----------------------------------------------------------------------
130//
131// Calculate the effective on time by fitting the distribution of
132// time differences
133//
134void MHEffOnTime::Calc(TH2D *hist, const Bool_t Draw)
135{
136 // Draw != 0 means the distribution of time differences should be drawn
137
138 // nbins = number of Var bins
139 const Int_t nbins = hist->GetNbinsY();
140
141 // start of 'for' loop ---------------------------
142 for (int i=1; i<=nbins; i++)
143 {
144 TString strg0("Calc-");
145 strg0 += fVarname;
146 TH1D &h = *hist->ProjectionX(strg0, i, i, "E");
147
148 // Nmdel = Nm * binwidth, with Nm = number of observed events
149 const Double_t Nmdel = h.Integral("width");
150 const Double_t Nm = h.Integral();
151
152 //...................................................
153 // determine range (yq[0], yq[1]) of time differences
154 // where fit should be performed;
155 // require a fraction >=xq[0] of all entries to lie below yq[0]
156 // and a fraction <=xq[1] of all entries to lie below yq[1];
157 // within the range (yq[0], yq[1]) there must be no empty bin;
158 // choose pedestrian approach as long as GetQuantiles is not available
159
160 Double_t xq[2] = { 0.15, 0.95 };
161 Double_t yq[2];
162
163 // GetQuantiles doesn't seem to be available in root 3.01/06
164 // h.GetQuantiles(2,yq,xq);
165
166 const Int_t jbins = h.GetNbinsX();
167
168 fHEffOn.SetBinContent (i, 1.e-20);
169 fHProb.SetBinContent (i, 1.e-20);
170 fHLambda.SetBinContent(i, 1.e-20);
171 fHRdead.SetBinContent (i, 1.e-20);
172
173 // start of 'if' loop ---------------------------
174 if (Nm > 0.0)
175 {
176 // del is bin width in time difference
177 const Double_t del = Nmdel/Nm;
178
179 Double_t sum1 = 0.0;
180 yq[0] = h.GetBinLowEdge(jbins+1);
181 for (int j=1; j<=jbins; j++)
182 {
183 if (sum1 >= xq[0]*Nm)
184 {
185 yq[0] = h.GetBinLowEdge(j);
186 break;
187 }
188 sum1 += h.GetBinContent(j);
189 }
190
191 Double_t sum2 = 0.0;
192 yq[1] = h.GetBinLowEdge(jbins+1);
193 for (int j=1; j<=jbins; j++)
194 {
195 const Double_t content = h.GetBinContent(j);
196 sum2 += content;
197 if (sum2 >= xq[1]*Nm || content == 0.0)
198 {
199 yq[1] = h.GetBinLowEdge(j);
200 break;
201 }
202 }
203
204 //...................................................
205
206 // parameter 0 = lambda
207 // parameter 1 = N0 with N0 = ideal number of events
208 // parameter 2 = del (fixed) with del = bin width of time difference
209
210 TF1 func("Poisson", " [1]*[2] * [0] * exp(-[0] *x)", yq[0], yq[1]);
211 func.FixParameter(2, del);
212
213 func.SetParameter(0, 100); // [Hz]
214 func.SetParameter(1, Nm);
215
216 func.SetParLimits(0, 0, 1000); // [Hz]
217 func.SetParLimits(1, 0, 10*Nm);
218
219 func.SetParName(0, "lambda");
220 func.SetParName(1, "N0");
221 func.SetParName(2, "del");
222
223 // options : 0 (=zero) do not plot the function
224 // I use integral of function in bin rather than value at bin center
225 // R use the range specified in the function range
226 // Q quiet mode
227 h.Fit("Poisson", "0IRQ");
228
229 const Double_t lambda = func.GetParameter(0);
230 const Double_t N0 = func.GetParameter(1);
231 // const Double_t chi2 = func.GetChisquare();
232 const Double_t Prob = func.GetProb();
233 const Int_t NDF = func.GetNDF();
234
235 // was fit successful ?
236 if (NDF>0 && Prob>0.01 && lambda>0.0 && N0>0.0)
237 {
238 // start error calculation .....................
239
240 Double_t emat[2][2];
241 // Double_t eplus, eminus, eparab, gcc;
242 gMinuit->mnemat(&emat[0][0], 2);
243
244 // for (int m=0; m<2; m++)
245 // {
246 // *fLog << "emat[" << m << "][n] = ";
247 // for (int n=0; n<2; n++)
248 // {
249 // *fLog << emat[m][n] << " ";
250 // }
251 // *fLog << endl;
252 // }
253
254 // *fLog << "eplus, eminus, eparab, gcc :" << endl;
255 // for (int k=0; k<2; k++)
256 // {
257 // gMinuit->mnerrs(k, eplus, eminus, eparab, gcc);
258 // *fLog << eplus << " " << eminus << " " << eparab << " "
259 // << gcc << endl;
260 // }
261
262 // Rdead : fraction of real time lost by the dead time
263 // kappa = number of observed events (Nm) divided by
264 // the number of genuine events (N0)
265 // Teff : effective on-time
266
267 Double_t Rdead, kappa, Teff, dTeff, dldl, dl, dRdead;
268 Double_t dN0dN0;
269 //Double_t dldN0,lN0corr;
270 Teff = Nm/lambda;
271 kappa = Nm/N0;
272 Rdead = 1.0 - kappa;
273
274 dldl = emat[0][0];
275 dN0dN0 = emat[1][1];
276 // dldN0 = emat[0][1];
277
278 dTeff = Teff * sqrt(dldl/(lambda*lambda) + 1.0/Nm);
279 dl = sqrt(dldl);
280 dRdead = kappa * sqrt(dN0dN0/(N0*N0) + 1.0/Nm);
281
282 // lN0corr = dldN0 / sqrt(dldl * dN0dN0);
283 // *fLog << "MHEffOnTime : correlation between lambda and N0 = "
284 // << lN0corr << endl;
285
286 // end error calculation .....................
287
288 // the effective on time is Nm/lambda
289 fHEffOn.SetBinContent(i, Teff);
290 fHEffOn.SetBinError (i, dTeff);
291
292 // plot chi2-probability of fit
293 fHProb.SetBinContent(i, Prob);
294
295 // lambda from fit
296 fHLambda.SetBinContent(i, lambda);
297 fHLambda.SetBinError (i, dl);
298
299 // Rdead from fit
300 fHRdead.SetBinContent(i, Rdead);
301 fHRdead.SetBinError (i,dRdead);
302 }
303
304 //........................
305 // draw the distribution of time differences if requested
306 //
307 if (Draw == kTRUE)
308 {
309 char txt[100];
310 TString strg1(fVarname);
311 strg1 += "-bin %d";
312 sprintf(txt, strg1, i);
313 new TCanvas(txt, txt);
314 // TCanvas &c = *MakeDefCanvas(txt, txt);
315 // gROOT->SetSelectedPad(NULL);
316
317 gPad->SetLogy();
318 gStyle->SetOptFit(1011);
319 // gStyle->SetOptStat(1);
320
321 h.SetName(txt);
322 h.SetXTitle("time difference [s]");
323 h.SetYTitle("Counts");
324 h.DrawCopy();
325
326 func.SetRange(yq[0], yq[1]); // Range of Drawing
327 func.DrawCopy("same");
328
329 // c.Modified();
330 // c.Update();
331 }
332 //........................
333 }
334 // end of 'if' loop ---------------------------
335
336 delete &h;
337 }
338 // end of 'for' loop ---------------------------
339 // gStyle->SetOptStat(1111);
340
341}
342
343// -------------------------------------------------------------------------
344//
345// Set the binnings and prepare the filling of the histograms
346//
347Bool_t MHEffOnTime::SetupFill(const MParList *plist)
348{
349 TString strg0("Binning");
350 strg0 += fVarname;
351
352 const MBinning* binsVar = (MBinning*)plist->FindObject(strg0);
353
354 if (!binsVar)
355 {
356 *fLog << err << dbginf << strg0
357 << " [MBinning] not found... aborting." << endl;
358 return kFALSE;
359 }
360
361 SetBinning(&fHEffOn, binsVar);
362 SetBinning(&fHProb, binsVar);
363 SetBinning(&fHLambda, binsVar);
364 SetBinning(&fHRdead, binsVar);
365
366 fHEffOn.Sumw2();
367 fHProb.Sumw2();
368 fHLambda.Sumw2();
369 fHRdead.Sumw2();
370
371 return kTRUE;
372}
373
374// -------------------------------------------------------------------------
375//
376// Dummy Fill
377//
378Bool_t MHEffOnTime::Fill(const MParContainer *par)
379{
380 return kTRUE;
381}
382
383// -------------------------------------------------------------------------
384//
385// Draw a copy of the histogram
386//
387TObject *MHEffOnTime::DrawClone(Option_t *opt) const
388{
389 TString strg0("EffOnTime");
390 strg0 += fVarname;
391 TString strg1("Results from on time fit vs. ");
392 strg1 += fVarname;
393
394 TCanvas &c = *MakeDefCanvas(strg0, strg1);
395 c.Divide(2, 2);
396
397 gROOT->SetSelectedPad(NULL);
398
399 c.cd(1);
400 ((TH2*)&fHEffOn)->DrawCopy(opt);
401
402 c.cd(2);
403 ((TH2*)&fHProb)->DrawCopy(opt);
404
405 c.cd(3);
406 ((TH2*)&fHLambda)->DrawCopy(opt);
407
408 c.cd(4);
409 ((TH2*)&fHRdead)->DrawCopy(opt);
410
411 c.Modified();
412 c.Update();
413
414 return &c;
415}
416
417// -------------------------------------------------------------------------
418//
419// Draw the histogram
420//
421void MHEffOnTime::Draw(Option_t *opt)
422{
423 TString strg0("EffOnTime");
424 strg0 += fVarname;
425 TString strg1("Results from on time fit vs. ");
426 strg1 += fVarname;
427
428 if (!gPad)
429 MakeDefCanvas(strg0, strg1);
430
431 gPad->Divide(2,2);
432
433 gPad->cd(1);
434 fHEffOn.Draw(opt);
435
436 gPad->cd(2);
437 fHProb.Draw(opt);
438
439 gPad->cd(3);
440 fHLambda.Draw(opt);
441
442 gPad->cd(4);
443 fHRdead.Draw(opt);
444
445 gPad->Modified();
446 gPad->Update();
447}
448
449
450
451
452
453
Note: See TracBrowser for help on using the repository browser.