source: trunk/MagicSoft/Mars/mhist/MHCamera.cc@ 3957

Last change on this file since 3957 was 3957, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 51.3 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, 05/2002 <mailto:tbretz@astro.uni-wuerzburg.de>
19! Author(s): Harald Kornmayer, 1/2001
20! Author(s): Markus Gaug, 03/2004 <mailto:markus@ifae.es>
21!
22! Copyright: MAGIC Software Development, 2000-2004
23!
24!
25\* ======================================================================== */
26
27/////////////////////////////////////////////////////////////////////////////
28//
29// MHCamera
30//
31// Camera Display, based on a TH1D. Pleas be carefull using the
32// underlaying TH1D.
33//
34// To change the scale to a logarithmic scale SetLogy() of the Pad.
35//
36// You can correct for the abberation. Assuming that the distance
37// between the mean position of the light distribution and the position
38// of a perfect reflection on a perfect mirror in the distance r on
39// the camera plane is dr it is d = a*dr while a is the abberation
40// constant (for the MAGIC mirror it is roughly 0.0713). You can
41// set this constant by calling SetAbberation(a) which results in a
42// 'corrected' display (all outer pixels are shifted towards the center
43// of the camera to correct for this abberation)
44//
45// Be carefull: Entries in this context means Entries/bin or Events
46//
47// FIXME? Maybe MHCamera can take the fLog object from MGeomCam?
48//
49////////////////////////////////////////////////////////////////////////////
50#include "MHCamera.h"
51
52#include <fstream>
53#include <iostream>
54
55#include <TBox.h>
56#include <TArrow.h>
57#include <TLatex.h>
58#include <TStyle.h>
59#include <TCanvas.h>
60#include <TArrayF.h>
61#include <TRandom.h>
62#include <TPaveText.h>
63#include <TPaveStats.h>
64#include <TClonesArray.h>
65#include <THistPainter.h>
66#include <THLimitsFinder.h>
67#include <TProfile.h>
68#include <TH1.h>
69#include <TF1.h>
70
71#include "MLog.h"
72#include "MLogManip.h"
73
74#include "MH.h"
75#include "MHexagon.h"
76
77#include "MGeomPix.h"
78#include "MGeomCam.h"
79
80#include "MCamEvent.h"
81
82#define kItemsLegend 48 // see SetPalette(1,0)
83
84ClassImp(MHCamera);
85
86using namespace std;
87
88void MHCamera::Init()
89{
90 UseCurrentStyle();
91
92 SetDirectory(NULL);
93
94 SetLineColor(kGreen);
95 SetMarkerStyle(kFullDotMedium);
96 SetXTitle("Pixel Index");
97
98 fNotify = new TList;
99 fNotify->SetBit(kMustCleanup);
100 gROOT->GetListOfCleanups()->Add(fNotify);
101
102#if ROOT_VERSION_CODE < ROOT_VERSION(3,01,06)
103 SetPalette(1, 0);
104#else
105 SetInvDeepBlueSeaPalette();
106#endif
107}
108
109// ------------------------------------------------------------------------
110//
111// Default Constructor. To be used by the root system ONLY.
112//
113MHCamera::MHCamera() : TH1D(), fGeomCam(NULL), fColors(kItemsLegend), fAbberation(0)
114{
115 Init();
116}
117
118// ------------------------------------------------------------------------
119//
120// Constructor. Makes a clone of MGeomCam. Removed the TH1D from the
121// current directory. Calls Sumw2(). Set the histogram line color
122// (for error bars) to Green and the marker style to kFullDotMedium.
123//
124MHCamera::MHCamera(const MGeomCam &geom, const char *name, const char *title)
125: fGeomCam(NULL), fColors(kItemsLegend), fAbberation(0)
126{
127 //fGeomCam = (MGeomCam*)geom.Clone();
128 SetGeometry(geom, name, title);
129 Init();
130
131 //
132 // root 3.02
133 // * base/inc/TObject.h:
134 // register BIT(8) as kNoContextMenu. If an object has this bit set it will
135 // not get an automatic context menu when clicked with the right mouse button.
136}
137
138void MHCamera::SetGeometry(const MGeomCam &geom, const char *name, const char *title)
139{
140 SetNameTitle(name, title);
141
142 TAxis &x = *GetXaxis();
143
144 SetBins(geom.GetNumPixels(), 0, 1);
145 x.Set(geom.GetNumPixels(), -0.5, geom.GetNumPixels()-0.5);
146
147 //SetBins(geom.GetNumPixels(), -0.5, geom.GetNumPixels()-0.5);
148 //Rebuild();
149
150 Sumw2(); // necessary?
151
152 if (fGeomCam)
153 delete fGeomCam;
154 fGeomCam = (MGeomCam*)geom.Clone();
155
156 fUsed.Set(geom.GetNumPixels());
157 for (Int_t i=0; i<fNcells-2; i++)
158 ResetUsed(i);
159}
160
161// ------------------------------------------------------------------------
162//
163// Destructor. Deletes the cloned fGeomCam and the notification list.
164//
165MHCamera::~MHCamera()
166{
167 if (fGeomCam)
168 delete fGeomCam;
169 if (fNotify)
170 delete fNotify;
171}
172
173// ------------------------------------------------------------------------
174//
175// Return kTRUE for sector<0. Otherwise return kTRUE only if the specified
176// sector idx matches the sector of the pixel with index idx.
177//
178Bool_t MHCamera::MatchSector(Int_t idx, const TArrayI &sector, const TArrayI &aidx) const
179{
180 const MGeomPix &pix = (*fGeomCam)[idx];
181 return FindVal(sector, pix.GetSector()) && FindVal(aidx, pix.GetAidx());
182}
183
184// ------------------------------------------------------------------------
185//
186// Taken from TH1D::Fill(). Uses the argument directly as bin index.
187// Doesn't increment the number of entries.
188//
189// -*-*-*-*-*-*-*-*Increment bin with abscissa X by 1*-*-*-*-*-*-*-*-*-*-*
190// ==================================
191//
192// if x is less than the low-edge of the first bin, the Underflow bin is incremented
193// if x is greater than the upper edge of last bin, the Overflow bin is incremented
194//
195// If the storage of the sum of squares of weights has been triggered,
196// via the function Sumw2, then the sum of the squares of weights is incremented
197// by 1 in the bin corresponding to x.
198//
199// -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
200Int_t MHCamera::Fill(Axis_t x)
201{
202#if ROOT_VERSION_CODE > ROOT_VERSION(3,05,00)
203 if (fBuffer) return BufferFill(x,1);
204#endif
205 const Int_t bin = (Int_t)x+1;
206 AddBinContent(bin);
207 if (fSumw2.fN)
208 fSumw2.fArray[bin]++;
209
210 if (bin<=0 || bin>fNcells-2)
211 return -1;
212
213 fTsumw++;
214 fTsumw2++;
215 fTsumwx += x;
216 fTsumwx2 += x*x;
217 return bin;
218}
219
220// ------------------------------------------------------------------------
221//
222// Taken from TH1D::Fill(). Uses the argument directly as bin index.
223// Doesn't increment the number of entries.
224//
225// -*-*-*-*-*-*Increment bin with abscissa X with a weight w*-*-*-*-*-*-*-*
226// =============================================
227//
228// if x is less than the low-edge of the first bin, the Underflow bin is incremented
229// if x is greater than the upper edge of last bin, the Overflow bin is incremented
230//
231// If the storage of the sum of squares of weights has been triggered,
232// via the function Sumw2, then the sum of the squares of weights is incremented
233// by w^2 in the bin corresponding to x.
234//
235// -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
236Int_t MHCamera::Fill(Axis_t x, Stat_t w)
237{
238#if ROOT_VERSION_CODE > ROOT_VERSION(3,05,00)
239 if (fBuffer) return BufferFill(x,w);
240#endif
241 const Int_t bin = (Int_t)x+1;
242 AddBinContent(bin, w);
243 if (fSumw2.fN)
244 fSumw2.fArray[bin] += w*w;
245
246 if (bin<=0 || bin>fNcells-2)
247 return -1;
248
249 const Stat_t z = (w > 0 ? w : -w);
250 fTsumw += z;
251 fTsumw2 += z*z;
252 fTsumwx += z*x;
253 fTsumwx2 += z*x*x;
254 return bin;
255}
256
257// ------------------------------------------------------------------------
258//
259// Use x and y in millimeters
260//
261Int_t MHCamera::Fill(Axis_t x, Axis_t y, Stat_t w)
262{
263 if (fNcells<=1 || IsFreezed())
264 return -1;
265
266 for (Int_t idx=0; idx<fNcells-2; idx++)
267 {
268 MHexagon hex((*fGeomCam)[idx]);
269 if (hex.DistanceToPrimitive(x, y)>0)
270 continue;
271
272 SetUsed(idx);
273 return Fill(idx, w);
274 }
275 return -1;
276}
277
278// ------------------------------------------------------------------------
279//
280// Call this if you want to change the display status (displayed or not)
281// for all pixels. val==0 means that the pixel is not displayed.
282//
283void MHCamera::SetUsed(const TArrayC &arr)
284{
285 if (fNcells-2 != arr.GetSize())
286 {
287 gLog << warn << "WARNING - MHCamera::SetUsed: array size mismatch... ignored." << endl;
288 return;
289 }
290
291 for (Int_t idx=0; idx<fNcells-2; idx++)
292 arr[idx] ? SetUsed(idx) : ResetUsed(idx);
293}
294
295// ------------------------------------------------------------------------
296//
297// Return the mean value of all entries which are used if all=kFALSE and
298// of all entries if all=kTRUE if sector<0. If sector>=0 only
299// entries with match the given sector are taken into account.
300//
301Stat_t MHCamera::GetMeanSectors(const TArrayI &sector, const TArrayI &aidx, Bool_t all) const
302{
303 if (fNcells<=1)
304 return 0;
305
306 Int_t n=0;
307
308 Stat_t mean = 0;
309 for (int i=0; i<fNcells-2; i++)
310 {
311 if ((all || IsUsed(i)) && MatchSector(i, sector, aidx))
312 {
313 mean += fArray[i+1];
314 n++;
315 }
316 }
317
318 return n==0 ? 0 : Profile(mean/n);
319}
320
321// ------------------------------------------------------------------------
322//
323// Return the variance of all entries which are used if all=kFALSE and
324// of all entries if all=kTRUE if sector<0. If sector>=0 only
325// entries with match the given sector are taken into account.
326//
327Stat_t MHCamera::GetRmsSectors(const TArrayI &sector, const TArrayI &aidx, Bool_t all) const
328{
329 if (fNcells<=1)
330 return -1;
331
332 Int_t n=0;
333
334 Stat_t sum = 0;
335 Stat_t sq = 0;
336 for (int i=0; i<fNcells-2; i++)
337 {
338 if ((all || IsUsed(i)) && MatchSector(i, sector, aidx))
339 {
340 sum += fArray[i+1];
341 sq += fArray[i+1]*fArray[i+1];
342 n++;
343 }
344 }
345
346 if (n==0)
347 return 0;
348
349 sum /= n;
350 sq /= n;
351
352 return Profile(TMath::Sqrt(sq-sum*sum));
353}
354
355// ------------------------------------------------------------------------
356//
357// Return the minimum contents of all pixels (if all is set, otherwise
358// only of all 'used' pixels), fMinimum if fMinimum set. If sector>=0
359// only pixels with matching sector number are taken into account.
360//
361Double_t MHCamera::GetMinimumSectors(const TArrayI &sector, const TArrayI &aidx, Bool_t all) const
362{
363 if (fMinimum != -1111)
364 return fMinimum;
365
366 if (fNcells<=1)
367 return 0;
368
369 Double_t minimum=FLT_MAX;
370
371 for (Int_t idx=0; idx<fNcells-2; idx++)
372 if (MatchSector(idx, sector, aidx) && (all || IsUsed(idx)) && fArray[idx+1]<minimum)
373 minimum = fArray[idx+1];
374
375 return Profile(minimum);
376}
377
378// ------------------------------------------------------------------------
379//
380// Return the maximum contents of all pixels (if all is set, otherwise
381// only of all 'used' pixels), fMaximum if fMaximum set. If sector>=0
382// only pixels with matching sector number are taken into account.
383//
384Double_t MHCamera::GetMaximumSectors(const TArrayI &sector, const TArrayI &aidx, Bool_t all) const
385{
386 if (fMaximum!=-1111)
387 return fMaximum;
388
389 if (fNcells<=1)
390 return 1;
391
392 Double_t maximum=-FLT_MAX;
393 for (Int_t idx=0; idx<fNcells-2; idx++)
394 if (MatchSector(idx, sector, aidx) && (all || IsUsed(idx)) && fArray[idx+1]>maximum)
395 maximum = fArray[idx+1];
396
397 return Profile(maximum);
398}
399
400// ------------------------------------------------------------------------
401//
402// Call this function to draw the camera layout into your canvas.
403// Setup a drawing canvas. Add this object and all child objects
404// (hexagons, etc) to the current pad. If no pad exists a new one is
405// created. (To access the 'real' pad containing the camera you have
406// to do a cd(1) in the current layer.
407//
408// To draw a camera into its own pad do something like:
409//
410// MGeomCamMagic m;
411// MHCamera *d=new MHCamera(m);
412//
413// TCanvas *c = new TCanvas;
414// c->Divide(2,1);
415// c->cd(1);
416//
417// d->FillRandom();
418// d->Draw();
419// d->SetBit(kCanDelete);
420//
421// There are several drawing options:
422// 'hist' Draw as a standard TH1 histogram (value vs. pixel index)
423// 'box' Draw hexagons which size is in respect to its contents
424// 'nocol' Leave the 'boxed' hexagons empty
425// 'pixelindex' Display the pixel index in each pixel
426// 'sectorindex' Display the sector index in each pixel
427// 'content' Display the relative content aligned to GetMaximum() and
428// GeMinimum() ((val-min)/(max-min))
429// 'proj' Display the y-projection of the histogram
430// 'same' Draw trandparent pixels on top of an existing pad. This
431// makes it possible to draw the camera image on top of an
432// existing TH2, but also allows for distorted camera images
433//
434void MHCamera::Draw(Option_t *option)
435{
436 const Bool_t hassame = TString(option).Contains("same", TString::kIgnoreCase) && gPad;
437
438 // root 3.02:
439 // gPad->SetFixedAspectRatio()
440 const Color_t col = gPad ? gPad->GetFillColor() : 16;
441 TVirtualPad *pad = gPad ? gPad : MH::MakeDefCanvas("CamDisplay", "Mars Camera Display", 656, 600);
442
443 if (!hassame)
444 {
445 pad->SetBorderMode(0);
446 pad->SetFillColor(col);
447
448 //
449 // Create an own pad for the MHCamera-Object which can be
450 // resized in paint to keep the correct aspect ratio
451 //
452 pad->Divide(1, 1, 0, 0, col);
453 pad->cd(1);
454 gPad->SetBorderMode(0);
455 }
456
457 AppendPad(option);
458 //fGeomCam->AppendPad();
459
460 //
461 // Do not change gPad. The user should not see, that Draw
462 // changes gPad...
463 //
464 if (!hassame)
465 pad->cd();
466}
467
468// ------------------------------------------------------------------------
469//
470// This is TObject::DrawClone but completely ignores
471// gROOT->GetSelectedPad(). tbretz had trouble with this in the past.
472// If this makes trouble please write a bug report.
473//
474TObject *MHCamera::DrawClone(Option_t *option) const
475{
476 // Draw a clone of this object in the current pad
477
478 //TVirtualPad *pad = gROOT->GetSelectedPad();
479 TVirtualPad *padsav = gPad;
480 //if (pad) pad->cd();
481
482 TObject *newobj = Clone();
483
484 if (!newobj)
485 return 0;
486
487 /*
488 if (pad) {
489 if (strlen(option)) pad->GetListOfPrimitives()->Add(newobj,option);
490 else pad->GetListOfPrimitives()->Add(newobj,GetDrawOption());
491 pad->Modified(kTRUE);
492 pad->Update();
493 if (padsav) padsav->cd();
494 return newobj;
495 }
496 */
497
498 TString opt(option);
499 opt.ToLower();
500
501 newobj->Draw(opt.IsNull() ? GetDrawOption() : option);
502
503 if (padsav)
504 padsav->cd();
505
506 return newobj;
507}
508
509// ------------------------------------------------------------------------
510//
511// Creates a TH1D which contains the projection of the contents of the
512// MHCamera onto the y-axis. The maximum and minimum are calculated
513// such that a slighly wider range than (GetMinimum(), GetMaximum()) is
514// displayed using THLimitsFinder::OptimizeLimits.
515//
516// If no name is given the newly allocated histogram is removed from
517// the current directory calling SetDirectory(0) in any other case
518// the newly created histogram is removed from the current directory
519// and added to gROOT such the gROOT->FindObject can find the histogram.
520//
521// If the standard name "_py" is given "_py" is appended to the name
522// of the MHCamera and the corresponding histogram is searched using
523// gROOT->FindObject and updated with the present projection.
524//
525// It is the responsibility of the user to make sure, that the newly
526// created histogram is freed correctly.
527//
528// Currently the new histogram is restrictred to 50 bins.
529// Maybe a optimal number can be calulated from the number of
530// bins on the x-axis of the MHCamera?
531//
532// The code was taken mainly from TH2::ProjectX such the interface
533// is more or less the same than to TH2-projections.
534//
535// If sector>=0 only entries with matching sector index are taken
536// into account.
537//
538TH1D *MHCamera::ProjectionS(const TArrayI &sector, const TArrayI &aidx, const char *name) const
539{
540 Int_t nbins = 50;
541
542 // Create the projection histogram
543 TString pname(name);
544 if (name=="_py")
545 {
546 pname.Prepend(GetName());
547 if (sector.GetSize()>0)
548 {
549 pname += ";";
550 for (int i=0; i<sector.GetSize(); i++)
551 pname += sector[i];
552 }
553 if (aidx.GetSize()>0)
554 {
555 pname += ";";
556 for (int i=0; i<aidx.GetSize(); i++)
557 pname += aidx[i];
558 }
559 }
560
561 TH1D *h1=0;
562
563 //check if histogram with identical name exist
564 TObject *h1obj = gROOT->FindObject(pname);
565 if (h1obj && h1obj->InheritsFrom("TH1D")) {
566 h1 = (TH1D*)h1obj;
567 h1->Reset();
568 }
569
570 if (!h1)
571 {
572 Double_t min = GetMinimumSectors(sector, aidx);
573 Double_t max = GetMaximumSectors(sector, aidx);
574
575 Int_t newbins=0;
576
577 THLimitsFinder::OptimizeLimits(nbins, newbins, min, max, kFALSE);
578
579 h1 = new TH1D(pname, GetTitle(), nbins, min, max);
580 h1->SetDirectory(pname.IsNull() ? NULL : gROOT);
581 h1->SetXTitle(GetYaxis()->GetTitle());
582 h1->SetYTitle("Counts");
583 //h1->Sumw2();
584 }
585
586 // Fill the projected histogram
587 for (Int_t idx=0; idx<fNcells-2; idx++)
588 if (IsUsed(idx) && MatchSector(idx, sector, aidx))
589 h1->Fill(GetBinContent(idx+1));
590
591 return h1;
592}
593
594// ------------------------------------------------------------------------
595//
596// Creates a TH1D which contains the projection of the contents of the
597// MHCamera onto the radius from the camera center.
598// The maximum and minimum are calculated
599// such that a slighly wider range than (GetMinimum(), GetMaximum()) is
600// displayed using THLimitsFinder::OptimizeLimits.
601//
602// If no name is given the newly allocated histogram is removed from
603// the current directory calling SetDirectory(0) in any other case
604// the newly created histogram is removed from the current directory
605// and added to gROOT such the gROOT->FindObject can find the histogram.
606//
607// If the standard name "_rad" is given "_rad" is appended to the name
608// of the MHCamera and the corresponding histogram is searched using
609// gROOT->FindObject and updated with the present projection.
610//
611// It is the responsibility of the user to make sure, that the newly
612// created histogram is freed correctly.
613//
614// Currently the new histogram is restrictred to 50 bins.
615// Maybe a optimal number can be calulated from the number of
616// bins on the x-axis of the MHCamera?
617//
618// The code was taken mainly from TH2::ProjectX such the interface
619// is more or less the same than to TH2-projections.
620//
621// If sector>=0 only entries with matching sector index are taken
622// into account.
623//
624TProfile *MHCamera::RadialProfileS(const TArrayI &sector, const TArrayI &aidx, const char *name, const Int_t nbins) const
625{
626
627 // Create the projection histogram
628 TString pname(name);
629 if (name=="_rad")
630 {
631 pname.Prepend(GetName());
632 if (sector.GetSize()>0)
633 {
634 pname += ";";
635 for (int i=0; i<sector.GetSize(); i++)
636 pname += sector[i];
637 }
638 if (aidx.GetSize()>0)
639 {
640 pname += ";";
641 for (int i=0; i<aidx.GetSize(); i++)
642 pname += aidx[i];
643 }
644 }
645
646 TProfile *h1=0;
647
648 //check if histogram with identical name exist
649 TObject *h1obj = gROOT->FindObject(pname);
650 if (h1obj && h1obj->InheritsFrom("TProfile")) {
651 h1 = (TProfile*)h1obj;
652 h1->Reset();
653 }
654
655 if (!h1)
656 {
657
658 Double_t min = 0.;
659 Double_t max = fGeomCam->GetMaxRadius();
660
661 Int_t newbins=0;
662
663 THLimitsFinder::OptimizeLimits(nbins, newbins, min, max, kFALSE);
664
665 h1 = new TProfile(pname, GetTitle(), nbins, min, max);
666 h1->SetDirectory(pname.IsNull() ? NULL : gROOT);
667 h1->SetXTitle("Radius from camera center [mm]");
668 h1->SetYTitle(GetYaxis()->GetTitle());
669 }
670
671 // Fill the projected histogram
672 for (Int_t idx=0; idx<fNcells-2; idx++)
673 if (IsUsed(idx) && MatchSector(idx, sector, aidx))
674 h1->Fill(TMath::Hypot((*fGeomCam)[idx].GetX(),(*fGeomCam)[idx].GetY()),
675 GetBinContent(idx+1));
676 return h1;
677}
678
679
680// ------------------------------------------------------------------------
681//
682// Resizes the current pad so that the camera is displayed in its
683// correct aspect ratio
684//
685void MHCamera::SetRange()
686{
687 const Float_t range = fGeomCam->GetMaxRadius()*1.05;
688
689 //
690 // Maintain aspect ratio
691 //
692 const float ratio = TestBit(kNoLegend) ? 1 : 1.15;
693
694 //
695 // Calculate width and height of the current pad in pixels
696 //
697 Float_t w = gPad->GetWw();
698 Float_t h = gPad->GetWh()*ratio;
699
700 //
701 // This prevents the pad from resizing itself wrongly
702 //
703 if (gPad->GetMother() != gPad)
704 {
705 w *= gPad->GetMother()->GetAbsWNDC();
706 h *= gPad->GetMother()->GetAbsHNDC();
707 }
708
709 //
710 // Set Range (coordinate system) of pad
711 //
712 gPad->Range(-range, -range, (2*ratio-1)*range, range);
713
714 //
715 // Resize Pad to given ratio
716 //
717 if (h<w)
718 gPad->SetPad((1.-h/w)/2, 0, (h/w+1.)/2, 1);
719 else
720 gPad->SetPad(0, (1.-w/h)/2, 1, (w/h+1.)/2);
721}
722
723// ------------------------------------------------------------------------
724//
725// Updates the pixel colors and paints the pixels
726//
727void MHCamera::Update(Bool_t islog, Bool_t isbox, Bool_t iscol, Bool_t issame)
728{
729 Double_t min = GetMinimum(kFALSE);
730 Double_t max = GetMaximum(kFALSE);
731 if (min==FLT_MAX)
732 {
733 min = 0;
734 max = 1;
735 }
736
737 if (min==max)
738 max += 1;
739
740 if (!issame)
741 UpdateLegend(min, max, islog);
742
743 // Try to estimate the units of the current display. This is only
744 // necessary for 'same' option and allows distorted images of the camera!
745 const Float_t maxr = (1-fGeomCam->GetConvMm2Deg())*fGeomCam->GetMaxRadius()/2;
746 const Float_t conv = !issame ||
747 gPad->GetX1()<-maxr || gPad->GetY1()<-maxr ||
748 gPad->GetX2()> maxr || gPad->GetY2()>maxr ? 1 : fGeomCam->GetConvMm2Deg();
749
750 MHexagon hex;
751 for (Int_t i=0; i<fNcells-2; i++)
752 {
753 hex.SetFillStyle(issame ? 0 : 1001);
754
755 if (!issame)
756 {
757 if (IsUsed(i) && iscol)
758 {
759 if (TMath::IsNaN(fArray[i+1]))
760 gLog << warn << "MHCamera::Update: " << GetName() << " <" << GetTitle() << "> - Pixel Index #" << i << " contents is NaN (Not a Number)..." << endl;
761
762 hex.SetFillColor(GetColor(GetBinContent(i+1), min, max, islog));
763 }
764 else
765 hex.SetFillColor(10);
766 }
767
768 const MGeomPix &pix = (*fGeomCam)[i];
769
770 Float_t x = pix.GetX()*conv/(fAbberation+1);
771 Float_t y = pix.GetY()*conv/(fAbberation+1);
772 Float_t d = pix.GetD()*conv;
773
774 if (!isbox)
775 hex.PaintHexagon(x, y, d);
776 else
777 if (IsUsed(i) && !TMath::IsNaN(fArray[i+1]))
778 {
779 Float_t size = d*(GetBinContent(i+1)-min)/(max-min);
780 if (size>d)
781 size=d;
782 hex.PaintHexagon(x, y, size);
783 }
784 }
785}
786
787// ------------------------------------------------------------------------
788//
789// Print minimum and maximum
790//
791void MHCamera::Print(Option_t *) const
792{
793 gLog << all << "Minimum: " << GetMinimum();
794 if (fMinimum==-1111)
795 gLog << " <autoscaled>";
796 gLog << endl;
797 gLog << "Maximum: " << GetMaximum();
798 if (fMaximum==-1111)
799 gLog << " <autoscaled>";
800 gLog << endl;
801}
802
803// ------------------------------------------------------------------------
804//
805// Paint the y-axis title
806//
807void MHCamera::PaintAxisTitle()
808{
809 const Float_t range = fGeomCam->GetMaxRadius()*1.05;
810 const Float_t w = (1 + 1.5/sqrt((float)(fNcells-2)))*range;
811
812 TLatex *ptitle = new TLatex(w, -.90*range, GetYaxis()->GetTitle());
813
814 ptitle->SetTextSize(0.05);
815 ptitle->SetTextAlign(21);
816
817 // box with the histogram title
818 ptitle->SetTextColor(gStyle->GetTitleTextColor());
819#if ROOT_VERSION_CODE > ROOT_VERSION(3,05,01)
820 ptitle->SetTextFont(gStyle->GetTitleFont(""));
821#endif
822 ptitle->Paint();
823}
824
825// ------------------------------------------------------------------------
826//
827// Paints the camera.
828//
829void MHCamera::Paint(Option_t *o)
830{
831 if (fNcells<=1)
832 return;
833
834 TString opt(o);
835 opt.ToLower();
836
837 if (opt.Contains("hist"))
838 {
839 opt.ReplaceAll("hist", "");
840 TH1D::Paint(opt);
841 return;
842 }
843
844 if (opt.Contains("proj"))
845 {
846 opt.ReplaceAll("proj", "");
847 Projection(GetName())->Paint(opt);
848 return;
849 }
850
851 const Bool_t hassame = opt.Contains("same");
852 const Bool_t hasbox = opt.Contains("box");
853 const Bool_t hascol = hasbox ? !opt.Contains("nocol") : kTRUE;
854
855 if (!hassame)
856 {
857 gPad->Clear();
858
859 // Maintain aspect ratio
860 SetRange();
861
862 if (GetPainter())
863 {
864 // Paint statistics
865 if (!TestBit(TH1::kNoStats))
866 fPainter->PaintStat(gStyle->GetOptStat(), NULL);
867
868 // Paint primitives (pixels, color legend, photons, ...)
869 if (fPainter->InheritsFrom(THistPainter::Class()))
870 {
871 static_cast<THistPainter*>(fPainter)->MakeChopt("");
872 static_cast<THistPainter*>(fPainter)->PaintTitle();
873 }
874 }
875 }
876
877 // Update Contents of the pixels and paint legend
878 Update(gPad->GetLogy(), hasbox, hascol, hassame);
879
880 if (!hassame)
881 PaintAxisTitle();
882
883 if (opt.Contains("pixelindex"))
884 PaintIndices(0);
885 if (opt.Contains("sectorindex"))
886 PaintIndices(1);
887 if (opt.Contains("content"))
888 PaintIndices(2);
889}
890
891// ------------------------------------------------------------------------
892//
893// With this function you can change the color palette. For more
894// information see TStyle::SetPalette. Only palettes with 50 colors
895// are allowed.
896// In addition you can use SetPalette(52, 0) to create an inverse
897// deep blue sea palette
898//
899void MHCamera::SetPalette(Int_t ncolors, Int_t *colors)
900{
901 //
902 // If not enough colors are specified skip this.
903 //
904 if (ncolors>1 && ncolors<50)
905 {
906 gLog << err << "MHCamera::SetPalette: Only default palettes with 50 colors are allowed... ignored." << endl;
907 return;
908 }
909
910 //
911 // If ncolors==52 create a reversed deep blue sea palette
912 //
913 if (ncolors==52)
914 {
915 gStyle->SetPalette(51, NULL);
916 TArrayI c(kItemsLegend);
917 for (int i=0; i<kItemsLegend; i++)
918 c[kItemsLegend-i-1] = gStyle->GetColorPalette(i);
919 gStyle->SetPalette(kItemsLegend, c.GetArray());
920 }
921 else
922 gStyle->SetPalette(ncolors, colors);
923
924 fColors.Set(kItemsLegend);
925 for (int i=0; i<kItemsLegend; i++)
926 fColors[i] = gStyle->GetColorPalette(i);
927}
928
929
930void MHCamera::SetPrettyPalette()
931{
932 if (!TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
933 SetPalette(1, 0);
934}
935
936void MHCamera::SetDeepBlueSeaPalette()
937{
938 if (!TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
939 SetPalette(51, 0);
940}
941
942void MHCamera::SetInvDeepBlueSeaPalette()
943{
944 if (!TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
945 SetPalette(52, 0);
946}
947
948void MHCamera::PaintIndices(Int_t type)
949{
950 if (fNcells<=1)
951 return;
952
953 const Double_t min = GetMinimum();
954 const Double_t max = GetMaximum();
955
956 if (type==2 && max==min)
957 return;
958
959 TText txt;
960 txt.SetTextFont(122);
961 txt.SetTextAlign(22); // centered/centered
962
963 for (Int_t i=0; i<fNcells-2; i++)
964 {
965 const MGeomPix &h = (*fGeomCam)[i];
966
967 TString num;
968 switch (type)
969 {
970 case 0: num += i; break;
971 case 1: num += h.GetSector(); break;
972 case 2: num += (Int_t)((fArray[i+1]-min)/(max-min)); break;
973 }
974
975 // FIXME: Should depend on the color of the pixel...
976 //(GetColor(GetBinContent(i+1), min, max, 0));
977 txt.SetTextColor(kRed);
978 txt.SetTextSize(0.3*h.GetD()/fGeomCam->GetMaxRadius()/1.05);
979 txt.PaintText(h.GetX(), h.GetY(), num);
980 }
981}
982
983// ------------------------------------------------------------------------
984//
985// Call this function to add a MCamEvent on top of the present contents.
986//
987void MHCamera::AddCamContent(const MCamEvent &event, Int_t type)
988{
989 if (fNcells<=1 || IsFreezed())
990 return;
991
992 // FIXME: Security check missing!
993 for (Int_t idx=0; idx<fNcells-2; idx++)
994 {
995 Double_t val=0;
996 if (event.GetPixelContent(val, idx, *fGeomCam, type)/* && !IsUsed(idx)*/)
997 SetUsed(idx);
998
999 Fill(idx, val); // FIXME: Slow!
1000 }
1001 fEntries++;
1002}
1003
1004// ------------------------------------------------------------------------
1005//
1006// Call this function to add a MCamEvent on top of the present contents.
1007//
1008void MHCamera::SetCamError(const MCamEvent &evt, Int_t type)
1009{
1010
1011 if (fNcells<=1 || IsFreezed())
1012 return;
1013
1014 // FIXME: Security check missing!
1015 for (Int_t idx=0; idx<fNcells-2; idx++)
1016 {
1017 Double_t val=0;
1018 if (evt.GetPixelContent(val, idx, *fGeomCam, type)/* && !IsUsed(idx)*/)
1019 SetUsed(idx);
1020
1021 SetBinError(idx+1, val); // FIXME: Slow!
1022 }
1023}
1024
1025Stat_t MHCamera::GetBinError(Int_t bin) const
1026{
1027 Double_t rc = 0;
1028 if (TestBit(kVariance) && GetEntries()>0) // error on the mean
1029 {
1030 const Double_t error = fSumw2.fArray[bin]/GetEntries();
1031 const Double_t val = fArray[bin]/GetEntries();
1032 rc = TMath::Sqrt(error - val*val);
1033 }
1034 else
1035 {
1036 rc = TH1D::GetBinError(bin);
1037 rc = Profile(rc);
1038 }
1039
1040 return rc;
1041}
1042
1043// ------------------------------------------------------------------------
1044//
1045// Call this function to add a MHCamera on top of the present contents.
1046// Type:
1047// 0) bin content
1048// 1) errors
1049// 2) rel. errors
1050//
1051void MHCamera::AddCamContent(const MHCamera &d, Int_t type)
1052{
1053 if (fNcells!=d.fNcells || IsFreezed())
1054 return;
1055
1056 // FIXME: Security check missing!
1057 for (Int_t idx=0; idx<fNcells-2; idx++)
1058 if (d.IsUsed(idx))
1059 SetUsed(idx);
1060
1061 switch (type)
1062 {
1063 case 1:
1064 for (Int_t idx=0; idx<fNcells-2; idx++)
1065 Fill(idx, d.GetBinError(idx+1));
1066 break;
1067 case 2:
1068 for (Int_t idx=0; idx<fNcells-2; idx++)
1069 if (d.GetBinContent(idx+1)!=0)
1070 Fill(idx, TMath::Abs(d.GetBinError(idx+1)/d.GetBinContent(idx+1)));
1071 break;
1072 default:
1073 for (Int_t idx=0; idx<fNcells-2; idx++)
1074 Fill(idx, d.GetBinContent(idx+1));
1075 break;
1076 }
1077 fEntries++;
1078}
1079
1080// ------------------------------------------------------------------------
1081//
1082// Call this function to add a TArrayD on top of the present contents.
1083//
1084void MHCamera::AddCamContent(const TArrayD &event, const TArrayC *used)
1085{
1086 if (event.GetSize()!=fNcells-2 || IsFreezed())
1087 return;
1088
1089 if (used && used->GetSize()!=fNcells-2)
1090 return;
1091
1092 for (Int_t idx=0; idx<fNcells-2; idx++)
1093 {
1094 Fill(idx, const_cast<TArrayD&>(event)[idx]); // FIXME: Slow!
1095
1096 if (!used || (*const_cast<TArrayC*>(used))[idx])
1097 SetUsed(idx);
1098 }
1099 fEntries++;
1100}
1101
1102// ------------------------------------------------------------------------
1103//
1104// Call this function to add a MCamEvent on top of the present contents.
1105// 1 is added to each pixel if the contents of MCamEvent>threshold
1106//
1107void MHCamera::CntCamContent(const MCamEvent &event, Double_t threshold, Int_t type)
1108{
1109 if (fNcells<=1 || IsFreezed())
1110 return;
1111
1112 // FIXME: Security check missing!
1113 for (Int_t idx=0; idx<fNcells-2; idx++)
1114 {
1115 Double_t val=threshold;
1116 if (event.GetPixelContent(val, idx, *fGeomCam, type)/* && !IsUsed(idx)*/)
1117 SetUsed(idx);
1118
1119 if (val>threshold)
1120 Fill(idx);
1121 }
1122 fEntries++;
1123}
1124
1125// ------------------------------------------------------------------------
1126//
1127// Call this function to add a TArrayD on top of the present contents.
1128// 1 is added to each pixel if the contents of MCamEvent>threshold
1129//
1130void MHCamera::CntCamContent(const TArrayD &event, Double_t threshold, Bool_t ispos)
1131{
1132 if (event.GetSize()!=fNcells-2 || IsFreezed())
1133 return;
1134
1135 for (Int_t idx=0; idx<fNcells-2; idx++)
1136 {
1137 if (const_cast<TArrayD&>(event)[idx]>threshold)
1138 Fill(idx);
1139
1140 if (!ispos || fArray[idx+1]>0)
1141 SetUsed(idx);
1142 }
1143 fEntries++;
1144}
1145
1146// ------------------------------------------------------------------------
1147//
1148// Fill the pixels with random contents.
1149//
1150void MHCamera::FillRandom()
1151{
1152 if (fNcells<=1 || IsFreezed())
1153 return;
1154
1155 Reset();
1156
1157 // FIXME: Security check missing!
1158 for (Int_t idx=0; idx<fNcells-2; idx++)
1159 {
1160 Fill(idx, gRandom->Uniform()*fGeomCam->GetPixRatio(idx));
1161 SetUsed(idx);
1162 }
1163 fEntries=1;
1164}
1165
1166
1167// ------------------------------------------------------------------------
1168//
1169// The array must be in increasing order, eg: 2.5, 3.7, 4.9
1170// The values in each bin are replaced by the interval in which the value
1171// fits. In the example we have four intervals
1172// (<2.5, 2.5-3.7, 3.7-4.9, >4.9). Maximum and minimum are set
1173// accordingly.
1174//
1175void MHCamera::SetLevels(const TArrayF &arr)
1176{
1177 if (fNcells<=1)
1178 return;
1179
1180 for (Int_t i=0; i<fNcells-2; i++)
1181 {
1182 if (!IsUsed(i))
1183 continue;
1184
1185 Int_t j = arr.GetSize();
1186 while (j && fArray[i+1]<arr[j-1])
1187 j--;
1188
1189 fArray[i+1] = j;
1190 }
1191 SetMaximum(arr.GetSize());
1192 SetMinimum(0);
1193}
1194
1195// ------------------------------------------------------------------------
1196//
1197// Reset the all pixel colors to a default value
1198//
1199void MHCamera::Reset(Option_t *opt)
1200{
1201 if (fNcells<=1 || IsFreezed())
1202 return;
1203
1204 TH1::Reset(opt);
1205
1206 for (Int_t i=0; i<fNcells-2; i++)
1207 {
1208 fArray[i+1]=0;
1209 ResetUsed(i);
1210 }
1211 fArray[0] = 0;
1212 fArray[fNcells-1] = 0;
1213}
1214
1215// ------------------------------------------------------------------------
1216//
1217// Here we calculate the color index for the current value.
1218// The color index is defined with the class TStyle and the
1219// Color palette inside. We use the command gStyle->SetPalette(1,0)
1220// for the display. So we have to convert the value "wert" into
1221// a color index that fits the color palette.
1222// The range of the color palette is defined by the values fMinPhe
1223// and fMaxRange. Between this values we have 50 color index, starting
1224// with 0 up to 49.
1225//
1226Int_t MHCamera::GetColor(Float_t val, Float_t min, Float_t max, Bool_t islog)
1227{
1228 if (TMath::IsNaN(val)) // FIXME: gLog!
1229 return 10;
1230
1231 //
1232 // first treat the over- and under-flows
1233 //
1234 const Int_t maxcolidx = kItemsLegend-1;
1235
1236 if (val >= max)
1237 return fColors[maxcolidx];
1238
1239 if (val <= min)
1240 return fColors[0];
1241
1242 //
1243 // calculate the color index
1244 //
1245 Float_t ratio;
1246 if (islog && min>0)
1247 ratio = log10(val/min) / log10(max/min);
1248 else
1249 ratio = (val-min) / (max-min);
1250
1251 const Int_t colidx = (Int_t)(ratio*maxcolidx + .5);
1252 return fColors[colidx];
1253}
1254
1255TPaveStats *MHCamera::GetStatisticBox()
1256{
1257 TObject *obj = 0;
1258
1259 TIter Next(fFunctions);
1260 while ((obj = Next()))
1261 if (obj->InheritsFrom(TPaveStats::Class()))
1262 return static_cast<TPaveStats*>(obj);
1263
1264 return NULL;
1265}
1266
1267// ------------------------------------------------------------------------
1268//
1269// Change the text on the legend according to the range of the Display
1270//
1271void MHCamera::UpdateLegend(Float_t min, Float_t max, Bool_t islog)
1272{
1273 const Float_t range = fGeomCam->GetMaxRadius()*1.05;
1274
1275 TArrow arr;
1276 arr.PaintArrow(-range*.9, -range*.9, -range*.6, -range*.9, 0.025);
1277 arr.PaintArrow(-range*.9, -range*.9, -range*.9, -range*.6, 0.025);
1278
1279 TString text;
1280 text += (int)(range*.3);
1281 text += "mm";
1282
1283 TText newtxt2;
1284 newtxt2.SetTextSize(0.04);
1285 newtxt2.PaintText(-range*.85, -range*.85, text);
1286
1287 text = "";
1288 text += (float)((int)(range*.3*fGeomCam->GetConvMm2Deg()*10))/10;
1289 text += "\\circ";
1290 text = text.Strip(TString::kLeading);
1291
1292 TLatex latex;
1293 latex.PaintLatex(-range*.85, -range*.75, 0, 0.04, text);
1294
1295 if (TestBit(kNoLegend))
1296 return;
1297
1298 TPaveStats *stats = GetStatisticBox();
1299
1300 const Float_t hndc = 0.92 - (stats ? stats->GetY1NDC() : 1);
1301 const Float_t H = (0.75-hndc)*range;
1302 const Float_t offset = hndc*range;
1303
1304 const Float_t h = 2./kItemsLegend;
1305 const Float_t w = range/sqrt((float)(fNcells-2));
1306
1307 TBox newbox;
1308 TText newtxt;
1309 newtxt.SetTextSize(0.03);
1310 newtxt.SetTextAlign(12);
1311#if ROOT_VERSION_CODE > ROOT_VERSION(3,01,06)
1312 newtxt.SetBit(/*kNoContextMenu|*/kCannotPick);
1313 newbox.SetBit(/*kNoContextMenu|*/kCannotPick);
1314#endif
1315
1316 const Float_t step = (islog && min>0 ? log10(max/min) : max-min) / kItemsLegend;
1317 const Int_t firsts = step*3 < 1e-8 ? 8 : (Int_t)floor(log10(step*3));
1318 const TString opt = Form("%%.%if", firsts>0 ? 0 : TMath::Abs(firsts));
1319
1320 for (Int_t i=0; i<kItemsLegend+1; i+=3)
1321 {
1322 Float_t val;
1323 if (islog && min>0)
1324 val = pow(10, step*i) * min;
1325 else
1326 val = min + step*i;
1327
1328 //const bool dispexp = max-min>1.5 && fabs(val)>0.1 && fabs(val)<1e6;
1329 newtxt.PaintText(range+1.5*w, H*(i*h-1)-offset, Form(opt, val));
1330 }
1331
1332 for (Int_t i=0; i<kItemsLegend; i++)
1333 {
1334 newbox.SetFillColor(fColors[i]);
1335 newbox.PaintBox(range, H*(i*h-1)-offset, range+w, H*((i+1)*h-1)-offset);
1336 }
1337}
1338
1339// ------------------------------------------------------------------------
1340//
1341// Save primitive as a C++ statement(s) on output stream out
1342//
1343void MHCamera::SavePrimitive(ofstream &out, Option_t *opt)
1344{
1345 gLog << err << "MHCamera::SavePrimitive: Must be rewritten!" << endl;
1346 /*
1347 if (!gROOT->ClassSaved(TCanvas::Class()))
1348 fDrawingPad->SavePrimitive(out, opt);
1349
1350 out << " " << fDrawingPad->GetName() << "->SetWindowSize(";
1351 out << fDrawingPad->GetWw() << "," << fDrawingPad->GetWh() << ");" << endl;
1352 */
1353}
1354
1355// ------------------------------------------------------------------------
1356//
1357// compute the distance of a point (px,py) to the Camera
1358// this functions needed for graphical primitives, that
1359// means without this function you are not able to interact
1360// with the graphical primitive with the mouse!!!
1361//
1362// All calcutations are done in pixel coordinates
1363//
1364Int_t MHCamera::DistancetoPrimitive(Int_t px, Int_t py)
1365{
1366 if (fNcells<=1)
1367 return 999999;
1368
1369 TPaveStats *box = (TPaveStats*)gPad->GetPrimitive("stats");
1370 if (box)
1371 {
1372 const Double_t w = box->GetY2NDC()-box->GetY1NDC();
1373 box->SetX1NDC(gStyle->GetStatX()-gStyle->GetStatW());
1374 box->SetY1NDC(gStyle->GetStatY()-w);
1375 box->SetX2NDC(gStyle->GetStatX());
1376 box->SetY2NDC(gStyle->GetStatY());
1377 }
1378
1379 if (TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
1380 return TH1D::DistancetoPrimitive(px, py);
1381
1382 const Bool_t issame = TString(GetDrawOption()).Contains("same", TString::kIgnoreCase);
1383
1384 const Float_t maxr = (1-fGeomCam->GetConvMm2Deg())*fGeomCam->GetMaxRadius()/2;
1385 const Float_t conv = !issame ||
1386 gPad->GetX1()<-maxr || gPad->GetY1()<-maxr ||
1387 gPad->GetX2()> maxr || gPad->GetY2()>maxr ? 1 : fGeomCam->GetConvMm2Deg();
1388
1389 if (GetPixelIndex(px, py, conv)>=0)
1390 return 0;
1391
1392 if (!box)
1393 return 999999;
1394
1395 const Int_t dist = box->DistancetoPrimitive(px, py);
1396 if (dist > TPad::GetMaxPickDistance())
1397 return 999999;
1398
1399 gPad->SetSelected(box);
1400 return dist;
1401}
1402
1403// ------------------------------------------------------------------------
1404//
1405//
1406Int_t MHCamera::GetPixelIndex(Int_t px, Int_t py, Float_t conv) const
1407{
1408 if (fNcells<=1)
1409 return -1;
1410
1411 Int_t i;
1412 for (i=0; i<fNcells-2; i++)
1413 {
1414 MHexagon hex((*fGeomCam)[i]);
1415 if (hex.DistancetoPrimitive(px, py, conv)>0)
1416 continue;
1417
1418 return i;
1419 }
1420 return -1;
1421}
1422
1423// ------------------------------------------------------------------------
1424//
1425// Returns string containing info about the object at position (px,py).
1426// Returned string will be re-used (lock in MT environment).
1427//
1428char *MHCamera::GetObjectInfo(Int_t px, Int_t py) const
1429{
1430 if (TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
1431 return TH1D::GetObjectInfo(px, py);
1432
1433 static char info[128];
1434
1435 const Int_t idx=GetPixelIndex(px, py);
1436
1437 if (idx<0)
1438 return TObject::GetObjectInfo(px, py);
1439
1440 sprintf(info, "Software Pixel Index: %d (Hardware Id=%d)", idx, idx+1);
1441 return info;
1442}
1443
1444// ------------------------------------------------------------------------
1445//
1446// Add a MCamEvent which should be displayed when the user clicks on a
1447// pixel.
1448// Warning: The object MUST inherit from TObject AND MCamEvent
1449//
1450void MHCamera::AddNotify(TObject *obj)
1451{
1452 // Make sure, that the object derives from MCamEvent!
1453 MCamEvent *evt = dynamic_cast<MCamEvent*>(obj);
1454 if (!evt)
1455 {
1456 gLog << err << "ERROR: MHCamera::AddNotify - TObject doesn't inherit from MCamEvent... ignored." << endl;
1457 return;
1458 }
1459
1460 // Make sure, that it is deleted from the list too, if the obj is deleted
1461 obj->SetBit(kMustCleanup);
1462
1463 // Add object to list
1464 fNotify->Add(obj);
1465}
1466
1467// ------------------------------------------------------------------------
1468//
1469// Execute a mouse event on the camera
1470//
1471void MHCamera::ExecuteEvent(Int_t event, Int_t px, Int_t py)
1472{
1473 if (TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
1474 {
1475 TH1D::ExecuteEvent(event, px, py);
1476 return;
1477 }
1478 //if (event==kMouseMotion && fStatusBar)
1479 // fStatusBar->SetText(GetObjectInfo(px, py), 0);
1480 if (event!=kButton1Down)
1481 return;
1482
1483 const Int_t idx = GetPixelIndex(px, py);
1484 if (idx<0)
1485 return;
1486
1487 gLog << all << GetTitle() << " <" << GetName() << ">" << endl;
1488 gLog << "Software Pixel Idx: " << idx << endl;
1489 gLog << "Hardware Pixel Id: " << idx+1 << endl;
1490 gLog << "Contents: " << GetBinContent(idx+1);
1491 if (GetBinError(idx+1)>0)
1492 gLog << " +/- " << GetBinError(idx+1);
1493 gLog << " <" << (IsUsed(idx)?"on":"off") << ">" << endl;
1494
1495 if (fNotify && fNotify->GetSize()>0)
1496 {
1497 // FIXME: Is there a simpler and more convinient way?
1498
1499 // The name which is created here depends on the instance of
1500 // MHCamera and on the pad on which it is drawn --> The name
1501 // is unique. For ExecuteEvent gPad is always correctly set.
1502 const TString name = Form("%p;%p;PixelContent", this, gPad);
1503
1504 TCanvas *old = (TCanvas*)gROOT->GetListOfCanvases()->FindObject(name);
1505 if (old)
1506 old->cd();
1507 else
1508 new TCanvas(name);
1509
1510 /*
1511 TIter Next(gPad->GetListOfPrimitives());
1512 TObject *o;
1513 while (o=Next()) cout << o << ": " << o->GetName() << " " << o->IsA()->GetName() << endl;
1514 */
1515
1516 // FIXME: Make sure, that the old histograms are really deleted.
1517 // Are they already deleted?
1518
1519 // The dynamic_cast is necessary here: We cannot use ForEach
1520 TIter Next(fNotify);
1521 MCamEvent *evt;
1522 while ((evt=dynamic_cast<MCamEvent*>(Next())))
1523 evt->DrawPixelContent(idx);
1524
1525 gPad->Modified();
1526 gPad->Update();
1527 }
1528}
1529
1530UInt_t MHCamera::GetNumPixels() const
1531{
1532 return fGeomCam->GetNumPixels();
1533}
1534
1535TH1 *MHCamera::DrawCopy() const
1536{
1537 gPad=NULL;
1538 return TH1D::DrawCopy(fName+";cpy");
1539}
1540
1541
1542// --------------------------------------------------------------------------
1543//
1544// Draw a projection of MHCamera onto the y-axis values. Depending on the
1545// variable fit, the following fits are performed:
1546//
1547// 0: No fit, simply draw the projection
1548// 1: Single Gauss (for distributions flat-fielded over the whole camera)
1549// 2: Double Gauss (for distributions different for inner and outer pixels)
1550// 3: Triple Gauss (for distributions with inner, outer pixels and outliers)
1551// 4: flat (for the probability distributions)
1552// (1-4:) Moreover, sectors 6,1 and 2 of the camera and sectors 3,4 and 5 are
1553// drawn separately, for inner and outer pixels.
1554// 5: Fit Inner and Outer pixels separately by a single Gaussian
1555// (only for MAGIC cameras)
1556// 6: Fit Inner and Outer pixels separately by a single Gaussian and display
1557// additionally the two camera halfs separately (for MAGIC camera)
1558//
1559//
1560void MHCamera::DrawProjection(Int_t fit) const
1561{
1562
1563 TArrayI inner(1);
1564 inner[0] = 0;
1565
1566 TArrayI outer(1);
1567 outer[0] = 1;
1568
1569 if (fit==5 || fit==6)
1570 {
1571
1572 if (GetGeomCam().InheritsFrom("MGeomCamMagic"))
1573 {
1574 TArrayI s0(6);
1575 s0[0] = 6;
1576 s0[1] = 1;
1577 s0[2] = 2;
1578 s0[3] = 3;
1579 s0[4] = 4;
1580 s0[5] = 5;
1581
1582 TArrayI s1(3);
1583 s1[0] = 6;
1584 s1[1] = 1;
1585 s1[2] = 2;
1586
1587 TArrayI s2(3);
1588 s2[0] = 3;
1589 s2[1] = 4;
1590 s2[2] = 5;
1591
1592 gPad->Clear();
1593 TVirtualPad *pad = gPad;
1594 pad->Divide(2,1);
1595
1596 TH1D *inout[2];
1597 inout[0] = ProjectionS(s0, inner, "Inner");
1598 inout[1] = ProjectionS(s0, outer, "Outer");
1599
1600 inout[0]->SetDirectory(NULL);
1601 inout[1]->SetDirectory(NULL);
1602
1603 for (int i=0; i<2; i++)
1604 {
1605 pad->cd(i+1);
1606 inout[i]->SetLineColor(kRed+i);
1607 inout[i]->SetBit(kCanDelete);
1608 inout[i]->Draw();
1609 inout[i]->Fit("gaus","Q");
1610
1611 if (fit == 6)
1612 {
1613 TH1D *half[2];
1614 half[0] = ProjectionS(s1, i==0 ? inner : outer , "Sector 6-1-2");
1615 half[1] = ProjectionS(s2, i==0 ? inner : outer , "Sector 3-4-5");
1616
1617 for (int j=0; j<2; j++)
1618 {
1619 half[j]->SetLineColor(kRed+i+2*j);
1620 half[j]->SetDirectory(0);
1621 half[j]->SetBit(kCanDelete);
1622 half[j]->Draw("same");
1623 }
1624 }
1625
1626 }
1627
1628 gLog << all << GetName()
1629 << Form("%s%5.3f%s%3.2f"," Inner Pixels: ",
1630 inout[0]->GetFunction("gaus")->GetParameter(1),"+-",
1631 inout[0]->GetFunction("gaus")->GetParameter(2));
1632 gLog << Form("%s%5.3f%s%3.2f"," Outer Pixels: ",
1633 inout[1]->GetFunction("gaus")->GetParameter(1),"+-",
1634 inout[1]->GetFunction("gaus")->GetParameter(2)) << endl;
1635
1636 }
1637 return;
1638 }
1639
1640 TH1D *obj2 = (TH1D*)Projection(GetName());
1641 obj2->SetDirectory(0);
1642 obj2->Draw();
1643 obj2->SetBit(kCanDelete);
1644
1645 if (fit == 0)
1646 return;
1647
1648 if (GetGeomCam().InheritsFrom("MGeomCamMagic"))
1649 {
1650 TArrayI s0(3);
1651 s0[0] = 6;
1652 s0[1] = 1;
1653 s0[2] = 2;
1654
1655 TArrayI s1(3);
1656 s1[0] = 3;
1657 s1[1] = 4;
1658 s1[2] = 5;
1659
1660
1661 TH1D *halfInOut[4];
1662
1663 // Just to get the right (maximum) binning
1664 halfInOut[0] = ProjectionS(s0, inner, "Sector 6-1-2 Inner");
1665 halfInOut[1] = ProjectionS(s1, inner, "Sector 3-4-5 Inner");
1666 halfInOut[2] = ProjectionS(s0, outer, "Sector 6-1-2 Outer");
1667 halfInOut[3] = ProjectionS(s1, outer, "Sector 3-4-5 Outer");
1668
1669 for (int i=0; i<4; i++)
1670 {
1671 halfInOut[i]->SetLineColor(kRed+i);
1672 halfInOut[i]->SetDirectory(0);
1673 halfInOut[i]->SetBit(kCanDelete);
1674 halfInOut[i]->Draw("same");
1675 }
1676 }
1677
1678 const Double_t min = obj2->GetBinCenter(obj2->GetXaxis()->GetFirst());
1679 const Double_t max = obj2->GetBinCenter(obj2->GetXaxis()->GetLast());
1680 const Double_t integ = obj2->Integral("width")/2.5;
1681 const Double_t mean = obj2->GetMean();
1682 const Double_t rms = obj2->GetRMS();
1683 const Double_t width = max-min;
1684
1685 const TString dgausformula = "([0]-[3])/[2]*exp(-0.5*(x-[1])*(x-[1])/[2]/[2])"
1686 "+[3]/[5]*exp(-0.5*(x-[4])*(x-[4])/[5]/[5])";
1687
1688 const TString tgausformula = "([0]-[3]-[6])/[2]*exp(-0.5*(x-[1])*(x-[1])/[2]/[2])"
1689 "+[3]/[5]*exp(-0.5*(x-[4])*(x-[4])/[5]/[5])"
1690 "+[6]/[8]*exp(-0.5*(x-[7])*(x-[7])/[8]/[8])";
1691 TF1 *f=0;
1692 switch (fit)
1693 {
1694 case 1:
1695 f = new TF1("sgaus", "gaus(0)", min, max);
1696 f->SetLineColor(kYellow);
1697 f->SetBit(kCanDelete);
1698 f->SetParNames("Area", "#mu", "#sigma");
1699 f->SetParameters(integ/rms, mean, rms);
1700 f->SetParLimits(0, 0, integ);
1701 f->SetParLimits(1, min, max);
1702 f->SetParLimits(2, 0, width/1.5);
1703
1704 obj2->Fit(f, "QLR");
1705 break;
1706
1707 case 2:
1708 f = new TF1("dgaus",dgausformula.Data(),min,max);
1709 f->SetLineColor(kYellow);
1710 f->SetBit(kCanDelete);
1711 f->SetParNames("A_{tot}", "#mu1", "#sigma1", "A2", "#mu2", "#sigma2");
1712 f->SetParameters(integ,(min+mean)/2.,width/4.,
1713 integ/width/2.,(max+mean)/2.,width/4.);
1714 // The left-sided Gauss
1715 f->SetParLimits(0,integ-1.5 , integ+1.5);
1716 f->SetParLimits(1,min+(width/10.), mean);
1717 f->SetParLimits(2,0 , width/2.);
1718 // The right-sided Gauss
1719 f->SetParLimits(3,0 , integ);
1720 f->SetParLimits(4,mean, max-(width/10.));
1721 f->SetParLimits(5,0 , width/2.);
1722 obj2->Fit(f,"QLRM");
1723 break;
1724
1725 case 3:
1726 f = new TF1("tgaus",tgausformula.Data(),min,max);
1727 f->SetLineColor(kYellow);
1728 f->SetBit(kCanDelete);
1729 f->SetParNames("A_{tot}","#mu_{1}","#sigma_{1}",
1730 "A_{2}","#mu_{2}","#sigma_{2}",
1731 "A_{3}","#mu_{3}","#sigma_{3}");
1732 f->SetParameters(integ,(min+mean)/2,width/4.,
1733 integ/width/3.,(max+mean)/2.,width/4.,
1734 integ/width/3.,mean,width/2.);
1735 // The left-sided Gauss
1736 f->SetParLimits(0,integ-1.5,integ+1.5);
1737 f->SetParLimits(1,min+(width/10.),mean);
1738 f->SetParLimits(2,width/15.,width/2.);
1739 // The right-sided Gauss
1740 f->SetParLimits(3,0.,integ);
1741 f->SetParLimits(4,mean,max-(width/10.));
1742 f->SetParLimits(5,width/15.,width/2.);
1743 // The Gauss describing the outliers
1744 f->SetParLimits(6,0.,integ);
1745 f->SetParLimits(7,min,max);
1746 f->SetParLimits(8,width/4.,width/1.5);
1747 obj2->Fit(f,"QLRM");
1748 break;
1749
1750 case 4:
1751 obj2->Fit("pol0", "Q");
1752 obj2->GetFunction("pol0")->SetLineColor(kYellow);
1753 break;
1754
1755 case 9:
1756 break;
1757
1758 default:
1759 obj2->Fit("gaus", "Q");
1760 obj2->GetFunction("gaus")->SetLineColor(kYellow);
1761 break;
1762 }
1763}
1764
1765
1766// --------------------------------------------------------------------------
1767//
1768// Draw a projection of MHCamera vs. the radius from the central pixel.
1769//
1770// The inner and outer pixels are drawn separately, both fitted by a polynomial
1771// of grade 1.
1772//
1773void MHCamera::DrawRadialProfile() const
1774{
1775
1776 TProfile *obj2 = (TProfile*)RadialProfile(GetName());
1777 obj2->SetDirectory(0);
1778 obj2->Draw();
1779 obj2->SetBit(kCanDelete);
1780
1781 if (GetGeomCam().InheritsFrom("MGeomCamMagic"))
1782 {
1783
1784 TArrayI s0(6);
1785 s0[0] = 1;
1786 s0[1] = 2;
1787 s0[2] = 3;
1788 s0[3] = 4;
1789 s0[4] = 5;
1790 s0[5] = 6;
1791
1792 TArrayI inner(1);
1793 inner[0] = 0;
1794
1795 TArrayI outer(1);
1796 outer[0] = 1;
1797
1798 // Just to get the right (maximum) binning
1799 TProfile *half[2];
1800 half[0] = RadialProfileS(s0, inner,Form("%s%s",GetName(),"Inner"));
1801 half[1] = RadialProfileS(s0, outer,Form("%s%s",GetName(),"Outer"));
1802
1803 for (Int_t i=0; i<2; i++)
1804 {
1805 Double_t min = GetGeomCam().GetMinRadius(i);
1806 Double_t max = GetGeomCam().GetMaxRadius(i);
1807
1808 half[i]->SetLineColor(kRed+i);
1809 half[i]->SetDirectory(0);
1810 half[i]->SetBit(kCanDelete);
1811 half[i]->Draw("same");
1812 half[i]->Fit("pol1","Q","",min,max);
1813 half[i]->GetFunction("pol1")->SetLineColor(kRed+i);
1814 half[i]->GetFunction("pol1")->SetLineWidth(1);
1815 }
1816 }
1817}
1818
Note: See TracBrowser for help on using the repository browser.