source: trunk/Mars/mhist/MHCamEvent.cc@ 12725

Last change on this file since 12725 was 11569, checked in by tbretz, 13 years ago
removed some obsolete calls to Format(); correctly display the relative and absolute error
File size: 10.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, 12/2002 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2003
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MHCamEvent
28//
29// A histogram to store the sum of camera events. This can be photons,
30// currents or enything else derived from MCamEvent
31//
32// Setup
33// =====
34//
35// To count how often a certain pixel is above or below a threshold do:
36// MHCamEvent::SetThreshold(5.5); // Default=LowerBound
37// MHCamEvent::SetThreshold(5.5, MHCamEvent::kIsUpperBound);
38//
39//
40// Axis titles
41// ===========
42//
43// 1) If no other title is given 'a.u.' is used.
44// 2) If the title of MHCamEvent is different from the default,
45// it is used as histogram title. You can use this to set the
46// axis title, too. For more information see TH1::SetTitle, eg.
47// SetTitle("MyHist;;y[cm];Counts");
48// Make sure to set an empty x-axis title.
49//
50// For example:
51// MHCamEvent myhist("Titele;;y [cm]");
52//
53/////////////////////////////////////////////////////////////////////////////
54#include "MHCamEvent.h"
55
56#include "MLog.h"
57#include "MLogManip.h"
58
59#include "MString.h"
60#include "MParList.h"
61#include "MCamEvent.h"
62#include "MHCamera.h"
63
64#include "MGeomCam.h"
65
66ClassImp(MHCamEvent);
67
68using namespace std;
69
70const TString MHCamEvent::gsDefName = "MHCamEvent";
71const TString MHCamEvent::gsDefTitle = "Average of MCamEvents";
72
73// --------------------------------------------------------------------------
74//
75// Initialize the name and title of the task
76//
77void MHCamEvent::Init(const char *name, const char *title)
78{
79 //
80 // set the name and title of this object
81 //
82 fName = name ? name : gsDefName.Data();
83 fTitle = title ? title : gsDefTitle.Data();
84
85 fNameGeom = "MGeomCam";
86}
87
88// --------------------------------------------------------------------------
89//
90// Initialize the name and title of the task. Set fType to 0
91//
92MHCamEvent::MHCamEvent(const char *name, const char *title)
93: fSum(NULL), fErr(NULL), fEvt(NULL), fType(0), fErrorSpread(kTRUE), fErrorRelative(kFALSE),
94fThreshold(0), fUseThreshold(0)
95{
96 Init(name, title);
97
98 fSum = new MHCamera;
99}
100
101// --------------------------------------------------------------------------
102//
103// Initialize the name and title of the task. Set fType to type
104//
105MHCamEvent::MHCamEvent(Int_t type, const char *name, const char *title)
106: fSum(NULL), fErr(NULL), fEvt(NULL), fType(type), fErrorSpread(kTRUE), fErrorRelative(kFALSE),
107fThreshold(0), fUseThreshold(0)
108{
109 Init(name, title);
110
111 fSum = new MHCamera;
112}
113
114// --------------------------------------------------------------------------
115//
116// Delete the corresponding camera display if available
117//
118MHCamEvent::~MHCamEvent()
119{
120 if (fSum)
121 delete fSum;
122}
123
124Bool_t MHCamEvent::InitGeom(const MParList &plist)
125{
126 MGeomCam *cam = (MGeomCam*)plist.FindObject(fNameGeom, "MGeomCam");
127 if (!cam)
128 {
129 *fLog << err << fNameGeom << " [MGeomCam] not found... abort." << endl;
130 return kFALSE;
131 }
132
133 // combine name
134 const TString name = fNameEvt.IsNull() ? fName : fNameEvt;
135
136 fSum->SetGeometry(*cam, name+";avg");
137 if (fTitle!=gsDefTitle)
138 fSum->SetTitle(fTitle);
139 if (!fTitle.Contains(";"))
140 fSum->SetYTitle("a.u.");
141
142 if (fErr)
143 fErr->SetGeometry(*fSum->GetGeometry(), fErr->GetName(), fErr->GetTitle());
144
145 return kTRUE;
146}
147
148// --------------------------------------------------------------------------
149//
150// Get the event (MCamEvent) the histogram might be filled with. If
151// it is not given, it is assumed, that it is filled with the argument
152// of the Fill function.
153// Looks for the camera geometry MGeomCam and resets the sum histogram.
154//
155Bool_t MHCamEvent::SetupFill(const MParList *plist)
156{
157 fEvt = (MCamEvent*)plist->FindObject(fNameEvt, "MCamEvent");
158 if (!fEvt)
159 {
160 if (!fNameEvt.IsNull())
161 {
162 *fLog << err << GetDescriptor() << ": No " << fNameEvt <<" [MCamEvent] available..." << endl;
163 return kFALSE;
164 }
165 *fLog << inf << GetDescriptor() << ": Assuming to get MCamEvent from Fill()..." << endl;
166 }
167
168 fSum->Reset();
169
170 fNumReInit = 0;
171
172 if (!InitGeom(*plist))
173 return kFALSE;
174
175 if (fUseThreshold!=kCollectMin && fUseThreshold!=kCollectMax)
176 fSum->SetBit(MHCamera::kProfile);
177 if (!fErrorSpread)
178 fSum->SetBit(MHCamera::kErrorMean);
179
180 return kTRUE;
181}
182
183// --------------------------------------------------------------------------
184//
185// The geometry read from the RunHeaders might have changed. This does not
186// effect anything in PreProcess. So we set a new geometry. We don't move
187// this away from PreProcess to support also loops without calling ReInit.
188//
189Bool_t MHCamEvent::ReInit(MParList *plist)
190{
191 return fNumReInit++==0 ? InitGeom(*plist) : kTRUE;
192}
193
194
195// --------------------------------------------------------------------------
196//
197// Fill the histograms with data from a MCamEvent-Container.
198//
199Int_t MHCamEvent::Fill(const MParContainer *par, const Stat_t w)
200{
201 const MCamEvent *evt = par ? dynamic_cast<const MCamEvent*>(par) : fEvt;
202 if (!evt)
203 {
204 *fLog << err << dbginf << "Got no MCamEvent as argument of Fill()..." << endl;
205 return kERROR;
206 }
207
208 switch (fUseThreshold)
209 {
210 case kNoBound:
211 fSum->AddCamContent(*evt, fType);
212 break;
213
214 case kIsLowerBound:
215 case kIsUpperBound:
216 fSum->CntCamContent(*evt, fThreshold, fType, fUseThreshold>0);
217 break;
218
219 case kCollectMin:
220 fSum->SetMinCamContent(*evt, /*fThreshold,*/ fType);
221 break;
222
223 case kCollectMax:
224 fSum->SetMaxCamContent(*evt, /*fThreshold,*/ fType);
225 break;
226
227 default:
228 *fLog << err << "ERROR - MHCamEvent::Fill: Unknown type." << endl;
229 return kERROR;
230 }
231
232 return kTRUE;
233}
234
235// --------------------------------------------------------------------------
236//
237// Take the mean of the sum histogram and print all pixel indices
238// which are above sum+s*rms
239//
240void MHCamEvent::PrintOutliers(Float_t s) const
241{
242 const Double_t mean = fSum->GetMean();
243 const Double_t rms = fSum->GetRMS();
244
245 *fLog << all << underline << GetDescriptor() << ": Mean=" << mean << ", Rms=" << rms << endl;
246
247 for (UInt_t i=0; i<fSum->GetNumPixels(); i++)
248 {
249 if (!fSum->IsUsed(i))
250 continue;
251
252 if ((*fSum)[i+1]>mean+s*rms)
253 *fLog << "Contents of Pixel-Index #" << i << ": " << (*fSum)[i+1] << " > " << s << "*rms" << endl;
254 }
255}
256
257// --------------------------------------------------------------------------
258//
259// Return fSum for "sum" and fRms for "rms"
260//
261TH1 *MHCamEvent::GetHistByName(const TString name) const
262{
263 return fSum;
264}
265
266// --------------------------------------------------------------------------
267//
268// Set the camera histogram to a clone of cam
269//
270void MHCamEvent::SetHist(const MHCamera &cam)
271{
272 if (fSum)
273 delete fSum;
274
275 fSum = static_cast<MHCamera*>(cam.Clone());
276}
277
278TString MHCamEvent::Format(const char *ext) const
279{
280 TString n = fSum->GetName();
281
282 const Ssiz_t pos = n.Last(';');
283 if (pos<0)
284 return "";
285
286 n = n(0, pos);
287 n += ";";
288 n += ext;
289 return n;
290}
291
292void MHCamEvent::Paint(Option_t *)
293{
294 TVirtualPad *pad = gPad;
295
296 pad->cd(2);
297 if (gPad->FindObject("proj"))
298 {
299 TH1 *h=fSum->Projection("proj");
300 if (h->GetMaximum()>0)
301 gPad->SetLogy();
302 }
303
304 pad->cd(5);
305 if (gPad->FindObject("rad"))
306 fSum->RadialProfile("rad");
307
308 pad->cd(6);
309 if (gPad->FindObject("az"))
310 fSum->AzimuthProfile("az");
311
312 pad->cd(4);
313 gPad->cd(1);
314 MHCamera *cam = (MHCamera*)gPad->FindObject("err");
315 if (cam)
316 cam->SetCamContent(*fSum, fErrorRelative ? 2 : 1);
317}
318
319void MHCamEvent::Draw(Option_t *)
320{
321 TVirtualPad *pad = gPad ? gPad : MakeDefCanvas(this);
322 const Int_t col = pad->GetFillColor();
323 pad->SetBorderMode(0);
324
325 AppendPad();
326
327 TString name = MString::Format("%s_1", pad->GetName());
328 TPad *p = new TPad(name,name,0.005, 0.5, 0.66, 0.995,col,0,0);
329 p->SetFrameBorderMode(0);
330 p->SetNumber(1);
331 p->Draw();
332 p->cd();
333 fSum->Draw("EPhist");
334
335 pad->cd();
336 name = MString::Format("%s_2", pad->GetName());
337 p = new TPad(name,name,0.66, 0.5, 0.995, 0.995,col,0,0);
338 p->SetFrameBorderMode(0);
339 p->SetNumber(2);
340 p->Draw();
341 p->cd();
342 TH1 *h = fSum->Projection("proj", 50);
343 h->SetTitle("Projection");
344 h->SetBit(kCanDelete);
345 h->Draw();
346
347 pad->cd();
348 name = MString::Format("%s_3", pad->GetName());
349 p = new TPad(name,name,0.005, 0.005, 3./8, 0.5,col,0,0);
350 p->SetFrameBorderMode(0);
351 p->SetNumber(3);
352 p->Draw();
353 p->cd();
354 fSum->Draw();
355
356 pad->cd();
357 name = MString::Format("%s_4", pad->GetName());
358 p = new TPad(name,name,3./8, 0.005, 6./8-0.005, 0.5,col,0,0);
359 p->SetFrameBorderMode(0);
360 p->SetNumber(4);
361 p->Draw();
362 p->cd();
363
364 TString e = "Sqrt(Variance)";
365 if (fSum->TestBit(MHCamera::kErrorMean))
366 e += "/Sqrt(n_{i})";
367 if (fErrorRelative)
368 e += "/v_{i}";
369
370 fErr = new MHCamera(*fSum->GetGeometry());
371 fErr->SetName("err");
372 fErr->SetTitle(e);
373 fErr->SetYTitle(fSum->GetYaxis()->GetTitle());
374 fErr->SetCamContent(*fSum, fErrorRelative ? 2 : 1);
375 fErr->SetBit(kMustCleanup);
376 fErr->SetBit(kCanDelete);
377 fErr->Draw();
378
379 pad->cd();
380 name = MString::Format("%s_5", pad->GetName());
381 p = new TPad(name,name,6./8,0.25,0.995,0.5,col,0,0);
382 p->SetFrameBorderMode(0);
383 p->SetNumber(5);
384 p->Draw();
385 p->cd();
386 h = (TH1*)fSum->RadialProfile("rad", 20);
387 h->SetTitle("Radial Profile");
388 h->SetBit(kCanDelete|TH1::kNoStats);
389 h->Draw();
390
391 pad->cd();
392 name = MString::Format("%s_6", pad->GetName());
393 p = new TPad(name,name,6./8,0.005,0.995,0.25,col,0,0);
394 p->SetFrameBorderMode(0);
395 p->SetNumber(6);
396 p->Draw();
397 p->cd();
398 h = (TH1*)fSum->AzimuthProfile("az", 30);
399 h->SetTitle("Azimuth Profile");
400 h->SetBit(kCanDelete|TH1::kNoStats);
401 h->Draw();
402}
403
404
405void MHCamEvent::RecursiveRemove(TObject *obj)
406{
407 if (obj==fErr)
408 fErr = 0;
409 if (obj==fSum)
410 fSum = 0;
411
412 MH::RecursiveRemove(obj);
413}
Note: See TracBrowser for help on using the repository browser.