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

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