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

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