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

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