source: trunk/MagicSoft/Mars/manalysis/MHExtractedSignalPix.cc@ 3038

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