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

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