source: trunk/MagicSoft/Mars/mhflux/MHEffectiveOnTime.cc@ 8929

Last change on this file since 8929 was 8929, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 32.7 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, 8/2002 <mailto:tbretz@astro.uni-wuerzburg.de>
19! Author(s): Wolfgang Wittek, 1/2002 <mailto:wittek@mppmu.mpg.de>
20!
21! Copyright: MAGIC Software Development, 2000-2008
22!
23!
24\* ======================================================================== */
25
26//////////////////////////////////////////////////////////////////////////////
27//
28// MHEffectiveOnTime
29//
30// Filling this you will get the effective on-time versus theta and
31// observation time.
32//
33// From this histogram the effective on-time is determined by a fit.
34// The result of the fit (see Fit()) and the fit-parameters (like chi^2)
35// are stored in corresponding histograms
36//
37// To determin the efective on time a poisson fit is done. For more details
38// please have a look into the source code of FitH() it should be simple
39// to understand. In this function a Delta-T distribution is fitted, while
40// Delta-T is the time between two consecutive events.
41//
42// The fit is done for projections of a 2D histogram in Theta and Delta T.
43// So you get the effective on time versus theta.
44//
45// To get the effective on-time versus time a histogram is filled with
46// the Delta-T distribution of a number of events set by SetNumEvents().
47// The default is 12000 (roughly 1min at 200Hz)
48//
49// For each "time-bin" the histogram is fitted and the resulting effective
50// on-time is stored in the fHTimeEffOn histogram. Each entry in this
51// histogram is the effective observation time between the upper and
52// lower edges of the bins.
53//
54// In addition the calculated effective on time is stored in a
55// "MEffectiveOnTime [MParameterDerr]" and the corresponding time-stamp
56// (the upper edge of the bin) "MTimeEffectiveOnTime [MTime]"
57//
58// The class takes two binnings from the Parameter list; if these binnings
59// are not available the defaultbinning is used:
60// MBinning("BinningDeltaT"); // Units of seconds
61// MBinning("BinningTheta"); // Units of degrees
62//
63//
64// Usage:
65// ------
66// MFillH fill("MHEffectiveOnTime", "MTime");
67// tlist.AddToList(&fill);
68//
69//
70// Input Container:
71// MPointingPos
72// MRawRunHeader
73// MTime
74//
75// Output Container:
76// MEffectiveOnTime [MParameterDerr]
77// MTimeEffectiveOnTime [MTime]
78//
79//
80// Class version 2:
81// ----------------
82// + UInt_t fFirstBin;
83// + UInt_t fNumEvents;
84// - Int_t fNumEvents;
85//
86// Class version 3:
87// ----------------
88// + Double_t fTotalTime;
89//
90//
91// ==========================================================================
92// Dear Colleagues,
93//
94// for the case that we are taking calibration events interleaved with
95// cosmics events the calculation of the effective observation time has to
96// be modified. I have summarized the proposed procedures in the note at the
97// end of this message. The formulas have been checked by a simulation.
98//
99// Comments are welcome.
100//
101// Regards, Wolfgang
102// --------------------------------------------------------------------------
103// Wolfgang Wittek
104// 2 Dec. 2004
105//
106// Calculation of the effective observation time when cosmics and calibration
107// events are taken simultaneously.
108// --------------------------------
109//
110// I. Introduction
111// ---------------
112// It is planned to take light calibration events (at a certain fixed frequency
113// lambda_calib) interlaced with cosmics events. The advantages of this
114// procedure are :
115//
116// - the pedestals, which would be determined from the cosmics, could be
117// used for both the calibration and the cosmics events
118//
119// - because calibration and cosmics events are taken quasi simultaneously,
120// rapid variations (in the order of a few minutes) of base lines and of the
121// photon/ADC conversion factors could be recognized and taken into account
122//
123// The effective observation time T_eff is defined as that time range, within
124// which the recorded number of events N_cosmics would be obtained under ideal
125// conditions (only cosmics, no dead time, no calibration events, ...).
126//
127// In the absence of calibration events the effective observation time can
128// be determined from the distribution of time differences 'dt' between
129// successive cosmics events (see first figure in the attached ps file).
130// The exponential slope 'lambda' of this distribution is the ideal cosmics
131// event rate. If 'N_cosmics' is the total number of recorded cosmics events,
132// T_eff is obtained by
133//
134// T_eff = N_cosmics / lambda
135//
136// In the case of a finite dead time 'dead', the distribution (for dt > dead) is
137// still exponential with the same slope 'lambda'. 'lambda' should be determined
138// in a region of 'dt' which is not affected by the dead time, i.e. at not too
139// low 'dt'.
140//
141//
142//
143// II. Problems in the presence of calibration events
144// --------------------------------------------------
145// If calibration events are taken interlaced with cosmics, and if the dead time
146// is negligible, the distribution of time differences 'dt' between cosmics can
147// be used for calculating the effective observation time, as if the calibration
148// events were not present.
149//
150// In the case of a non-negligible dead time 'dead', however, the distribution of
151// time differences between cosmics is distorted, because a cosmics event may be
152// lost due to the dead time after a calibration event. Even if the time
153// intervals are ignored which contain a calibration event,
154//
155//
156// ---|---------o--------|---------> t
157//
158// cosmics calib cosmics
159//
160// <----------------> <==== time interval to be ignored
161//
162//
163// the distribution of 'dt' is still distorted, because there would be no
164// 'dt' with dt > tau_calib = 1/lambda_calib. The distribution would also be
165// distorted in the region dt < tau_calib, due to calibration events occuring
166// shortly after cosmics events. As a result, the slope of the distribution of
167// 'dt' would not reflect the ideal cosmics event rate (see second figure; the
168// values assumed in the simulation are lambda = 200 Hz, lambda_calib = 50
169// Hz, dead = 0.001 sec, total time = 500 sec, number of generated cosmics
170// events = 100 000).
171//
172//
173// Note also that some calibration events will not be recorded due to the dead
174// time after a cosmics event.
175//
176//
177// III. Proposed procedures
178// ------------------------
179//
180// A) The ideal event rate 'lambda' may be calculated from the distribution of
181// the time difference 'dt_first' between a calibration event and the first
182// recorded cosmics event after the calibration event. In the region
183//
184// dead < dt_first < tau_calib
185//
186// the probability distribution of dt_first is given by
187//
188// p(dt_first) = c * exp(-lambda*dt_first)
189//
190// where c is a normalization constant. 'lambda' can be obtained by a simple
191// exponential fit to the experimental distribution of dt_first (see third
192// figure). The fit range should start well above the average value of the dead
193// time 'dead'.
194//
195//
196// B) One may consider those time intervals between recorded cosmics events, which
197// are completely contained in the region
198//
199// t_calib < t < t_calib + tau_calib
200//
201// where t_calib is the time of a recorded calibration event.
202//
203//
204// <--------------- tau_calib ----------->
205//
206//
207// 0 1 2 3 4 5 6 7 8 9 10
208// --|-o---|-|---|--|-|----|--|---|---|-|----o-|---|-|---------> t
209// ^ ^
210// | |
211// t_calib t_calib + tau_calib
212//
213//
214// In this example, of the time intervals 0 to 10 only the intervals 1 to 9
215// should be retained and plotted. The distribution of the length 'dt' of these
216// intervals in the region
217//
218// dead < dt < tau_calib
219//
220// is given by
221//
222// p(dt) = c * (tau_calib-dt-dead) * exp(-lambda*dt)
223//
224// A fit of this expression to the experimental distribution of 'dt' yields
225// 'lambda' (see fourth figure). For 'dead' an average value of the dead time
226// should be chosen, and the fit range should end well before dt = tau_calib-dead.
227//
228//
229// Method A has the advantage that the p(dt_first) does not depend on 'dead'.
230// 'dead' has to be considered when defining the fit range, both in method A and
231// in method B. In method B the event statistics is larger leading to a smaller
232// fitted error of 'lambda' than method A (see the figures).
233//
234//
235// The effective observation time is again obtained by
236//
237// T_eff = N_cosmics / lambda
238//
239// where N_cosmics is the total number of recorded cosmics events. Note that
240// N_cosmics is equal to
241//
242// N_cosmics = N_tot - N_calib
243//
244// where N_tot is the total number of recorded events (including the calibration
245// events) and N_calib is the number of recorded calibration events.
246//
247// Note that if time intervals are discarded for the determination of lambda,
248// the corresponding cosmics events need not and should not be discarded.
249//
250//
251// IV. Procedure if the calibration events are taken in bunches
252// ------------------------------------------------------------
253// In November 2004 the rate of calibration events is not constant. The events
254// are taken in 200 Hz bunches every second, such that the rate is 200 Hz for
255// 0.25 sec, followed by a gap of 0.75 sec. Then follows the next 200 Hz bunch.
256//
257// In this case it is proposed to consider for the calculation of 'lambda' only
258// the cosmics events within the gaps of 0.75 sec. For these cosmics events one
259// of the methods described in III. can be applied.
260//
261//
262// V. Alternative pocedure
263// -----------------------
264// The effective observation time can also be determined from the total
265// observation time and the total dead time. The latter is written out by the DAQ.
266// In this case it has to be made sure that the dead time is available in Mars
267// when the effective observation time is calculated.
268//
269//////////////////////////////////////////////////////////////////////////////
270#include "MHEffectiveOnTime.h"
271
272#include <TF1.h>
273#include <TMinuit.h>
274#include <TRandom.h>
275
276#include <TLatex.h>
277#include <TGaxis.h>
278#include <TCanvas.h>
279#include <TPaveStats.h>
280
281#include "MTime.h"
282#include "MParameters.h"
283#include "MPointingPos.h"
284#include "MRawRunHeader.h"
285
286#include "MBinning.h"
287#include "MParList.h"
288
289#include "MLog.h"
290#include "MLogManip.h"
291
292ClassImp(MHEffectiveOnTime);
293
294using namespace std;
295
296// --------------------------------------------------------------------------
297//
298// Default Constructor. It initializes all histograms.
299//
300MHEffectiveOnTime::MHEffectiveOnTime(const char *name, const char *title)
301 : fPointPos(0), fTime(0), fParam(0), fIsFinalized(kFALSE),
302 fNumEvents(200*60), fFirstBin(3), fTotalTime(-1)
303 //fNumEvents(2*60), fFirstBin(1)
304{
305 //
306 // set the name and title of this object
307 //
308 fName = name ? name : "MHEffectiveOnTime";
309 fTitle = title ? title : "Histogram to determin effective On-Time vs Time and Zenith Angle";
310
311 // Main histogram
312 fH2DeltaT.SetName("DeltaT");
313 fH2DeltaT.SetXTitle("\\Delta t [s]");
314 fH2DeltaT.SetYTitle("\\Theta [\\circ]");
315 fH2DeltaT.SetZTitle("Count");
316 fH2DeltaT.UseCurrentStyle();
317 fH2DeltaT.SetDirectory(NULL);
318
319 // Main histogram
320 fH1DeltaT.SetName("DeltaT");
321 fH1DeltaT.SetXTitle("\\Delta t [s]");
322 fH1DeltaT.SetYTitle("Counts");
323 fH1DeltaT.UseCurrentStyle();
324 fH1DeltaT.SetDirectory(NULL);
325
326 // effective on time versus theta
327 fHThetaEffOn.SetName("EffOnTheta");
328 fHThetaEffOn.SetTitle("Effective On Time T_{eff}");
329 fHThetaEffOn.SetXTitle("\\Theta [\\circ]");
330 fHThetaEffOn.SetYTitle("T_{eff} [s]");
331 fHThetaEffOn.UseCurrentStyle();
332 fHThetaEffOn.SetDirectory(NULL);
333 fHThetaEffOn.GetYaxis()->SetTitleOffset(1.2);
334 fHThetaEffOn.GetYaxis()->SetTitleColor(kBlue);
335 fHThetaEffOn.SetLineColor(kBlue);
336 //fHEffOn.Sumw2();
337
338 // effective on time versus time
339 fHTimeEffOn.SetName("EffOnTime");
340 fHTimeEffOn.SetTitle("Effective On Time T_{eff}");
341 fHTimeEffOn.SetXTitle("Time");
342 fHTimeEffOn.SetYTitle("T_{eff} [s]");
343 fHTimeEffOn.UseCurrentStyle();
344 fHTimeEffOn.SetDirectory(NULL);
345 fHTimeEffOn.GetYaxis()->SetTitleOffset(1.2);
346 fHTimeEffOn.GetXaxis()->SetLabelSize(0.033);
347 fHTimeEffOn.GetXaxis()->SetTimeFormat("%H:%M:%S %F1995-01-01 00:00:00 GMT");
348 fHTimeEffOn.GetXaxis()->SetTimeDisplay(1);
349 fHTimeEffOn.GetYaxis()->SetTitleColor(kBlue);
350 fHTimeEffOn.SetLineColor(kBlue);
351
352 // chi2 probability
353 fHThetaProb.SetName("ProbTheta");
354 fHThetaProb.SetTitle("\\chi^{2} Probability of Fit");
355 fHThetaProb.SetXTitle("\\Theta [\\circ]");
356 fHThetaProb.SetYTitle("p [%]");
357 fHThetaProb.UseCurrentStyle();
358 fHThetaProb.SetDirectory(NULL);
359 fHThetaProb.GetYaxis()->SetTitleOffset(1.2);
360 fHThetaProb.SetMaximum(101);
361 fHThetaProb.GetYaxis()->SetTitleColor(kBlue);
362 fHThetaProb.SetLineColor(kBlue);
363
364 // chi2 probability
365 fHTimeProb.SetName("ProbTime");
366 fHTimeProb.SetTitle("\\chi^{2} Probability of Fit");
367 fHTimeProb.SetXTitle("Time");
368 fHTimeProb.SetYTitle("p [%]");
369 fHTimeProb.UseCurrentStyle();
370 fHTimeProb.SetDirectory(NULL);
371 fHTimeProb.GetYaxis()->SetTitleOffset(1.2);
372 fHTimeProb.GetXaxis()->SetLabelSize(0.033);
373 fHTimeProb.GetXaxis()->SetTimeFormat("%H:%M:%S %F1995-01-01 00:00:00 GMT");
374 fHTimeProb.GetXaxis()->SetTimeDisplay(1);
375 fHTimeProb.SetMaximum(101);
376 fHTimeProb.GetYaxis()->SetTitleColor(kBlue);
377 fHTimeProb.SetLineColor(kBlue);
378
379 // lambda versus theta
380 fHThetaLambda.SetName("LambdaTheta");
381 fHThetaLambda.SetTitle("Slope (Rate) vs Theta");
382 fHThetaLambda.SetXTitle("\\Theta [\\circ]");
383 fHThetaLambda.SetYTitle("\\lambda [s^{-1}]");
384 fHThetaLambda.UseCurrentStyle();
385 fHThetaLambda.SetDirectory(NULL);
386 fHThetaLambda.SetLineColor(kGreen);
387
388 // lambda versus time
389 fHTimeLambda.SetName("LambdaTime");
390 fHTimeLambda.SetTitle("Slope (Rate) vs Time");
391 fHTimeLambda.SetXTitle("\\Time [\\circ]");
392 fHTimeLambda.SetYTitle("\\lambda [s^{-1}]");
393 fHTimeLambda.UseCurrentStyle();
394 fHTimeLambda.SetDirectory(NULL);
395 fHTimeLambda.GetYaxis()->SetTitleOffset(1.2);
396 fHTimeLambda.GetXaxis()->SetLabelSize(0.033);
397 fHTimeLambda.GetXaxis()->SetTimeFormat("%H:%M:%S %F1995-01-01 00:00:00 GMT");
398 fHTimeLambda.GetXaxis()->SetTimeDisplay(1);
399 fHTimeLambda.SetLineColor(kGreen);
400
401 // NDoF versus theta
402 fHThetaNDF.SetName("NDofTheta");
403 fHThetaNDF.SetTitle("Number of Degrees of freedom vs Theta");
404 fHThetaNDF.SetXTitle("\\Theta [\\circ]");
405 fHThetaNDF.SetYTitle("NDoF [#]");
406 fHThetaNDF.UseCurrentStyle();
407 fHThetaNDF.SetDirectory(NULL);
408 fHThetaNDF.SetLineColor(kGreen);
409
410 // NDoF versus time
411 /*
412 fHTimeNDF.SetName("NDofTime");
413 fHTimeNDF.SetTitle("Number of Degrees of freedom vs Time");
414 fHTimeNDF.SetXTitle("Time");
415 fHTimeNDF.SetYTitle("NDoF [#]");
416 fHTimeNDF.UseCurrentStyle();
417 fHTimeNDF.SetDirectory(NULL);
418 fHTimeNDF.GetYaxis()->SetTitleOffset(1.2);
419 fHTimeNDF.GetXaxis()->SetLabelSize(0.033);
420 fHTimeNDF.GetXaxis()->SetTimeFormat("%H:%M:%S %F1995-01-01 00:00:00 GMT");
421 fHTimeNDF.GetXaxis()->SetTimeDisplay(1);
422 fHTimeNDF.SetLineColor(kBlue);
423 */
424 // setup binning
425 MBinning btheta("BinningTheta");
426 btheta.SetEdgesASin(67, -0.005, 0.665);
427
428 MBinning btime("BinningDeltaT");
429 btime.SetEdges(50, 0, 0.1);
430
431 MH::SetBinning(&fH2DeltaT, &btime, &btheta);
432
433 btime.Apply(fH1DeltaT);
434
435 btheta.Apply(fHThetaEffOn);
436 btheta.Apply(fHThetaLambda);
437 btheta.Apply(fHThetaNDF);
438 btheta.Apply(fHThetaProb);
439 //btheta.Apply(fHChi2);
440}
441
442// --------------------------------------------------------------------------
443//
444// Set the binnings and prepare the filling of the histogram
445//
446Bool_t MHEffectiveOnTime::SetupFill(const MParList *plist)
447{
448 fPointPos = (MPointingPos*)plist->FindObject("MPointingPos");
449 if (!fPointPos)
450 {
451 *fLog << err << dbginf << "MPointingPos not found... aborting." << endl;
452 return kFALSE;
453 }
454
455 // FIXME: Remove const-qualifier from base-class!
456 fTime = (MTime*)const_cast<MParList*>(plist)->FindCreateObj("MTime", "MTimeEffectiveOnTime");
457 if (!fTime)
458 return kFALSE;
459 fParam = (MParameterDerr*)const_cast<MParList*>(plist)->FindCreateObj("MParameterDerr", "MEffectiveOnTime");
460 if (!fParam)
461 return kFALSE;
462
463 const MBinning* binsdtime = (MBinning*)plist->FindObject("BinningDeltaT");
464 const MBinning* binstheta = (MBinning*)plist->FindObject("BinningTheta");
465 if (binsdtime)
466 binsdtime->Apply(fH1DeltaT);
467 if (binstheta)
468 {
469 binstheta->Apply(fHThetaEffOn);
470 binstheta->Apply(fHThetaLambda);
471 binstheta->Apply(fHThetaNDF);
472 binstheta->Apply(fHThetaProb);
473 //binstheta->Apply(fHChi2);
474 }
475 if (binstheta && binsdtime)
476 SetBinning(&fH2DeltaT, binsdtime, binstheta);
477
478 fTotalTime = 0;
479
480 return kTRUE;
481}
482
483Bool_t MHEffectiveOnTime::ReInit(MParList *pList)
484{
485 MRawRunHeader *header = (MRawRunHeader*)pList->FindObject("MRawRunHeader");
486 if (header)
487 {
488 *fLog << err << "MRawRunHeader not found... aborting." << endl;
489 return kFALSE;
490 }
491
492 fTotalTime += header->GetRunLength();
493
494 return kTRUE;
495}
496
497// --------------------------------------------------------------------------
498//
499// Fit a single Delta-T distribution. See source code for more details
500//
501Bool_t MHEffectiveOnTime::FitH(TH1D *h, Double_t *res, Bool_t paint) const
502{
503 const Double_t Nm = h->Integral();
504
505 // FIXME: Do fit only if contents of bin has changed
506 if (Nm<=0 || h->GetBinContent(1)<=0)
507 return kFALSE;
508
509 // determine range (yq[0], yq[1]) of time differences
510 // where fit should be performed;
511 // require a fraction >=xq[0] of all entries to lie below yq[0]
512 // and a fraction <=xq[1] of all entries to lie below yq[1];
513 // within the range (yq[0], yq[1]) there must be no empty bin;
514 // choose pedestrian approach as long as GetQuantiles is not available
515 Double_t xq[2] = { 0.6, 0.95 }; // previously 0.99
516 Double_t yq[2];
517 h->GetQuantiles(2, yq, xq);
518
519 //
520 // Determine a good starting value for the slope
521 //
522 const TAxis &axe = *h->GetXaxis();
523 const UInt_t ibin = axe.FindFixBin(yq[1]);
524 const Double_t x1 = axe.GetBinCenter(ibin<=fFirstBin?fFirstBin+1:ibin);
525 const Double_t x0 = axe.GetBinCenter(fFirstBin);
526 const Double_t y1 = h->GetBinContent(ibin)>1 ? TMath::Log(h->GetBinContent(ibin)) : 0;
527 const Double_t y0 = TMath::Log(h->GetBinContent(fFirstBin));
528
529 // Estimated slope
530 const Float_t m = -(y1-y0)/(x1-x0);
531
532 //
533 // Setup exponential function for the fit:
534 //
535 // parameter 0 = rate [Hz]
536 // parameter 1 = normalization
537 //
538 TF1 func("Exp", " exp([1]-[0]*x)");
539
540 func.SetParameter(0, m); // Hz
541 func.SetParameter(1, log(h->GetBinContent(1))); // Hz
542
543 // We set a limit to make sure that almost empty histograms which
544 // are fitted dont't produce hang ups or crashes
545 func.SetParLimits(0, 0, 15000); // Hz
546
547 // options : N do not store the function, do not draw
548 // I use integral of function in bin rather than value at bin center
549 // R use the range specified in the function range
550 // Q quiet mode
551 // L Use log-likelihood (better for low statistics)
552 h->Fit(&func, "NIQEL", "", h->GetBinLowEdge(fFirstBin)/*yq[0]*/, yq[1]);
553
554 const Double_t chi2 = func.GetChisquare();
555 const Double_t prob = func.GetProb();
556 const Int_t NDF = func.GetNDF();
557
558 // was fit successful ?
559 const Bool_t ok = prob>0.001; //NDF>0 && chi2<3*NDF;
560
561 if (paint)
562 {
563 func.SetLineWidth(2);
564 func.SetLineColor(ok ? kGreen : kRed);
565 func.Paint("same");
566 }
567
568 // The effective on time is the "real rate" (slope of the exponential)
569 // divided by the total number of events (histogram integral including
570 // under- and overflows)
571 const Double_t lambda = func.GetParameter(0);
572 const Double_t dldl = func.GetParError(0)*func.GetParError(0);
573 const Double_t teff = lambda==0 ? 0 : Nm / lambda;
574 const Double_t dteff = lambda==0 ? 0 : teff * TMath::Sqrt(dldl/(lambda*lambda) + 1.0/Nm);
575 const Double_t dl = TMath::Sqrt(dldl);
576
577 // the effective on time is Nm/lambda
578 res[0] = teff;
579 res[1] = dteff;
580
581 // plot chi2-probability of fit
582 res[2] = prob*100;
583
584 // lambda of fit
585 res[3] = lambda;
586 res[4] = dl;
587
588 // NDoF of fit
589 res[5] = NDF;
590
591 // Chi2
592 res[6] = chi2;
593
594 return ok;
595}
596
597// --------------------------------------------------------------------------
598//
599// Fit a all bins of the distribution in theta. Store the result in the
600// Theta-Histograms
601//
602void MHEffectiveOnTime::FitThetaBins()
603{
604 fHThetaEffOn.Reset();
605 fHThetaProb.Reset();
606 fHThetaLambda.Reset();
607 fHThetaNDF.Reset();
608
609 // Use a random name to make sure the object is unique
610 const TString name = Form("CalcTheta%d", (UInt_t)gRandom->Uniform(999999999));
611
612 // nbins = number of Theta bins
613 const Int_t nbins = fH2DeltaT.GetNbinsY();
614
615 TH1D *h=0;
616 for (int i=1; i<=nbins; i++)
617 {
618 // TH1D &h = *hist->ProjectionX("Calc-theta", i, i);
619 h = fH2DeltaT.ProjectionX(name, i, i, "E");
620
621 Double_t res[7] = {0, 0, 0, 0, 0, 0, 0};
622 //if (!FitH(h, res))
623 // continue;
624 FitH(h, res);
625
626 if (res[0]==0)
627 continue;
628
629 // the effective on time is Nm/lambda
630 fHThetaEffOn.SetBinContent(i, res[0]);
631 fHThetaEffOn.SetBinError (i, res[1]);
632
633 // plot chi2-probability of fit
634 fHThetaProb.SetBinContent(i, res[2]);
635
636 // plot chi2/NDF of fit
637 //fHChi2.SetBinContent(i, res[3]);
638
639 // lambda of fit
640 fHThetaLambda.SetBinContent(i, res[3]);
641 fHThetaLambda.SetBinError (i, res[4]);
642
643 // NDoF of fit
644 fHThetaNDF.SetBinContent(i, res[5]);
645
646 // Rdead (from fit) is the fraction from real time lost by the dead time
647 //fHRdead.SetBinContent(i, Rdead);
648 //fHRdead.SetBinError (i,dRdead);
649 }
650
651 // Histogram is reused via gROOT->FindObject()
652 // Need to be deleted only once
653 if (h)
654 delete h;
655}
656
657// --------------------------------------------------------------------------
658//
659// Fit the single-time-bin histogram. Store the result in the
660// Time-Histograms
661//
662void MHEffectiveOnTime::FitTimeBin()
663{
664 //
665 // Fit histogram
666 //
667 Double_t res[7];
668 if (!FitH(&fH1DeltaT, res))
669 return;
670
671 // Reset Histogram
672 fH1DeltaT.Reset();
673
674 //
675 // Prepare Histogram
676 //
677
678 // Get number of bins
679 const Int_t n = fHTimeEffOn.GetNbinsX();
680
681 // Enhance binning
682 MBinning bins;
683 bins.SetEdges(fHTimeEffOn, 'x');
684 bins.AddEdge(fLastTime.GetAxisTime());
685 bins.Apply(fHTimeEffOn);
686 bins.Apply(fHTimeProb);
687 bins.Apply(fHTimeLambda);
688 //bins.Apply(fHTimeNDF);
689
690 //
691 // Fill histogram
692 //
693 fHTimeEffOn.SetBinContent(n, res[0]);
694 fHTimeEffOn.SetBinError(n, res[1]);
695
696 fHTimeProb.SetBinContent(n, res[2]);
697
698 fHTimeLambda.SetBinContent(n, res[3]);
699 fHTimeLambda.SetBinError(n, res[4]);
700
701 //fHTimeNDF.SetBinContent(n, res[5]);
702
703 //
704 // Now prepare output
705 //
706 fParam->SetVal(res[0], res[1]);
707 fParam->SetReadyToSave();
708
709 *fTime = fLastTime;
710
711 // Include the current event
712 fTime->Plus1ns();
713
714 *fLog << fLastTime << ": Val=" << res[0] << " Err=" << res[1] << endl;
715}
716
717// --------------------------------------------------------------------------
718//
719// Fill the histogram
720//
721Bool_t MHEffectiveOnTime::Fill(const MParContainer *par, const Stat_t w)
722{
723 const MTime *time = dynamic_cast<const MTime*>(par);
724 if (!time)
725 {
726 *fLog << err << "ERROR - MHEffectiveOnTime::Fill without argument or container doesn't inherit from MTime... abort." << endl;
727 return kFALSE;
728 }
729
730 //
731 // If this is the first call we have to initialize the time-histogram
732 //
733 if (fLastTime==MTime())
734 {
735 MBinning bins;
736 bins.SetEdges(1, time->GetAxisTime()-fNumEvents/200, time->GetAxisTime());
737 bins.Apply(fHTimeEffOn);
738 bins.Apply(fHTimeProb);
739 bins.Apply(fHTimeLambda);
740 //bins.Apply(fHTimeNDF);
741
742 fParam->SetVal(0, 0);
743 fParam->SetReadyToSave();
744
745 *fTime = *time;
746
747 // Make this 1ns before the first event!
748 fTime->Minus1ns();
749 }
750
751 //
752 // Fill time difference into the histograms
753 //
754 const Double_t dt = *time-fLastTime;
755 fLastTime = *time;
756
757 fH2DeltaT.Fill(dt, fPointPos->GetZd(), w);
758 fH1DeltaT.Fill(dt, w);
759
760 //
761 // If we reached the event number limit for the time-bins fit the
762 // histogram - if it fails try again when 1.6% more events available
763 //
764 const UInt_t n = (UInt_t)fH1DeltaT.GetEntries();
765 if (n>=fNumEvents && n%(fNumEvents/60)==0)
766 FitTimeBin();
767
768 return kTRUE;
769}
770
771// --------------------------------------------------------------------------
772//
773// Fit the theta projections of the 2D histogram and the 1D Delta-T
774// distribution
775//
776Bool_t MHEffectiveOnTime::Finalize()
777{
778 FitThetaBins();
779 FitTimeBin();
780
781 fIsFinalized = kTRUE;
782
783 return kTRUE;
784}
785
786// --------------------------------------------------------------------------
787//
788// Paint the integral and the error on top of the histogram
789//
790void MHEffectiveOnTime::PaintText(Double_t val, Double_t error, Double_t range) const
791{
792 TLatex text;
793 text.SetBit(TLatex::kTextNDC);
794 text.SetTextSize(0.04);
795
796 TString txt = Form("T_{eff} = %.1fs \\pm %.1fs", val, error);
797 if (range>0)
798 txt += Form(" T_{axe} = %.1fs", range);
799 if (fTotalTime>0)
800 txt += Form(" T_{max} = %.1fs", fTotalTime);
801
802 text.SetText(0.35, 0.94, txt);
803 text.Paint();
804}
805
806void MHEffectiveOnTime::PaintText(Double_t *res) const
807{
808 TLatex text(0.27, 0.94, Form("T_{eff}=%.1fs\\pm%.1fs \\lambda=%.1f\\pm%.1fHz p=%.1f%% \\chi^{2}/%d=%.1f",
809 res[0], res[1], res[3], res[4], res[2], (int)res[5], res[6]/res[5]));
810 text.SetBit(TLatex::kTextNDC);
811 text.SetTextSize(0.04);
812 text.Paint();
813}
814
815void MHEffectiveOnTime::PaintProb(TH1 &h) const
816{
817 Double_t sum = 0;
818 Int_t n = 0;
819 for (int i=0; i<h.GetNbinsX(); i++)
820 if (h.GetBinContent(i+1)>0)
821 {
822 sum += h.GetBinContent(i+1);
823 n++;
824 }
825
826 if (n==0)
827 return;
828
829 TLatex text(0.47, 0.94, Form("\\bar{p} = %.1f%%", sum/n));
830 text.SetBit(TLatex::kTextNDC);
831 text.SetTextSize(0.04);
832 text.Paint();
833}
834
835void MHEffectiveOnTime::UpdateRightAxis(TH1 &h)
836{
837 const Double_t max = h.GetMaximum()*1.1;
838 if (max==0)
839 return;
840
841 h.SetNormFactor(h.Integral()*gPad->GetUymax()/max);
842
843 TGaxis *axis = (TGaxis*)gPad->FindObject("RightAxis");
844 if (!axis)
845 return;
846
847 axis->SetX1(gPad->GetUxmax());
848 axis->SetX2(gPad->GetUxmax());
849 axis->SetY1(gPad->GetUymin());
850 axis->SetY2(gPad->GetUymax());
851 axis->SetWmax(max);
852}
853
854// --------------------------------------------------------------------------
855//
856// Prepare painting the histograms
857//
858void MHEffectiveOnTime::Paint(Option_t *opt)
859{
860 TH1D *h=0;
861 TPaveStats *st=0;
862
863 TString o(opt);
864 if (o==(TString)"fit")
865 {
866 TVirtualPad *pad = gPad;
867
868 for (int x=0; x<2; x++)
869 for (int y=0; y<3; y++)
870 {
871 TVirtualPad *p=gPad->GetPad(x+1)->GetPad(y+1);
872 if (!(st = dynamic_cast<TPaveStats*>(p->GetPrimitive("stats"))))
873 continue;
874
875 if (st->GetOptStat()==11)
876 continue;
877
878 const Double_t y1 = st->GetY1NDC();
879 const Double_t y2 = st->GetY2NDC();
880 const Double_t x1 = st->GetX1NDC();
881 const Double_t x2 = st->GetX2NDC();
882
883 st->SetY1NDC((y2-y1)/3+y1);
884 st->SetX1NDC((x2-x1)/3+x1);
885 st->SetOptStat(11);
886 }
887
888 pad->GetPad(1)->cd(1);
889 if ((h = (TH1D*)gPad->FindObject("ProjDeltaT"/*fNameProjDeltaT*/)))
890 {
891 h = fH2DeltaT.ProjectionX("ProjDeltaT"/*fNameProjDeltaT*/, -1, -1, "E");
892 if (h->GetEntries()>0)
893 gPad->SetLogy();
894 }
895
896 pad->GetPad(2)->cd(1);
897 if ((h = (TH1D*)gPad->FindObject("ProjTheta"/*fNameProjTheta*/)))
898 fH2DeltaT.ProjectionY("ProjTheta"/*fNameProjTheta*/, -1, -1, "E");
899
900 if (!fIsFinalized)
901 FitThetaBins();
902 return;
903 }
904 if (o==(TString)"paint")
905 {
906 if ((h = (TH1D*)gPad->FindObject("ProjDeltaT"/*fNameProjDeltaT*/)))
907 {
908 Double_t res[7];
909 FitH(h, res, kTRUE);
910 PaintText(res);
911 }
912 return;
913 }
914
915 if (o==(TString)"timendf")
916 {
917 // UpdateRightAxis(fHTimeNDF);
918 // FIXME: first bin?
919 PaintProb(fHTimeProb);
920 }
921
922 if (o==(TString)"thetandf")
923 {
924 UpdateRightAxis(fHThetaNDF);
925 // FIXME: first bin?
926 PaintProb(fHThetaProb);
927 }
928
929 h=0;
930
931 Double_t range=-1;
932 if (o==(TString)"theta")
933 {
934 h = &fHThetaEffOn;
935 UpdateRightAxis(fHThetaLambda);
936 }
937 if (o==(TString)"time")
938 {
939 h = &fHTimeEffOn;
940 UpdateRightAxis(fHTimeLambda);
941 range = h->GetXaxis()->GetXmax() - h->GetXaxis()->GetXmin();
942 }
943
944 if (!h)
945 return;
946
947 Double_t error = 0;
948 for (int i=0; i<h->GetXaxis()->GetNbins(); i++)
949 error += h->GetBinError(i);
950
951 PaintText(h->Integral(), error, range);
952}
953
954void MHEffectiveOnTime::DrawRightAxis(const char *title)
955{
956 TGaxis *axis = new TGaxis(gPad->GetUxmax(), gPad->GetUymin(),
957 gPad->GetUxmax(), gPad->GetUymax(),
958 0, 1, 510, "+L");
959 axis->SetName("RightAxis");
960 axis->SetTitle(title);
961 axis->SetTitleOffset(0.9);
962 axis->SetTextColor(kGreen);
963 axis->CenterTitle();
964 axis->SetBit(kCanDelete);
965 axis->Draw();
966}
967
968// --------------------------------------------------------------------------
969//
970// Draw the histogram
971//
972void MHEffectiveOnTime::Draw(Option_t *opt)
973{
974 TVirtualPad *pad = gPad ? gPad : MakeDefCanvas(this);
975 pad->SetBorderMode(0);
976
977 AppendPad("fit");
978
979 pad->Divide(2, 1, 1e-10, 1e-10);
980
981 TH1 *h;
982
983 pad->cd(1);
984 gPad->SetBorderMode(0);
985 gPad->Divide(1, 3, 1e-10, 1e-10);
986 pad->GetPad(1)->cd(1);
987 gPad->SetBorderMode(0);
988 h = fH2DeltaT.ProjectionX("ProjDeltaT"/*fNameProjDeltaT*/, -1, -1, "E");
989 h->SetTitle("Distribution of \\Delta t [s]");
990 h->SetXTitle("\\Delta t [s]");
991 h->SetYTitle("Counts");
992 h->SetDirectory(NULL);
993 h->SetMarkerStyle(kFullDotMedium);
994 h->SetBit(kCanDelete);
995 h->Draw();
996 AppendPad("paint");
997
998 pad->GetPad(1)->cd(2);
999 gPad->SetBorderMode(0);
1000 fHTimeProb.Draw();
1001 AppendPad("timendf");
1002 //fHTimeNDF.Draw("same");
1003 //DrawRightAxis("NDF");
1004
1005 pad->GetPad(1)->cd(3);
1006 gPad->SetBorderMode(0);
1007 fHTimeEffOn.Draw();
1008 AppendPad("time");
1009 fHTimeLambda.Draw("same");
1010 DrawRightAxis("\\lambda [s^{-1}]");
1011
1012 pad->cd(2);
1013 gPad->SetBorderMode(0);
1014 gPad->Divide(1, 3, 1e-10, 1e-10);
1015
1016 pad->GetPad(2)->cd(1);
1017 gPad->SetBorderMode(0);
1018 h = fH2DeltaT.ProjectionY("ProjTheta"/*fNameProjTheta*/, -1, -1, "E");
1019 h->SetTitle("Distribution of \\Theta [\\circ]");
1020 h->SetXTitle("\\Theta [\\circ]");
1021 h->SetYTitle("Counts");
1022 h->SetDirectory(NULL);
1023 h->SetMarkerStyle(kFullDotMedium);
1024 h->SetBit(kCanDelete);
1025 h->GetYaxis()->SetTitleOffset(1.1);
1026 h->Draw();
1027
1028 pad->GetPad(2)->cd(2);
1029 gPad->SetBorderMode(0);
1030 fHThetaProb.Draw();
1031 AppendPad("thetandf");
1032 fHThetaNDF.Draw("same");
1033 DrawRightAxis("NDF");
1034
1035 pad->GetPad(2)->cd(3);
1036 gPad->SetBorderMode(0);
1037 fHThetaEffOn.Draw();
1038 AppendPad("theta");
1039 fHThetaLambda.Draw("same");
1040 DrawRightAxis("\\lambda [s^{-1}]");
1041}
1042
1043// --------------------------------------------------------------------------
1044//
1045// The following resources are available:
1046//
1047// MHEffectiveOnTime.FistBin: 3
1048// MHEffectiveOnTime.NumEvents: 12000
1049//
1050Int_t MHEffectiveOnTime::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
1051{
1052 Bool_t rc = kFALSE;
1053 if (IsEnvDefined(env, prefix, "FirstBin", print))
1054 {
1055 rc = kTRUE;
1056 SetFirstBin(GetEnvValue(env, prefix, "FirstBin", (Int_t)fFirstBin));
1057 }
1058 if (IsEnvDefined(env, prefix, "NumEvents", print))
1059 {
1060 rc = kTRUE;
1061 SetNumEvents(GetEnvValue(env, prefix, "NumEvents", (Int_t)fNumEvents));
1062 }
1063 return rc;
1064}
Note: See TracBrowser for help on using the repository browser.