source: trunk/MagicSoft/Mars/manalysis/MHPedestalPixel.cc@ 2951

Last change on this file since 2951 was 2951, checked in by gaug, 21 years ago
*** empty log message ***
File size: 9.3 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): Markus Gaug 02/2004 <mailto:markus@ifae.es>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25//////////////////////////////////////////////////////////////////////////////
26//
27// MHPedestalPixel
28//
29// Performs all the necessary fits to extract the mean number of photons
30// out of the derived light flux
31//
32//////////////////////////////////////////////////////////////////////////////
33#include "MHPedestalPixel.h"
34
35#include <TH1.h>
36#include <TF1.h>
37
38#include <TStyle.h>
39#include <TCanvas.h>
40
41#include "MLog.h"
42#include "MLogManip.h"
43
44ClassImp(MHPedestalPixel);
45
46using namespace std;
47// Square root of 2pi:
48const Float_t gkSq2Pi = 2.506628274631;
49const Float_t gkProbLimit = 0.01;
50
51// --------------------------------------------------------------------------
52//
53// Default Constructor.
54//
55MHPedestalPixel::MHPedestalPixel(const char *name, const char *title)
56 : fPixId(-1),
57 fChargeNbins(500),
58 fChargevsNbins(1000),
59 fChargevsNFirst(-0.5),
60 fChargevsNLast(999.5),
61 fChargeFirst(-0.5),
62 fChargeLast(99.5),
63 fGausFit(NULL)
64{
65
66 fName = name ? name : "MHPedestalPixel";
67 fTitle = title ? title : "Fill the accumulated charges and times of all events and perform fits";
68
69 // Create a large number of bins, later we will rebin
70 fHPedestalCharge = new TH1F("HPedestalCharge","Distribution of Summed FADC Pedestal Slices Pixel ",
71 fChargeNbins,fChargeFirst,fChargeLast);
72 fHPedestalCharge->SetXTitle("Sum FADC Slices");
73 fHPedestalCharge->SetYTitle("Nr. of events");
74 fHPedestalCharge->Sumw2();
75
76 // We define a reasonable number and later enlarge it if necessary
77 fHPedestalChargevsN = new TH1I("HChargevsN","Sum of Charges vs. Event Number Pixel ",
78 fChargevsNbins,fChargevsNFirst,fChargevsNLast);
79 fHPedestalChargevsN->SetXTitle("Event Nr.");
80 fHPedestalChargevsN->SetYTitle("Sum of FADC slices");
81
82 fHPedestalCharge->SetDirectory(NULL);
83 fHPedestalChargevsN->SetDirectory(NULL);
84
85 Clear();
86}
87
88MHPedestalPixel::~MHPedestalPixel()
89{
90
91 delete fHPedestalCharge;
92 delete fHPedestalChargevsN;
93
94 if (fGausFit)
95 delete fGausFit;
96}
97
98
99void MHPedestalPixel::Clear(Option_t *o)
100{
101
102 fTotalEntries = 0;
103
104 fChargeMean = -1.;
105 fChargeMeanErr = -1.;
106 fChargeSigma = -1;
107 fChargeSigmaErr = -1;
108
109 fChargeChisquare = -1.;
110 fChargeProb = -1.;
111 fChargeNdf = -1;
112
113 fChargeFirst = -0.5;
114 fChargeLast = 99.5;
115
116 if (fGausFit)
117 delete fGausFit;
118
119 return;
120}
121
122
123void MHPedestalPixel::Reset()
124{
125
126 Clear();
127
128 fHPedestalCharge->Reset();
129 fHPedestalChargevsN->Reset();
130
131}
132
133
134
135Bool_t MHPedestalPixel::IsEmpty() const
136{
137 return !fHPedestalCharge->GetEntries();
138}
139
140Bool_t MHPedestalPixel::IsFitOK() const
141{
142 return TESTBIT(fFlags,kFitOK);
143}
144
145Bool_t MHPedestalPixel::FillCharge(Float_t q)
146{
147 return (fHPedestalCharge->Fill(q) > -1);
148}
149
150Bool_t MHPedestalPixel::FillChargevsN(Float_t q)
151{
152
153 fTotalEntries++;
154 return (fHPedestalChargevsN->Fill(fTotalEntries,q) > -1);
155}
156
157void MHPedestalPixel::ChangeHistId(Int_t id)
158{
159
160 fPixId = id;
161
162 TString nameQ = TString(fHPedestalCharge->GetName());
163 nameQ += id;
164 fHPedestalCharge->SetName(nameQ.Data());
165
166 TString nameQvsN = TString(fHPedestalChargevsN->GetName());
167 nameQvsN += id;
168 fHPedestalChargevsN->SetName(nameQvsN.Data());
169
170 TString titleQ = TString(fHPedestalCharge->GetTitle());
171 titleQ += id;
172 fHPedestalCharge->SetTitle(titleQ.Data());
173
174 TString titleQvsN = TString(fHPedestalChargevsN->GetTitle());
175 titleQvsN += id;
176 fHPedestalChargevsN->SetTitle(titleQvsN.Data());
177
178}
179
180
181Bool_t MHPedestalPixel::SetupFill(const MParList *plist)
182{
183
184 Reset();
185
186 return kTRUE;
187}
188
189
190
191
192TObject *MHPedestalPixel::DrawClone(Option_t *option) const
193{
194
195 gROOT->SetSelectedPad(NULL);
196
197 MHPedestalPixel *newobj = (MHPedestalPixel*)Clone();
198
199 if (!newobj)
200 return 0;
201 newobj->SetBit(kCanDelete);
202
203
204 if (strlen(option))
205 newobj->Draw(option);
206 else
207 newobj->Draw(GetDrawOption());
208
209 return newobj;
210}
211
212
213// -------------------------------------------------------------------------
214//
215// Draw the histogram
216//
217void MHPedestalPixel::Draw(Option_t *opt)
218{
219
220 gStyle->SetOptFit(1);
221 gStyle->SetOptStat(111111);
222
223 gROOT->SetSelectedPad(NULL);
224
225 TCanvas *c = MakeDefCanvas(this,600,900);
226
227 c->Divide(1,2);
228
229 c->cd(1);
230 gPad->SetBorderMode(0);
231 gPad->SetTicks();
232
233 if (fHPedestalCharge->Integral() > 0)
234 gPad->SetLogy(1);
235 else
236 gPad->SetLogy(0);
237
238 fHPedestalCharge->Draw(opt);
239
240 c->Modified();
241 c->Update();
242
243 if (fGausFit)
244 {
245 if (IsFitOK())
246 fGausFit->SetLineColor(kGreen);
247 else
248 fGausFit->SetLineColor(kRed);
249
250 fGausFit->Draw("same");
251 }
252
253 c->Modified();
254 c->Update();
255
256 c->cd(2);
257 gPad->SetTicks();
258
259 fHPedestalChargevsN->Draw(opt);
260 c->Modified();
261 c->Update();
262
263 return;
264}
265
266
267Bool_t MHPedestalPixel::FitCharge(Option_t *option)
268{
269
270 if (fGausFit)
271 return kFALSE;
272
273 //
274 // Get the fitting ranges
275 //
276 Axis_t rmin = fChargeFirst;
277 Axis_t rmax = fChargeLast;
278
279 //
280 // First guesses for the fit (should be as close to reality as possible,
281 // otherwise the fit goes gaga because of high number of dimensions ...
282 //
283 const Stat_t entries = fHPedestalCharge->Integral();
284 const Double_t area_guess = entries/gkSq2Pi;
285 const Double_t mu_guess = fHPedestalCharge->GetBinCenter(fHPedestalCharge->GetMaximumBin());
286 const Double_t sigma_guess = mu_guess/15.;
287
288 TString name = TString("GausFit");
289 name += fPixId;
290
291 fGausFit = new TF1(name.Data(),"gaus",rmin,rmax);
292
293 if (!fGausFit)
294 {
295 *fLog << warn << dbginf << "WARNING: Could not create fit function for Gauss fit" << endl;
296 return kFALSE;
297 }
298
299 fGausFit->SetParameters(area_guess,mu_guess,sigma_guess);
300 fGausFit->SetParNames("Area","#mu","#sigma");
301 fGausFit->SetParLimits(0,0.,entries);
302 fGausFit->SetParLimits(1,rmin,rmax);
303 fGausFit->SetParLimits(2,0.,rmax-rmin);
304 fGausFit->SetRange(rmin,rmax);
305
306 fHPedestalCharge->Fit(fGausFit,option);
307
308 //
309 // If we are not able to fit, try once again
310 //
311 if (fGausFit->GetProb() < gkProbLimit)
312 {
313
314 Axis_t rtry = fGausFit->GetParameter(1) - 3.0*fGausFit->GetParameter(2);
315 rmin = (rtry < rmin ? rmin : rtry);
316 rmax = fGausFit->GetParameter(1) + 3.0*fGausFit->GetParameter(2);
317 fGausFit->SetRange(rmin,rmax);
318
319 fHPedestalCharge->Fit(fGausFit,option);
320 }
321
322 fChargeChisquare = fGausFit->GetChisquare();
323 fChargeNdf = fGausFit->GetNDF();
324 fChargeProb = fGausFit->GetProb();
325 fChargeMean = fGausFit->GetParameter(1);
326 fChargeMeanErr = fGausFit->GetParError(1);
327 fChargeSigma = fGausFit->GetParameter(2);
328 fChargeSigmaErr = fGausFit->GetParError(2);
329
330 SETBIT(fFlags,kFitted);
331 //
332 // The fit result is accepted under condition:
333 // The Results are not nan's
334 // The Probability is greater than gkProbLimit (default 0.001 == 99.9%)
335 //
336 if (TMath::IsNaN(fChargeMean) || TMath::IsNaN(fChargeMeanErr))
337 {
338 CLRBIT(fFlags,kFitOK);
339 return kFALSE;
340 }
341
342 if ((fChargeProb < gkProbLimit) || (TMath::IsNaN(fChargeProb)))
343 {
344 CLRBIT(fFlags,kFitOK);
345 return kFALSE;
346 }
347
348 SETBIT(fFlags,kFitOK);
349 return kTRUE;
350}
351
352void MHPedestalPixel::CutAllEdges()
353{
354
355 Int_t nbins = 50;
356
357 CutEdges(fHPedestalCharge,nbins);
358
359 fChargeFirst = fHPedestalCharge->GetBinLowEdge(fHPedestalCharge->GetXaxis()->GetFirst());
360 fChargeLast = fHPedestalCharge->GetBinLowEdge(fHPedestalCharge->GetXaxis()->GetLast())
361 +fHPedestalCharge->GetBinWidth(0);
362
363 CutEdges(fHPedestalChargevsN,0);
364
365}
366
367void MHPedestalPixel::Print(const Option_t *o) const
368{
369
370 *fLog << all << "Pedestal Fits Pixel: " << fPixId << endl;
371
372 if (TESTBIT(fFlags,kFitted))
373 {
374
375 *fLog << all << "Results of the Summed Charges Fit: " << endl;
376 *fLog << all << "Chisquare: " << fChargeChisquare << endl;
377 *fLog << all << "DoF: " << fChargeNdf << endl;
378 *fLog << all << "Probability: " << fChargeProb << endl;
379 *fLog << all << "Results of fit: " ;
380 if (TESTBIT(fFlags,kFitOK))
381 *fLog << inf << "OK" << endl;
382 else
383 *fLog << err << "NOT OK" << endl;
384 *fLog << all << endl;
385 }
386 else
387 {
388 *fLog << all << "Pedestal Histogram has not yet been fitted" << endl;
389 *fLog << all << endl;
390 }
391
392}
393
Note: See TracBrowser for help on using the repository browser.