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

Last change on this file since 12922 was 12922, checked in by tbretz, 13 years ago
Added a possibility to histogram the deviation from a median or mean value event per event.
File size: 11.0 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 case kMeanShift:
228 fSum->AddMeanShift(*evt, fType);
229 break;
230
231 case kMedianShift:
232 fSum->AddMedianShift(*evt, fType);
233 break;
234
235 default:
236 *fLog << err << "ERROR - MHCamEvent::Fill: Unknown type." << endl;
237 return kERROR;
238 }
239
240 return kTRUE;
241}
242
243// --------------------------------------------------------------------------
244//
245// Take the mean of the sum histogram and print all pixel indices
246// which are above sum+s*rms
247//
248void MHCamEvent::PrintOutliers(Float_t s) const
249{
250 const Double_t mean = fSum->GetMean();
251 const Double_t rms = fSum->GetRMS();
252
253 *fLog << all << underline << GetDescriptor() << ": Mean=" << mean << ", Rms=" << rms << endl;
254
255 for (UInt_t i=0; i<fSum->GetNumPixels(); i++)
256 {
257 if (!fSum->IsUsed(i))
258 continue;
259
260 if ((*fSum)[i+1]>mean+s*rms)
261 *fLog << "Contents of Pixel-Index #" << i << ": " << (*fSum)[i+1] << " > " << s << "*rms" << endl;
262 }
263}
264
265// --------------------------------------------------------------------------
266//
267// Return fSum for "sum" and fRms for "rms"
268//
269TH1 *MHCamEvent::GetHistByName(const TString name) const
270{
271 return fSum;
272}
273
274// --------------------------------------------------------------------------
275//
276// Set the camera histogram to a clone of cam
277//
278void MHCamEvent::SetHist(const MHCamera &cam)
279{
280 if (fSum)
281 delete fSum;
282
283 fSum = static_cast<MHCamera*>(cam.Clone());
284}
285
286TString MHCamEvent::Format(const char *ext) const
287{
288 TString n = fSum->GetName();
289
290 const Ssiz_t pos = n.Last(';');
291 if (pos<0)
292 return "";
293
294 n = n(0, pos);
295 n += ";";
296 n += ext;
297 return n;
298}
299
300void MHCamEvent::Paint(Option_t *)
301{
302 TVirtualPad *pad = gPad;
303
304 pad->cd(2);
305 if (gPad->FindObject("proj"))
306 {
307 TH1 *h=fSum->Projection("proj");
308 if (h->GetMaximum()>0)
309 gPad->SetLogy();
310 }
311
312 pad->cd(5);
313 if (gPad->FindObject("rad"))
314 fSum->RadialProfile("rad");
315
316 pad->cd(6);
317 if (gPad->FindObject("az"))
318 fSum->AzimuthProfile("az");
319
320 pad->cd(4);
321 gPad->cd(1);
322 MHCamera *cam = (MHCamera*)gPad->FindObject("err");
323 if (cam)
324 cam->SetCamContent(*fSum, fErrorRelative ? 2 : 1);
325}
326
327void MHCamEvent::Draw(Option_t *)
328{
329 TVirtualPad *pad = gPad ? gPad : MakeDefCanvas(this);
330 const Int_t col = pad->GetFillColor();
331 pad->SetBorderMode(0);
332
333 AppendPad();
334
335 TString name = MString::Format("%s_1", pad->GetName());
336 TPad *p = new TPad(name,name,0.005, 0.5, 0.66, 0.995,col,0,0);
337 p->SetFrameBorderMode(0);
338 p->SetNumber(1);
339 p->Draw();
340 p->cd();
341 fSum->Draw("EPhist");
342
343 pad->cd();
344 name = MString::Format("%s_2", pad->GetName());
345 p = new TPad(name,name,0.66, 0.5, 0.995, 0.995,col,0,0);
346 p->SetFrameBorderMode(0);
347 p->SetNumber(2);
348 p->Draw();
349 p->cd();
350 TH1 *h = fSum->Projection("proj", 50);
351 h->SetTitle("Projection");
352 h->SetBit(kCanDelete);
353 h->Draw();
354
355 pad->cd();
356 name = MString::Format("%s_3", pad->GetName());
357 p = new TPad(name,name,0.005, 0.005, 3./8, 0.5,col,0,0);
358 p->SetFrameBorderMode(0);
359 p->SetNumber(3);
360 p->Draw();
361 p->cd();
362 fSum->Draw();
363
364 pad->cd();
365 name = MString::Format("%s_4", pad->GetName());
366 p = new TPad(name,name,3./8, 0.005, 6./8-0.005, 0.5,col,0,0);
367 p->SetFrameBorderMode(0);
368 p->SetNumber(4);
369 p->Draw();
370 p->cd();
371
372 TString e = "Sqrt(Variance)";
373 if (fSum->TestBit(MHCamera::kErrorMean))
374 e += "/Sqrt(n_{i})";
375 if (fErrorRelative)
376 e += "/v_{i}";
377
378 fErr = new MHCamera(*fSum->GetGeometry());
379 fErr->SetName("err");
380 fErr->SetTitle(e);
381 fErr->SetYTitle(fSum->GetYaxis()->GetTitle());
382 fErr->SetCamContent(*fSum, fErrorRelative ? 2 : 1);
383 fErr->SetBit(kMustCleanup);
384 fErr->SetBit(kCanDelete);
385 fErr->Draw();
386
387 pad->cd();
388 name = MString::Format("%s_5", pad->GetName());
389 p = new TPad(name,name,6./8,0.25,0.995,0.5,col,0,0);
390 p->SetFrameBorderMode(0);
391 p->SetNumber(5);
392 p->Draw();
393 p->cd();
394 h = (TH1*)fSum->RadialProfile("rad", 20);
395 h->SetTitle("Radial Profile");
396 h->SetBit(kCanDelete|TH1::kNoStats);
397 h->Draw();
398
399 pad->cd();
400 name = MString::Format("%s_6", pad->GetName());
401 p = new TPad(name,name,6./8,0.005,0.995,0.25,col,0,0);
402 p->SetFrameBorderMode(0);
403 p->SetNumber(6);
404 p->Draw();
405 p->cd();
406 h = (TH1*)fSum->AzimuthProfile("az", 30);
407 h->SetTitle("Azimuth Profile");
408 h->SetBit(kCanDelete|TH1::kNoStats);
409 h->Draw();
410}
411
412
413void MHCamEvent::RecursiveRemove(TObject *obj)
414{
415 if (obj==fErr)
416 fErr = 0;
417 if (obj==fSum)
418 fSum = 0;
419
420 MH::RecursiveRemove(obj);
421}
Note: See TracBrowser for help on using the repository browser.