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

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