source: trunk/MagicSoft/Mars/mjobs/MJPedestal.cc@ 3051

Last change on this file since 3051 was 3051, checked in by gaug, 21 years ago
*** empty log message ***
File size: 8.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): Thomas Bretz, 1/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MJPedestal
28//
29/////////////////////////////////////////////////////////////////////////////
30#include "MJPedestal.h"
31
32#include <TF1.h>
33#include <TFile.h>
34#include <TStyle.h>
35#include <TString.h>
36#include <TCanvas.h>
37#include <TSystem.h>
38
39#include "MLog.h"
40#include "MLogManip.h"
41
42#include "MRunIter.h"
43#include "MParList.h"
44#include "MTaskList.h"
45#include "MEvtLoop.h"
46
47#include "MStatusDisplay.h"
48
49#include "MHCamera.h"
50#include "MPedestalCam.h"
51
52#include "MReadMarsFile.h"
53#include "MGeomApply.h"
54#include "MPedCalcPedRun.h"
55
56ClassImp(MJPedestal);
57
58using namespace std;
59
60MJPedestal::MJPedestal(const char *name, const char *title) : fRuns(0)
61{
62 fName = name ? name : "MJPedestal";
63 fTitle = title ? title : "Tool to create a pedestal file (MPedestalCam)";
64}
65
66TString MJPedestal::GetOutputFile() const
67{
68 if (!fRuns)
69 return "";
70
71 return Form("%s/%s-F0.root", (const char*)fOutputPath, (const char*)fRuns->GetRunsAsFileName());
72}
73
74Bool_t MJPedestal::ReadPedestalCam()
75{
76 const TString fname = GetOutputFile();
77
78 if (gSystem->AccessPathName(fname, kFileExists))
79 {
80 *fLog << err << "Input file " << fname << " doesn't exist." << endl;
81 return kFALSE;
82 }
83
84 *fLog << inf << "Reading from file: " << fname << endl;
85
86 TFile file(fname, "READ");
87 if (fPedestalCam.Read()<=0)
88 {
89 *fLog << "Unable to read MPedestalCam from " << fname << endl;
90 return kFALSE;
91 }
92
93 if (fDisplay && !fDisplay->GetCanvas("Pedestals"))
94 fDisplay->Read();
95
96 return kTRUE;
97}
98
99void MJPedestal::DisplayResult(MParList &plist)
100{
101 if (!fDisplay)
102 return;
103
104 //
105 // Update display
106 //
107 TString title = fDisplay->GetTitle();
108 title += "-- Pedestal ";
109 title += fRuns->GetRunsAsString();
110 title += " --";
111 fDisplay->SetTitle(title);
112
113 //
114 // Get container from list
115 //
116 MGeomCam &geomcam = *(MGeomCam*)plist.FindObject("MGeomCam");
117
118 //
119 // Create container to display
120 //
121 MHCamera disp0(geomcam, "MPedestalCam;ped", "Pedestals");
122 MHCamera disp1(geomcam, "MPedestalCam;rms", "Pedestal RMS");
123
124 disp0.SetCamContent(fPedestalCam, 0);
125 disp0.SetCamError(fPedestalCam, 1);
126 disp1.SetCamContent(fPedestalCam, 2);
127 disp1.SetCamError(fPedestalCam, 3);
128
129 disp0.SetYTitle("P [fadc/slice]");
130 disp1.SetYTitle("P_{RMS} [fadc/slice]");
131
132 //
133 // Display data
134 //
135 TCanvas &c3 = fDisplay->AddTab("Pedestals");
136 c3.Divide(2,3);
137
138 *fLog << "BALBALBALBA" <<endl;
139
140 CamDraw(c3, 1, 2, disp0, 0);
141 CamDraw(c3, 2, 2, disp1, 1);
142
143}
144
145
146void MJPedestal::DrawProjection(MHCamera *obj1, Int_t fit) const
147{
148
149 TH1D *obj2 = (TH1D*)obj1->Projection();
150 obj2->Draw();
151 obj2->SetBit(kCanDelete);
152
153 const Double_t min = obj2->GetBinCenter(obj2->GetXaxis()->GetFirst());
154 const Double_t max = obj2->GetBinCenter(obj2->GetXaxis()->GetLast());
155 const Double_t integ = obj2->Integral("width")/2.5066283;
156 const Double_t mean = obj2->GetMean();
157 const Double_t rms = obj2->GetRMS();
158 const Double_t width = max-min;
159
160 if (rms == 0. || width == 0. )
161 return;
162
163 const TString dgausformula = TString("([0]-[3])/[2]*exp(-0.5*(x-[1])*(x-[1])/[2]/[2])"
164 "+[3]/[5]*exp(-0.5*(x-[4])*(x-[4])/[5]/[5])");
165
166 TF1 *f=0;
167 switch (fit)
168 {
169 case 0:
170 f = new TF1("sgaus","gaus(0)",min,max);
171 f->SetLineColor(kYellow);
172 f->SetBit(kCanDelete);
173 f->SetParNames("Area","#mu","#sigma");
174 f->SetParameters(integ/rms,mean,rms);
175 f->SetParLimits(0, 0. , integ);
176 f->SetParLimits(1, min , max);
177 f->SetParLimits(2, 0 , width/1.5);
178 obj2->Fit(f,"QLRM");
179 break;
180
181 case 1:
182 f = new TF1("dgaus",dgausformula.Data(),min,max);
183 f->SetLineColor(kYellow);
184 f->SetBit(kCanDelete);
185 f->SetParNames("A_{tot}","#mu_{1}","#sigma_{1}","A_{2}","#mu_{2}","#sigma_{2}");
186 f->SetParameters(integ,(min+mean)/2.,width/4.,
187 integ/width/2.,(max+mean)/2.,width/4.);
188 // The left-sided Gauss
189 f->SetParLimits(0,integ-1.5 , integ+1.5);
190 f->SetParLimits(1,min+(width/10.), mean);
191 f->SetParLimits(2,0 , width/2.);
192 // The right-sided Gauss
193 f->SetParLimits(3,0 , integ);
194 f->SetParLimits(4,mean, max-(width/10.));
195 f->SetParLimits(5,0 , width/2.);
196 obj2->Fit(f,"QLRM");
197 break;
198
199 default:
200 obj2->Fit("gaus","Q");
201 obj2->GetFunction("gaus")->SetLineColor(kYellow);
202 break;
203 }
204}
205
206
207
208void MJPedestal::CamDraw(TCanvas &c, Int_t x, Int_t y, MHCamera &cam1, Int_t fit)
209{
210
211 c.cd(x);
212 gPad->SetBorderMode(0);
213 MHCamera *obj1=(MHCamera*)cam1.DrawCopy("hist");
214 obj1->AddNotify(fPedestalCam);
215
216 c.cd(x+y);
217 gPad->SetBorderMode(0);
218 obj1->Draw();
219 ((MHCamera*)obj1)->SetPrettyPalette();
220
221 c.cd(x+2*y);
222 gPad->SetBorderMode(0);
223 DrawProjection(obj1, fit);
224
225}
226
227
228Bool_t MJPedestal::WriteResult()
229{
230 if (fOutputPath.IsNull())
231 return kTRUE;
232
233 const TString oname(GetOutputFile());
234
235 *fLog << inf << "Writing to file: " << oname << endl;
236
237 TFile file(oname, "UPDATE");
238
239 if (fDisplay && fDisplay->Write()<=0)
240 {
241 *fLog << err << "Unable to write MStatusDisplay to " << oname << endl;
242 return kFALSE;
243 }
244
245 if (fPedestalCam.Write()<=0)
246 {
247 *fLog << err << "Unable to write MPedestalCam to " << oname << endl;
248 return kFALSE;
249 }
250 return kTRUE;
251}
252
253void MJPedestal::SetOutputPath(const char *path)
254{
255 fOutputPath = path;
256 if (fOutputPath.EndsWith("/"))
257 fOutputPath = fOutputPath(0, fOutputPath.Length()-1);
258}
259
260Bool_t MJPedestal::Process()
261{
262 if (!ReadPedestalCam())
263 return ProcessFile();
264
265 return kTRUE;
266}
267
268Bool_t MJPedestal::ProcessFile()
269{
270 if (!fRuns)
271 {
272 *fLog << err << "No Runs choosen... abort." << endl;
273 return kFALSE;
274 }
275 if (fRuns->GetNumRuns() != fRuns->GetNumEntries())
276 {
277 *fLog << err << "Number of files found doesn't metch number of runs... abort." << endl;
278 return kFALSE;
279 }
280
281 *fLog << inf;
282 fLog->Separator(GetDescriptor());
283 *fLog << "Calculate MPedestalCam from Runs " << fRuns->GetRunsAsString() << endl;
284 *fLog << endl;
285
286 MReadMarsFile read("Events");
287 read.DisableAutoScheme();
288 static_cast<MRead&>(read).AddFiles(*fRuns);
289
290 // Enable logging to file
291 //*fLog.SetOutputFile(lname, kTRUE);
292
293 // Setup Tasklist
294 MParList plist;
295 plist.AddToList(&fPedestalCam);
296
297 MTaskList tlist;
298 plist.AddToList(&tlist);
299
300 MGeomApply geomapl;
301 //MExtractSignal sigcalc;
302 MPedCalcPedRun pedcalc;
303
304 tlist.AddToList(&read);
305 tlist.AddToList(&geomapl);
306 //tlist.AddToList(&sigcalc);
307 tlist.AddToList(&pedcalc);
308
309 //
310 // Execute the eventloop
311 //
312 MEvtLoop evtloop(fName);
313 evtloop.SetParList(&plist);
314 evtloop.SetDisplay(fDisplay);
315 evtloop.SetLogStream(fLog);
316
317 // Execute first analysis
318 if (!evtloop.Eventloop())
319 {
320 *fLog << err << GetDescriptor() << ": Failed." << endl;
321 return kFALSE;
322 }
323
324 tlist.PrintStatistics();
325
326 DisplayResult(plist);
327
328 if (!WriteResult())
329 return kFALSE;
330
331 *fLog << inf << GetDescriptor() << ": Done." << endl;
332
333 return kTRUE;
334}
Note: See TracBrowser for help on using the repository browser.