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

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