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

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