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

Last change on this file since 6130 was 5807, 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 = val*val>error ? 0 : TMath::Sqrt(error - val*val);
1162 }
1163 else
1164 rc = TH1D::GetBinError(bin);
1165
1166 return Profile(rc);
1167}
1168
1169// ------------------------------------------------------------------------
1170//
1171// Call this function to add a MHCamera on top of the present contents.
1172// Type:
1173// 0) bin content
1174// 1) errors
1175// 2) rel. errors
1176//
1177void MHCamera::AddCamContent(const MHCamera &d, Int_t type)
1178{
1179 if (fNcells!=d.fNcells || IsFreezed())
1180 return;
1181
1182 // FIXME: Security check missing!
1183 for (Int_t idx=0; idx<fNcells-2; idx++)
1184 if (d.IsUsed(idx))
1185 SetUsed(idx);
1186
1187 switch (type)
1188 {
1189 case 1:
1190 for (Int_t idx=0; idx<fNcells-2; idx++)
1191 Fill(idx, d.GetBinError(idx+1));
1192 break;
1193 case 2:
1194 for (Int_t idx=0; idx<fNcells-2; idx++)
1195 if (d.GetBinContent(idx+1)!=0)
1196 Fill(idx, TMath::Abs(d.GetBinError(idx+1)/d.GetBinContent(idx+1)));
1197 break;
1198 default:
1199 for (Int_t idx=0; idx<fNcells-2; idx++)
1200 Fill(idx, d.GetBinContent(idx+1));
1201 break;
1202 }
1203 fEntries++;
1204}
1205
1206// ------------------------------------------------------------------------
1207//
1208// Call this function to add a TArrayD on top of the present contents.
1209//
1210void MHCamera::AddCamContent(const TArrayD &event, const TArrayC *used)
1211{
1212 if (event.GetSize()!=fNcells-2 || IsFreezed())
1213 return;
1214
1215 if (used && used->GetSize()!=fNcells-2)
1216 return;
1217
1218 for (Int_t idx=0; idx<fNcells-2; idx++)
1219 {
1220 Fill(idx, event[idx]); // FIXME: Slow!
1221
1222 if (!used || (*used)[idx])
1223 SetUsed(idx);
1224 }
1225 fEntries++;
1226}
1227
1228// ------------------------------------------------------------------------
1229//
1230// Call this function to add a MArrayD on top of the present contents.
1231//
1232void MHCamera::AddCamContent(const MArrayD &event, const TArrayC *used)
1233{
1234 if (event.GetSize()!=(UInt_t)(fNcells-2) || IsFreezed())
1235 return;
1236
1237 if (used && used->GetSize()!=fNcells-2)
1238 return;
1239
1240 for (Int_t idx=0; idx<fNcells-2; idx++)
1241 {
1242 Fill(idx, event[idx]); // FIXME: Slow!
1243
1244 if (!used || (*used)[idx])
1245 SetUsed(idx);
1246 }
1247 fEntries++;
1248}
1249
1250// ------------------------------------------------------------------------
1251//
1252// Call this function to add a MCamEvent on top of the present contents.
1253// 1 is added to each pixel if the contents of MCamEvent>threshold (in case isabove is set to kTRUE == default)
1254// 1 is added to each pixel if the contents of MCamEvent<threshold (in case isabove is set to kFALSE)
1255//
1256// in unused pixel is not counted if it didn't fullfill the condition.
1257//
1258void MHCamera::CntCamContent(const MCamEvent &event, Double_t threshold, Int_t type, Bool_t isabove)
1259{
1260 if (fNcells<=1 || IsFreezed())
1261 return;
1262
1263 // FIXME: Security check missing!
1264 for (Int_t idx=0; idx<fNcells-2; idx++)
1265 {
1266 Double_t val=threshold;
1267 if (event.GetPixelContent(val, idx, *fGeomCam, type)/* && !IsUsed(idx)*/)
1268 {
1269 SetUsed(idx);
1270
1271 if (isabove && val>threshold)
1272 Fill(idx);
1273 if (!isabove && val<threshold)
1274 Fill(idx);
1275 }
1276 }
1277 fEntries++;
1278}
1279
1280// ------------------------------------------------------------------------
1281//
1282// Call this function to add a MCamEvent on top of the present contents.
1283// 1 is added to each pixel if the contents of MCamEvent>threshold (in case isabove is set to kTRUE == default)
1284// 1 is added to each pixel if the contents of MCamEvent<threshold (in case isabove is set to kFALSE)
1285//
1286// in unused pixel is not counted if it didn't fullfill the condition.
1287//
1288void MHCamera::CntCamContent(const MCamEvent &event, TArrayD threshold, Int_t type, Bool_t isabove)
1289{
1290 if (fNcells<=1 || IsFreezed())
1291 return;
1292
1293 // FIXME: Security check missing!
1294 for (Int_t idx=0; idx<fNcells-2; idx++)
1295 {
1296 Double_t val=threshold[idx];
1297 if (event.GetPixelContent(val, idx, *fGeomCam, type)/* && !IsUsed(idx)*/)
1298 {
1299 SetUsed(idx);
1300
1301 if (val>threshold[idx] && isabove)
1302 Fill(idx);
1303 if (val<threshold[idx] && !isabove)
1304 Fill(idx);
1305 }
1306 }
1307 fEntries++;
1308}
1309
1310// ------------------------------------------------------------------------
1311//
1312// Call this function to add a TArrayD on top of the present contents.
1313// 1 is added to each pixel if the contents of MCamEvent>threshold
1314//
1315void MHCamera::CntCamContent(const TArrayD &event, Double_t threshold, Bool_t ispos)
1316{
1317 if (event.GetSize()!=fNcells-2 || IsFreezed())
1318 return;
1319
1320 for (Int_t idx=0; idx<fNcells-2; idx++)
1321 {
1322 if (event[idx]>threshold)
1323 Fill(idx);
1324
1325 if (!ispos || fArray[idx+1]>0)
1326 SetUsed(idx);
1327 }
1328 fEntries++;
1329}
1330
1331// ------------------------------------------------------------------------
1332//
1333// Fill the pixels with random contents.
1334//
1335void MHCamera::FillRandom()
1336{
1337 if (fNcells<=1 || IsFreezed())
1338 return;
1339
1340 Reset();
1341
1342 // FIXME: Security check missing!
1343 for (Int_t idx=0; idx<fNcells-2; idx++)
1344 {
1345 Fill(idx, gRandom->Uniform()*fGeomCam->GetPixRatio(idx));
1346 SetUsed(idx);
1347 }
1348 fEntries=1;
1349}
1350
1351
1352// ------------------------------------------------------------------------
1353//
1354// The array must be in increasing order, eg: 2.5, 3.7, 4.9
1355// The values in each bin are replaced by the interval in which the value
1356// fits. In the example we have four intervals
1357// (<2.5, 2.5-3.7, 3.7-4.9, >4.9). Maximum and minimum are set
1358// accordingly.
1359//
1360void MHCamera::SetLevels(const TArrayF &arr)
1361{
1362 if (fNcells<=1)
1363 return;
1364
1365 for (Int_t i=0; i<fNcells-2; i++)
1366 {
1367 if (!IsUsed(i))
1368 continue;
1369
1370 Int_t j = arr.GetSize();
1371 while (j && fArray[i+1]<arr[j-1])
1372 j--;
1373
1374 fArray[i+1] = j;
1375 }
1376 SetMaximum(arr.GetSize());
1377 SetMinimum(0);
1378}
1379
1380// ------------------------------------------------------------------------
1381//
1382// Reset the all pixel colors to a default value
1383//
1384void MHCamera::Reset(Option_t *opt)
1385{
1386 if (fNcells<=1 || IsFreezed())
1387 return;
1388
1389 TH1::Reset(opt);
1390
1391 for (Int_t i=0; i<fNcells-2; i++)
1392 {
1393 fArray[i+1]=0;
1394 ResetUsed(i);
1395 }
1396 fArray[0] = 0;
1397 fArray[fNcells-1] = 0;
1398}
1399
1400// ------------------------------------------------------------------------
1401//
1402// Here we calculate the color index for the current value.
1403// The color index is defined with the class TStyle and the
1404// Color palette inside. We use the command gStyle->SetPalette(1,0)
1405// for the display. So we have to convert the value "wert" into
1406// a color index that fits the color palette.
1407// The range of the color palette is defined by the values fMinPhe
1408// and fMaxRange. Between this values we have 50 color index, starting
1409// with 0 up to 49.
1410//
1411Int_t MHCamera::GetColor(Float_t val, Float_t min, Float_t max, Bool_t islog)
1412{
1413 if (TMath::IsNaN(val)) // FIXME: gLog!
1414 return 10;
1415
1416 //
1417 // first treat the over- and under-flows
1418 //
1419 const Int_t maxcolidx = kItemsLegend-1;
1420
1421 if (val >= max)
1422 return fColors[maxcolidx];
1423
1424 if (val <= min)
1425 return fColors[0];
1426
1427 //
1428 // calculate the color index
1429 //
1430 Float_t ratio;
1431 if (islog && min>0)
1432 ratio = log10(val/min) / log10(max/min);
1433 else
1434 ratio = (val-min) / (max-min);
1435
1436 const Int_t colidx = (Int_t)(ratio*maxcolidx + .5);
1437 return fColors[colidx];
1438}
1439
1440TPaveStats *MHCamera::GetStatisticBox()
1441{
1442 TObject *obj = 0;
1443
1444 TIter Next(fFunctions);
1445 while ((obj = Next()))
1446 if (obj->InheritsFrom(TPaveStats::Class()))
1447 return static_cast<TPaveStats*>(obj);
1448
1449 return NULL;
1450}
1451
1452// ------------------------------------------------------------------------
1453//
1454// Change the text on the legend according to the range of the Display
1455//
1456void MHCamera::UpdateLegend(Float_t min, Float_t max, Bool_t islog)
1457{
1458 const Float_t range = fGeomCam->GetMaxRadius()*1.05;
1459
1460 TArrow arr;
1461 arr.PaintArrow(-range*.9, -range*.9, -range*.6, -range*.9, 0.025);
1462 arr.PaintArrow(-range*.9, -range*.9, -range*.9, -range*.6, 0.025);
1463
1464 TString text;
1465 text += (int)(range*.3);
1466 text += "mm";
1467
1468 TText newtxt2;
1469 newtxt2.SetTextSize(0.04);
1470 newtxt2.PaintText(-range*.85, -range*.85, text);
1471
1472 text = "";
1473 text += Form("%.2f", (float)((int)(range*.3*fGeomCam->GetConvMm2Deg()*10))/10);
1474 text += "\\circ";
1475 text = text.Strip(TString::kLeading);
1476
1477 TLatex latex;
1478 latex.PaintLatex(-range*.85, -range*.75, 0, 0.04, text);
1479
1480 if (TestBit(kNoLegend))
1481 return;
1482
1483 TPaveStats *stats = GetStatisticBox();
1484
1485 const Float_t hndc = 0.92 - (stats ? stats->GetY1NDC() : 1);
1486 const Float_t H = (0.75-hndc)*range;
1487 const Float_t offset = hndc*range;
1488
1489 const Float_t h = 2./kItemsLegend;
1490 const Float_t w = range/sqrt((float)(fNcells-2));
1491
1492 TBox newbox;
1493 TText newtxt;
1494 newtxt.SetTextSize(0.03);
1495 newtxt.SetTextAlign(12);
1496#if ROOT_VERSION_CODE > ROOT_VERSION(3,01,06)
1497 newtxt.SetBit(/*kNoContextMenu|*/kCannotPick);
1498 newbox.SetBit(/*kNoContextMenu|*/kCannotPick);
1499#endif
1500
1501 const Float_t step = (islog && min>0 ? log10(max/min) : max-min) / kItemsLegend;
1502 const Int_t firsts = step*3 < 1e-8 ? 8 : (Int_t)floor(log10(step*3));
1503 const TString opt = Form("%%.%if", firsts>0 ? 0 : TMath::Abs(firsts));
1504
1505 for (Int_t i=0; i<kItemsLegend+1; i+=3)
1506 {
1507 Float_t val;
1508 if (islog && min>0)
1509 val = pow(10, step*i) * min;
1510 else
1511 val = min + step*i;
1512
1513 //const bool dispexp = max-min>1.5 && fabs(val)>0.1 && fabs(val)<1e6;
1514 newtxt.PaintText(range+1.5*w, H*(i*h-1)-offset, Form(opt, val));
1515 }
1516
1517 for (Int_t i=0; i<kItemsLegend; i++)
1518 {
1519 newbox.SetFillColor(fColors[i]);
1520 newbox.PaintBox(range, H*(i*h-1)-offset, range+w, H*((i+1)*h-1)-offset);
1521 }
1522}
1523
1524// ------------------------------------------------------------------------
1525//
1526// Save primitive as a C++ statement(s) on output stream out
1527//
1528void MHCamera::SavePrimitive(ofstream &out, Option_t *opt)
1529{
1530 gLog << err << "MHCamera::SavePrimitive: Must be rewritten!" << endl;
1531 /*
1532 if (!gROOT->ClassSaved(TCanvas::Class()))
1533 fDrawingPad->SavePrimitive(out, opt);
1534
1535 out << " " << fDrawingPad->GetName() << "->SetWindowSize(";
1536 out << fDrawingPad->GetWw() << "," << fDrawingPad->GetWh() << ");" << endl;
1537 */
1538}
1539
1540// ------------------------------------------------------------------------
1541//
1542// compute the distance of a point (px,py) to the Camera
1543// this functions needed for graphical primitives, that
1544// means without this function you are not able to interact
1545// with the graphical primitive with the mouse!!!
1546//
1547// All calcutations are done in pixel coordinates
1548//
1549Int_t MHCamera::DistancetoPrimitive(Int_t px, Int_t py)
1550{
1551 if (fNcells<=1)
1552 return 999999;
1553
1554 TPaveStats *box = (TPaveStats*)gPad->GetPrimitive("stats");
1555 if (box)
1556 {
1557 const Double_t w = box->GetY2NDC()-box->GetY1NDC();
1558 box->SetX1NDC(gStyle->GetStatX()-gStyle->GetStatW());
1559 box->SetY1NDC(gStyle->GetStatY()-w);
1560 box->SetX2NDC(gStyle->GetStatX());
1561 box->SetY2NDC(gStyle->GetStatY());
1562 }
1563
1564 if (TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
1565 return TH1D::DistancetoPrimitive(px, py);
1566
1567 const Bool_t issame = TString(GetDrawOption()).Contains("same", TString::kIgnoreCase);
1568
1569 const Float_t maxr = (1-fGeomCam->GetConvMm2Deg())*fGeomCam->GetMaxRadius()/2;
1570 const Float_t conv = !issame ||
1571 gPad->GetX1()<-maxr || gPad->GetY1()<-maxr ||
1572 gPad->GetX2()> maxr || gPad->GetY2()>maxr ? 1 : fGeomCam->GetConvMm2Deg();
1573
1574 if (GetPixelIndex(px, py, conv)>=0)
1575 return 0;
1576
1577 if (!box)
1578 return 999999;
1579
1580 const Int_t dist = box->DistancetoPrimitive(px, py);
1581 if (dist > TPad::GetMaxPickDistance())
1582 return 999999;
1583
1584 gPad->SetSelected(box);
1585 return dist;
1586}
1587
1588// ------------------------------------------------------------------------
1589//
1590//
1591Int_t MHCamera::GetPixelIndex(Int_t px, Int_t py, Float_t conv) const
1592{
1593 if (fNcells<=1)
1594 return -1;
1595
1596 Int_t i;
1597 for (i=0; i<fNcells-2; i++)
1598 {
1599 MHexagon hex((*fGeomCam)[i]);
1600 if (hex.DistancetoPrimitive(px, py, conv)>0)
1601 continue;
1602
1603 return i;
1604 }
1605 return -1;
1606}
1607
1608// ------------------------------------------------------------------------
1609//
1610// Returns string containing info about the object at position (px,py).
1611// Returned string will be re-used (lock in MT environment).
1612//
1613char *MHCamera::GetObjectInfo(Int_t px, Int_t py) const
1614{
1615 if (TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
1616 return TH1D::GetObjectInfo(px, py);
1617
1618 static char info[128];
1619
1620 const Int_t idx=GetPixelIndex(px, py);
1621
1622 if (idx<0)
1623 return TObject::GetObjectInfo(px, py);
1624
1625 sprintf(info, "Software Pixel Idx: %d (Hardware Id=%d) c=%.1f <%s>",
1626 idx, idx+1, GetBinContent(idx+1), IsUsed(idx)?"on":"off");
1627 return info;
1628}
1629
1630// ------------------------------------------------------------------------
1631//
1632// Add a MCamEvent which should be displayed when the user clicks on a
1633// pixel.
1634// Warning: The object MUST inherit from TObject AND MCamEvent
1635//
1636void MHCamera::AddNotify(TObject *obj)
1637{
1638 // Make sure, that the object derives from MCamEvent!
1639 MCamEvent *evt = dynamic_cast<MCamEvent*>(obj);
1640 if (!evt)
1641 {
1642 gLog << err << "ERROR: MHCamera::AddNotify - TObject doesn't inherit from MCamEvent... ignored." << endl;
1643 return;
1644 }
1645
1646 // Make sure, that it is deleted from the list too, if the obj is deleted
1647 obj->SetBit(kMustCleanup);
1648
1649 // Add object to list
1650 fNotify->Add(obj);
1651}
1652
1653// ------------------------------------------------------------------------
1654//
1655// Execute a mouse event on the camera
1656//
1657void MHCamera::ExecuteEvent(Int_t event, Int_t px, Int_t py)
1658{
1659 if (TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
1660 {
1661 TH1D::ExecuteEvent(event, px, py);
1662 return;
1663 }
1664 //if (event==kMouseMotion && fStatusBar)
1665 // fStatusBar->SetText(GetObjectInfo(px, py), 0);
1666 if (event!=kButton1Down)
1667 return;
1668
1669 const Int_t idx = GetPixelIndex(px, py);
1670 if (idx<0)
1671 return;
1672
1673 gLog << all << GetTitle() << " <" << GetName() << ">" << endl;
1674 gLog << "Software Pixel Idx: " << idx << endl;
1675 gLog << "Hardware Pixel Id: " << idx+1 << endl;
1676 gLog << "Contents: " << GetBinContent(idx+1);
1677 if (GetBinError(idx+1)>0)
1678 gLog << " +/- " << GetBinError(idx+1);
1679 gLog << " <" << (IsUsed(idx)?"on":"off") << ">" << endl;
1680
1681 if (fNotify && fNotify->GetSize()>0)
1682 {
1683 // FIXME: Is there a simpler and more convinient way?
1684
1685 // The name which is created here depends on the instance of
1686 // MHCamera and on the pad on which it is drawn --> The name
1687 // is unique. For ExecuteEvent gPad is always correctly set.
1688 const TString name = Form("%p;%p;PixelContent", this, gPad);
1689
1690 TCanvas *old = (TCanvas*)gROOT->GetListOfCanvases()->FindObject(name);
1691 if (old)
1692 old->cd();
1693 else
1694 new TCanvas(name);
1695
1696 /*
1697 TIter Next(gPad->GetListOfPrimitives());
1698 TObject *o;
1699 while (o=Next()) cout << o << ": " << o->GetName() << " " << o->IsA()->GetName() << endl;
1700 */
1701
1702 // FIXME: Make sure, that the old histograms are really deleted.
1703 // Are they already deleted?
1704
1705 // The dynamic_cast is necessary here: We cannot use ForEach
1706 TIter Next(fNotify);
1707 MCamEvent *evt;
1708 while ((evt=dynamic_cast<MCamEvent*>(Next())))
1709 evt->DrawPixelContent(idx);
1710
1711 gPad->Modified();
1712 gPad->Update();
1713 }
1714}
1715
1716UInt_t MHCamera::GetNumPixels() const
1717{
1718 return fGeomCam->GetNumPixels();
1719}
1720
1721TH1 *MHCamera::DrawCopy() const
1722{
1723 gPad=NULL;
1724 return TH1D::DrawCopy(fName+";cpy");
1725}
1726
1727// --------------------------------------------------------------------------
1728//
1729// Draw a projection of MHCamera onto the y-axis values. Depending on the
1730// variable fit, the following fits are performed:
1731//
1732// 0: No fit, simply draw the projection
1733// 1: Single Gauss (for distributions flat-fielded over the whole camera)
1734// 2: Double Gauss (for distributions different for inner and outer pixels)
1735// 3: Triple Gauss (for distributions with inner, outer pixels and outliers)
1736// 4: flat (for the probability distributions)
1737// (1-4:) Moreover, sectors 6,1 and 2 of the camera and sectors 3,4 and 5 are
1738// drawn separately, for inner and outer pixels.
1739// 5: Fit Inner and Outer pixels separately by a single Gaussian
1740// (only for MAGIC cameras)
1741// 6: Fit Inner and Outer pixels separately by a single Gaussian and display
1742// additionally the two camera halfs separately (for MAGIC camera)
1743// 7: Single Gauss with TLegend to show the meaning of the colours
1744//
1745void MHCamera::DrawProjection(Int_t fit) const
1746{
1747 TArrayI inner(1);
1748 inner[0] = 0;
1749
1750 TArrayI outer(1);
1751 outer[0] = 1;
1752
1753 if (fit==5 || fit==6)
1754 {
1755 if (GetGeomCam().InheritsFrom("MGeomCamMagic"))
1756 {
1757 TArrayI s0(6);
1758 s0[0] = 6;
1759 s0[1] = 1;
1760 s0[2] = 2;
1761 s0[3] = 3;
1762 s0[4] = 4;
1763 s0[5] = 5;
1764
1765 TArrayI s1(3);
1766 s1[0] = 6;
1767 s1[1] = 1;
1768 s1[2] = 2;
1769
1770 TArrayI s2(3);
1771 s2[0] = 3;
1772 s2[1] = 4;
1773 s2[2] = 5;
1774
1775 gPad->Clear();
1776 TVirtualPad *pad = gPad;
1777 pad->Divide(2,1);
1778
1779 TH1D *inout[2];
1780 inout[0] = ProjectionS(s0, inner, "Inner");
1781 inout[1] = ProjectionS(s0, outer, "Outer");
1782
1783 inout[0]->SetDirectory(NULL);
1784 inout[1]->SetDirectory(NULL);
1785
1786 for (int i=0; i<2; i++)
1787 {
1788 pad->cd(i+1);
1789 gPad->SetBorderMode(0);
1790
1791 inout[i]->SetLineColor(kRed+i);
1792 inout[i]->SetBit(kCanDelete);
1793 inout[i]->Draw();
1794 inout[i]->Fit("gaus","Q");
1795
1796 if (fit == 6)
1797 {
1798 TH1D *half[2];
1799 half[0] = ProjectionS(s1, i==0 ? inner : outer , "Sector 6-1-2");
1800 half[1] = ProjectionS(s2, i==0 ? inner : outer , "Sector 3-4-5");
1801
1802 for (int j=0; j<2; j++)
1803 {
1804 half[j]->SetLineColor(kRed+i+2*j+1);
1805 half[j]->SetDirectory(NULL);
1806 half[j]->SetBit(kCanDelete);
1807 half[j]->Draw("same");
1808 }
1809 }
1810
1811 }
1812 }
1813 return;
1814 }
1815
1816 TH1D *obj2 = (TH1D*)Projection(GetName());
1817 obj2->SetDirectory(0);
1818 obj2->Draw();
1819 obj2->SetBit(kCanDelete);
1820
1821 if (fit == 0)
1822 return;
1823
1824 if (GetGeomCam().InheritsFrom("MGeomCamMagic"))
1825 {
1826 TArrayI s0(3);
1827 s0[0] = 6;
1828 s0[1] = 1;
1829 s0[2] = 2;
1830
1831 TArrayI s1(3);
1832 s1[0] = 3;
1833 s1[1] = 4;
1834 s1[2] = 5;
1835
1836 TH1D *halfInOut[4];
1837
1838 // Just to get the right (maximum) binning
1839 halfInOut[0] = ProjectionS(s0, inner, "Sector 6-1-2 Inner");
1840 halfInOut[1] = ProjectionS(s1, inner, "Sector 3-4-5 Inner");
1841 halfInOut[2] = ProjectionS(s0, outer, "Sector 6-1-2 Outer");
1842 halfInOut[3] = ProjectionS(s1, outer, "Sector 3-4-5 Outer");
1843
1844 TLegend *leg = new TLegend(0.05,0.65,0.35,0.9);
1845
1846 for (int i=0; i<4; i++)
1847 {
1848 halfInOut[i]->SetLineColor(kRed+i);
1849 halfInOut[i]->SetDirectory(0);
1850 halfInOut[i]->SetBit(kCanDelete);
1851 halfInOut[i]->Draw("same");
1852 leg->AddEntry(halfInOut[i],halfInOut[i]->GetTitle(),"l");
1853 }
1854
1855 if (fit==7)
1856 leg->Draw();
1857
1858 gPad->Modified();
1859 gPad->Update();
1860 }
1861
1862 const Double_t min = obj2->GetBinCenter(obj2->GetXaxis()->GetFirst());
1863 const Double_t max = obj2->GetBinCenter(obj2->GetXaxis()->GetLast());
1864 const Double_t integ = obj2->Integral("width")/2.5;
1865 const Double_t mean = obj2->GetMean();
1866 const Double_t rms = obj2->GetRMS();
1867 const Double_t width = max-min;
1868
1869 const TString dgausformula = "([0]-[3])/[2]*exp(-0.5*(x-[1])*(x-[1])/[2]/[2])"
1870 "+[3]/[5]*exp(-0.5*(x-[4])*(x-[4])/[5]/[5])";
1871
1872 const TString tgausformula = "([0]-[3]-[6])/[2]*exp(-0.5*(x-[1])*(x-[1])/[2]/[2])"
1873 "+[3]/[5]*exp(-0.5*(x-[4])*(x-[4])/[5]/[5])"
1874 "+[6]/[8]*exp(-0.5*(x-[7])*(x-[7])/[8]/[8])";
1875
1876 TF1 *f=0;
1877 switch (fit)
1878 {
1879 case 1:
1880 f = new TF1("sgaus", "gaus(0)", min, max);
1881 f->SetLineColor(kYellow);
1882 f->SetBit(kCanDelete);
1883 f->SetParNames("Area", "#mu", "#sigma");
1884 f->SetParameters(integ/rms, mean, rms);
1885 f->SetParLimits(0, 0, integ);
1886 f->SetParLimits(1, min, max);
1887 f->SetParLimits(2, 0, width/1.5);
1888
1889 obj2->Fit(f, "QLR");
1890 break;
1891
1892 case 2:
1893 f = new TF1("dgaus",dgausformula.Data(),min,max);
1894 f->SetLineColor(kYellow);
1895 f->SetBit(kCanDelete);
1896 f->SetParNames("A_{tot}", "#mu1", "#sigma1", "A2", "#mu2", "#sigma2");
1897 f->SetParameters(integ,(min+mean)/2.,width/4.,
1898 integ/width/2.,(max+mean)/2.,width/4.);
1899 // The left-sided Gauss
1900 f->SetParLimits(0,integ-1.5 , integ+1.5);
1901 f->SetParLimits(1,min+(width/10.), mean);
1902 f->SetParLimits(2,0 , width/2.);
1903 // The right-sided Gauss
1904 f->SetParLimits(3,0 , integ);
1905 f->SetParLimits(4,mean, max-(width/10.));
1906 f->SetParLimits(5,0 , width/2.);
1907 obj2->Fit(f,"QLRM");
1908 break;
1909
1910 case 3:
1911 f = new TF1("tgaus",tgausformula.Data(),min,max);
1912 f->SetLineColor(kYellow);
1913 f->SetBit(kCanDelete);
1914 f->SetParNames("A_{tot}","#mu_{1}","#sigma_{1}",
1915 "A_{2}","#mu_{2}","#sigma_{2}",
1916 "A_{3}","#mu_{3}","#sigma_{3}");
1917 f->SetParameters(integ,(min+mean)/2,width/4.,
1918 integ/width/3.,(max+mean)/2.,width/4.,
1919 integ/width/3.,mean,width/2.);
1920 // The left-sided Gauss
1921 f->SetParLimits(0,integ-1.5,integ+1.5);
1922 f->SetParLimits(1,min+(width/10.),mean);
1923 f->SetParLimits(2,width/15.,width/2.);
1924 // The right-sided Gauss
1925 f->SetParLimits(3,0.,integ);
1926 f->SetParLimits(4,mean,max-(width/10.));
1927 f->SetParLimits(5,width/15.,width/2.);
1928 // The Gauss describing the outliers
1929 f->SetParLimits(6,0.,integ);
1930 f->SetParLimits(7,min,max);
1931 f->SetParLimits(8,width/4.,width/1.5);
1932 obj2->Fit(f,"QLRM");
1933 break;
1934
1935 case 4:
1936 obj2->Fit("pol0", "Q");
1937 obj2->GetFunction("pol0")->SetLineColor(kYellow);
1938 break;
1939
1940 case 9:
1941 break;
1942
1943 default:
1944 obj2->Fit("gaus", "Q");
1945 obj2->GetFunction("gaus")->SetLineColor(kYellow);
1946 break;
1947 }
1948}
1949
1950// --------------------------------------------------------------------------
1951//
1952// Draw a projection of MHCamera vs. the radius from the central pixel.
1953//
1954// The inner and outer pixels are drawn separately, both fitted by a polynomial
1955// of grade 1.
1956//
1957void MHCamera::DrawRadialProfile() const
1958{
1959 TProfile *obj2 = (TProfile*)RadialProfile(GetName());
1960 obj2->SetDirectory(0);
1961 obj2->Draw();
1962 obj2->SetBit(kCanDelete);
1963
1964 if (GetGeomCam().InheritsFrom("MGeomCamMagic"))
1965 {
1966 TArrayI s0(6);
1967 s0[0] = 1;
1968 s0[1] = 2;
1969 s0[2] = 3;
1970 s0[3] = 4;
1971 s0[4] = 5;
1972 s0[5] = 6;
1973
1974 TArrayI inner(1);
1975 inner[0] = 0;
1976
1977 TArrayI outer(1);
1978 outer[0] = 1;
1979
1980 // Just to get the right (maximum) binning
1981 TProfile *half[2];
1982 half[0] = RadialProfileS(s0, inner,Form("%sInner",GetName()));
1983 half[1] = RadialProfileS(s0, outer,Form("%sOuter",GetName()));
1984
1985 for (Int_t i=0; i<2; i++)
1986 {
1987 Double_t min = GetGeomCam().GetMinRadius(i);
1988 Double_t max = GetGeomCam().GetMaxRadius(i);
1989
1990 half[i]->SetLineColor(kRed+i);
1991 half[i]->SetDirectory(0);
1992 half[i]->SetBit(kCanDelete);
1993 half[i]->Draw("same");
1994 half[i]->Fit("pol1","Q","",min,max);
1995 half[i]->GetFunction("pol1")->SetLineColor(kRed+i);
1996 half[i]->GetFunction("pol1")->SetLineWidth(1);
1997 }
1998 }
1999}
2000
2001// --------------------------------------------------------------------------
2002//
2003// Draw a projection of MHCamera vs. the azimuth angle inside the camera.
2004//
2005// The inner and outer pixels are drawn separately.
2006// The general azimuth profile is fitted by a straight line
2007//
2008void MHCamera::DrawAzimuthProfile() const
2009{
2010 TProfile *obj2 = (TProfile*)AzimuthProfile(GetName());
2011 obj2->SetDirectory(0);
2012 obj2->Draw();
2013 obj2->SetBit(kCanDelete);
2014 obj2->Fit("pol0","Q","");
2015 obj2->GetFunction("pol0")->SetLineWidth(1);
2016
2017 if (GetGeomCam().InheritsFrom("MGeomCamMagic"))
2018 {
2019 TArrayI inner(1);
2020 inner[0] = 0;
2021
2022 TArrayI outer(1);
2023 outer[0] = 1;
2024
2025 // Just to get the right (maximum) binning
2026 TProfile *half[2];
2027 half[0] = AzimuthProfileA(inner,Form("%sInner",GetName()));
2028 half[1] = AzimuthProfileA(outer,Form("%sOuter",GetName()));
2029
2030 for (Int_t i=0; i<2; i++)
2031 {
2032 half[i]->SetLineColor(kRed+i);
2033 half[i]->SetDirectory(0);
2034 half[i]->SetBit(kCanDelete);
2035 half[i]->SetMarkerSize(0.5);
2036 half[i]->Draw("same");
2037 }
2038 }
2039}
2040
2041// --------------------------------------------------------------------------
2042//
2043// Draw the MHCamera into the MStatusDisplay:
2044//
2045// 1) Draw it as histogram (MHCamera::DrawCopy("hist")
2046// 2) Draw it as a camera, with MHCamera::SetPrettyPalette() set.
2047// 3) If "rad" is not zero, draw its values vs. the radius from the camera center.
2048// (DrawRadialProfile())
2049// 4) Depending on the variable "fit", draw the values projection on the y-axis
2050// (DrawProjection()):
2051// 0: don't draw
2052// 1: Draw fit to Single Gauss (for distributions flat-fielded over the whole camera)
2053// 2: Draw and fit to Double Gauss (for distributions different for inner and outer pixels)
2054// 3: Draw and fit to Triple Gauss (for distributions with inner, outer pixels and outliers)
2055// 4: Draw and fit to Polynomial grade 0: (for the probability distributions)
2056// >4: Draw and don;t fit.
2057//
2058void MHCamera::CamDraw(TCanvas &c, const Int_t x, const Int_t y,
2059 const Int_t fit, const Int_t rad, const Int_t azi,
2060 TObject *notify)
2061{
2062 c.cd(x);
2063 gPad->SetBorderMode(0);
2064 gPad->SetTicks();
2065 MHCamera *obj1=(MHCamera*)DrawCopy("hist");
2066 obj1->SetDirectory(NULL);
2067
2068 if (notify)
2069 obj1->AddNotify(notify);
2070
2071 c.cd(x+y);
2072 gPad->SetBorderMode(0);
2073 obj1->SetPrettyPalette();
2074 obj1->Draw();
2075
2076 Int_t cnt = 2;
2077
2078 if (rad)
2079 {
2080 c.cd(x+2*y);
2081 gPad->SetBorderMode(0);
2082 gPad->SetTicks();
2083 DrawRadialProfile();
2084 cnt++;
2085 }
2086
2087 if (azi)
2088 {
2089 c.cd(x+cnt*y);
2090 gPad->SetBorderMode(0);
2091 gPad->SetTicks();
2092 DrawAzimuthProfile();
2093 cnt++;
2094 }
2095
2096 if (!fit)
2097 return;
2098
2099 c.cd(x + cnt*y);
2100 gPad->SetBorderMode(0);
2101 gPad->SetTicks();
2102 DrawProjection(fit);
2103}
Note: See TracBrowser for help on using the repository browser.