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

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