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

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