source: trunk/Mars/mhist/MHCamera.cc@ 9873

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