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

Last change on this file since 2812 was 2812, checked in by gaug, 21 years ago
*** empty log message ***
File size: 35.0 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!
21! Copyright: MAGIC Software Development, 2000-2003
22!
23!
24\* ======================================================================== */
25
26/////////////////////////////////////////////////////////////////////////////
27//
28// MHCamera
29//
30// Camera Display, based on a TH1D. Pleas be carefull using the
31// underlaying TH1D.
32//
33// To change the scale to a logarithmic scale SetLogy() of the Pad.
34//
35// Be carefull: Entries in this context means Entries/bin or Events
36//
37////////////////////////////////////////////////////////////////////////////
38#include "MHCamera.h"
39
40#include <fstream>
41#include <iostream>
42
43#include <TBox.h>
44#include <TArrow.h>
45#include <TLatex.h>
46#include <TStyle.h>
47#include <TCanvas.h>
48#include <TArrayF.h>
49#include <TRandom.h>
50#include <TPaveText.h>
51#include <TPaveStats.h>
52#include <TClonesArray.h>
53#include <THistPainter.h>
54#include <THLimitsFinder.h>
55
56#include "MLog.h"
57#include "MLogManip.h"
58
59#include "MH.h"
60#include "MHexagon.h"
61
62#include "MGeomPix.h"
63#include "MGeomCam.h"
64
65#include "MCamEvent.h"
66
67
68#define kItemsLegend 48 // see SetPalette(1,0)
69
70ClassImp(MHCamera);
71
72using namespace std;
73
74void MHCamera::Init()
75{
76 UseCurrentStyle();
77
78 SetDirectory(NULL);
79
80 SetLineColor(kGreen);
81 SetMarkerStyle(kFullDotMedium);
82 SetXTitle("Pixel Index");
83
84 fNotify = new TList;
85
86#if ROOT_VERSION_CODE < ROOT_VERSION(3,01,06)
87 SetPalette(1, 0);
88#else
89 SetInvDeepBlueSeaPalette();
90#endif
91}
92
93// ------------------------------------------------------------------------
94//
95// Default Constructor. To be used by the root system ONLY.
96//
97MHCamera::MHCamera() : TH1D(), fGeomCam(NULL), fColors(kItemsLegend)
98{
99 Init();
100}
101
102// ------------------------------------------------------------------------
103//
104// Constructor. Makes a clone of MGeomCam. Removed the TH1D from the
105// current directory. Calls Sumw2(). Set the histogram line color
106// (for error bars) to Green and the marker style to kFullDotMedium.
107//
108MHCamera::MHCamera(const MGeomCam &geom, const char *name, const char *title)
109: fGeomCam(NULL), /*TH1D(name, title, geom.GetNumPixels(), -0.5, geom.GetNumPixels()-0.5),
110 fUsed(geom.GetNumPixels()),*/ fColors(kItemsLegend), fYProj(NULL)
111{
112 //fGeomCam = (MGeomCam*)geom.Clone();
113 SetGeometry(geom, name, title);
114 Init();
115
116 //
117 // root 3.02
118 // * base/inc/TObject.h:
119 // register BIT(8) as kNoContextMenu. If an object has this bit set it will
120 // not get an automatic context menu when clicked with the right mouse button.
121}
122
123void MHCamera::SetGeometry(const MGeomCam &geom, const char *name, const char *title)
124{
125 SetNameTitle(name, title);
126
127 TAxis &x = *GetXaxis();
128
129 SetBins(geom.GetNumPixels(), 0, 1);
130 x.Set(geom.GetNumPixels(), -0.5, geom.GetNumPixels()-0.5);
131
132 //SetBins(geom.GetNumPixels(), -0.5, geom.GetNumPixels()-0.5);
133 //Rebuild();
134
135 Sumw2(); // necessary?
136
137 if (fGeomCam)
138 delete fGeomCam;
139 fGeomCam = (MGeomCam*)geom.Clone();
140
141 fUsed.Set(geom.GetNumPixels());
142 for (Int_t i=0; i<fNcells-2; i++)
143 ResetUsed(i);
144
145}
146
147// ------------------------------------------------------------------------
148//
149// Destructor. Deletes the cloned fGeomCam and the notification list.
150//
151MHCamera::~MHCamera()
152{
153 if (fGeomCam)
154 delete fGeomCam;
155 if (fNotify)
156 delete fNotify;
157 if (fYProj)
158 delete fYProj;
159
160}
161
162// ------------------------------------------------------------------------
163//
164// Taken from TH1D::Fill(). Uses the argument directly as bin index.
165// Doesn't increment the number of entries.
166//
167// -*-*-*-*-*-*-*-*Increment bin with abscissa X by 1*-*-*-*-*-*-*-*-*-*-*
168// ==================================
169//
170// if x is less than the low-edge of the first bin, the Underflow bin is incremented
171// if x is greater than the upper edge of last bin, the Overflow bin is incremented
172//
173// If the storage of the sum of squares of weights has been triggered,
174// via the function Sumw2, then the sum of the squares of weights is incremented
175// by 1 in the bin corresponding to x.
176//
177// -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
178Int_t MHCamera::Fill(Axis_t x)
179{
180
181#if ROOT_VERSION_CODE > ROOT_VERSION(3,05,00)
182 if (fBuffer) return BufferFill(x,1);
183#endif
184 const Int_t bin = (Int_t)x+1;
185 AddBinContent(bin);
186 if (fSumw2.fN)
187 fSumw2.fArray[bin]++;
188
189 if (bin<=0 || bin>fNcells-2)
190 return -1;
191
192 fTsumw++;
193 fTsumw2++;
194 fTsumwx += x;
195 fTsumwx2 += x*x;
196 return bin;
197}
198
199// ------------------------------------------------------------------------
200//
201// Taken from TH1D::Fill(). Uses the argument directly as bin index.
202// Doesn't increment the number of entries.
203//
204// -*-*-*-*-*-*Increment bin with abscissa X with a weight w*-*-*-*-*-*-*-*
205// =============================================
206//
207// if x is less than the low-edge of the first bin, the Underflow bin is incremented
208// if x is greater than the upper edge of last bin, the Overflow bin is incremented
209//
210// If the storage of the sum of squares of weights has been triggered,
211// via the function Sumw2, then the sum of the squares of weights is incremented
212// by w^2 in the bin corresponding to x.
213//
214// -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
215Int_t MHCamera::Fill(Axis_t x, Stat_t w)
216{
217#if ROOT_VERSION_CODE > ROOT_VERSION(3,05,00)
218 if (fBuffer) return BufferFill(x,w);
219#endif
220 const Int_t bin = (Int_t)x+1;
221 AddBinContent(bin, w);
222 if (fSumw2.fN)
223 fSumw2.fArray[bin] += w*w;
224
225 if (bin<=0 || bin>fNcells-2)
226 return -1;
227
228 const Stat_t z = (w > 0 ? w : -w);
229 fTsumw += z;
230 fTsumw2 += z*z;
231 fTsumwx += z*x;
232 fTsumwx2 += z*x*x;
233 return bin;
234}
235
236// ------------------------------------------------------------------------
237//
238// Use x and y in millimeters
239//
240Int_t MHCamera::Fill(Axis_t x, Axis_t y, Stat_t w)
241{
242 if (fNcells<=1 || IsFreezed())
243 return -1;
244
245 for (Int_t idx=0; idx<fNcells-2; idx++)
246 {
247 MHexagon hex((*fGeomCam)[idx]);
248 if (hex.DistanceToPrimitive(x, y)>0)
249 continue;
250
251 SetUsed(idx);
252 return Fill(idx, w);
253 }
254 return -1;
255}
256
257Stat_t MHCamera::GetMean(Int_t axis) const
258{
259 if (fNcells<=1)
260 return 0;
261
262 Stat_t mean = 0;
263 for (int i=1; i<fNcells-1; i++)
264 mean += fArray[i];
265
266 return Profile(mean/(fNcells-2));
267}
268
269Stat_t MHCamera::GetRMS(Int_t axis) const
270{
271 if (fNcells<=1)
272 return -1;
273
274 const Int_t n = fNcells-2;
275
276 Stat_t sum = 0;
277 Stat_t sq = 0;
278 for (int i=1; i<n+1; i++)
279 {
280 sum += fArray[i];
281 sq += fArray[i]*fArray[i];
282 }
283
284 sum /= n;
285 sq /= n;
286
287 return Profile(TMath::Sqrt(sq-sum*sum));
288}
289
290// ------------------------------------------------------------------------
291//
292// Return the minimum contents of all pixels (if all is set, otherwise
293// only of all 'used' pixels), fMinimum if fMinimum set
294//
295Double_t MHCamera::GetMinimum(Bool_t all) const
296{
297 if (fMinimum != -1111)
298 return fMinimum;
299
300 if (fNcells<=1)
301 return 0;
302
303 Double_t minimum=FLT_MAX;
304
305 if (all)
306 {
307 for (Int_t idx=0; idx<fNcells-2; idx++)
308 if (fArray[idx+1] < minimum)
309 minimum = fArray[idx+1];
310 }
311 else
312 {
313 for (Int_t idx=0; idx<fNcells-2; idx++)
314 if (IsUsed(idx) && fArray[idx+1] < minimum)
315 minimum = fArray[idx+1];
316 }
317 return Profile(minimum);
318}
319
320// ------------------------------------------------------------------------
321//
322// Return the maximum contents of all pixels (if all is set, otherwise
323// only of all 'used' pixels), fMaximum if fMaximum set
324//
325Double_t MHCamera::GetMaximum(Bool_t all) const
326{
327 if (fMaximum!=-1111)
328 return fMaximum;
329
330 if (fNcells<=1)
331 return 1;
332
333 Double_t maximum=-FLT_MAX;
334 if (all)
335 {
336 for (Int_t idx=0; idx<fNcells-2; idx++)
337 if (fArray[idx+1] > maximum)
338 maximum = fArray[idx+1];
339 }
340 else
341 {
342 for (Int_t idx=0; idx<fNcells-2; idx++)
343 if (IsUsed(idx) && fArray[idx+1] > maximum)
344 maximum = fArray[idx+1];
345 }
346 return Profile(maximum);
347}
348
349// ------------------------------------------------------------------------
350//
351// Call this function to draw the camera layout into your canvas.
352// Setup a drawing canvas. Add this object and all child objects
353// (hexagons, etc) to the current pad. If no pad exists a new one is
354// created. (To access the 'real' pad containing the camera you have
355// to do a cd(1) in the current layer.
356//
357// To draw a camera into its own pad do something like:
358//
359// MGeomCamMagic m;
360// MHCamera *d=new MHCamera(m);
361//
362// TCanvas *c = new TCanvas;
363// c->Divide(2,1);
364// c->cd(1);
365//
366// d->FillRandom();
367// d->Draw();
368// d->SetBit(kCanDelete);
369//
370// There are several drawing options:
371// 'hist' Draw as a standard TH1 histogram (value vs. pixel index)
372// 'box' Draw hexagons which size is in respect to its contents
373// 'nocol' Leave the 'boxed' hexagons empty
374// 'pixelindex' Display the pixel index in each pixel
375// 'sectorindex' Display the sector index in each pixel
376// 'content' Display the relative content aligned to GetMaximum() and
377// GeMinimum() ((val-min)/(max-min))
378// 'proj' Display the y-projection of the histogram
379//
380void MHCamera::Draw(Option_t *option)
381{
382 // root 3.02:
383 // gPad->SetFixedAspectRatio()
384 const Color_t col = gPad ? gPad->GetFillColor() : 16;
385 TVirtualPad *pad = gPad ? gPad : MH::MakeDefCanvas("CamDisplay", "Mars Camera Display", 656, 600);
386 pad->SetBorderMode(0);
387 pad->SetFillColor(col);
388
389 //
390 // Create an own pad for the MHCamera-Object which can be
391 // resized in paint to keep the correct aspect ratio
392 //
393 pad->Divide(1, 1, 0, 0, col);
394 pad->cd(1);
395 gPad->SetBorderMode(0);
396
397 TString opt(option);
398 opt.ToLower();
399
400 AppendPad(option);
401
402 //
403 // Do not change gPad. The user should not see, that Draw
404 // changes gPad...
405 //
406 pad->cd();
407}
408
409// ------------------------------------------------------------------------
410//
411// This is TObject::DrawClone but completely ignores
412// gROOT->GetSelectedPad(). tbretz had trouble with this in the past.
413// If this makes trouble please write a bug report.
414//
415TObject *MHCamera::DrawClone(Option_t *option) const
416{
417 // Draw a clone of this object in the current pad
418
419 //TVirtualPad *pad = gROOT->GetSelectedPad();
420 TVirtualPad *padsav = gPad;
421 //if (pad) pad->cd();
422
423 TObject *newobj = Clone();
424
425 if (!newobj)
426 return 0;
427
428 /*
429 if (pad) {
430 if (strlen(option)) pad->GetListOfPrimitives()->Add(newobj,option);
431 else pad->GetListOfPrimitives()->Add(newobj,GetDrawOption());
432 pad->Modified(kTRUE);
433 pad->Update();
434 if (padsav) padsav->cd();
435 return newobj;
436 }
437 */
438
439 TString opt(option);
440 opt.ToLower();
441
442 newobj->Draw(opt.IsNull() ? GetDrawOption() : option);
443
444 if (padsav)
445 padsav->cd();
446
447 return newobj;
448}
449
450// ------------------------------------------------------------------------
451//
452// Creates a TH1D which contains the projection of the contents of the
453// MHCamera onto the y-axis. The maximum and minimum are calculated
454// such that a slighly wider range than (GetMinimum(), GetMaximum()) is
455// displayed using THLimitsFinder::OptimizeLimits.
456//
457// If no name is given the newly allocated histogram is removed from
458// the current directory calling SetDirectory(0)
459//
460// If the standard name "_py" is given "_py" is appended to the name
461// of the MHCamera and the corresponding histogram is searched using
462// gROOT->FindObject and updated with the present projection.
463//
464// It is the responsibility of the user to make sure, that the newly
465// created histogram is freed correctly.
466//
467// Currently the new histogram is restrictred to 50 bins.
468// Maybe a optimal number can be calulated from the number of
469// bins on the x-axis of the MHCamera?
470//
471// The code was taken mainly from TH2::ProjectX such the interface
472// is more or less the same than to TH2-projections.
473//
474TH1D *MHCamera::Projection(const char *name) const
475{
476
477 Int_t nbins = 50;
478
479 // Create the projection histogram
480 TString pname(name);
481 if (name=="_py")
482 pname.Prepend(GetName());
483
484 TH1D *h1=0;
485
486 //check if histogram with identical name exist
487 TObject *h1obj = gROOT->FindObject(pname);
488 if (h1obj && h1obj->InheritsFrom("TH1D")) {
489 h1 = (TH1D*)h1obj;
490 h1->Reset();
491 }
492
493 if (!h1)
494 {
495 Double_t min = GetMinimum();
496 Double_t max = GetMaximum();
497
498 Int_t newbins=0;
499
500 THLimitsFinder::OptimizeLimits(nbins, newbins, min, max, kFALSE);
501
502 h1 = new TH1D(pname, GetTitle(), nbins, min, max);
503
504 h1->SetXTitle(GetYaxis()->GetTitle());
505 h1->SetYTitle("Counts");
506 h1->Sumw2();
507 if (pname.IsNull())
508 h1->SetDirectory(NULL);
509 }
510
511 // Fill the projected histogram
512 for (Int_t binx =0;binx<=GetNbinsX()+1;binx++) {
513 const Double_t cont = GetBinContent(binx);
514 if (cont!=0)
515 h1->Fill(cont);
516 }
517
518 h1->SetEntries(GetXaxis()->GetNbins());
519
520 return h1;
521}
522
523// ------------------------------------------------------------------------
524//
525// Resizes the current pad so that the camera is displayed in its
526// correct aspect ratio
527//
528void MHCamera::SetRange()
529{
530 const Float_t range = fGeomCam->GetMaxRadius()*1.05;
531
532 //
533 // Maintain aspect ratio
534 //
535 const float ratio = TestBit(kNoLegend) ? 1 : 1.15;
536
537 //
538 // Calculate width and height of the current pad in pixels
539 //
540 Float_t w = gPad->GetWw();
541 Float_t h = gPad->GetWh()*ratio;
542
543 //
544 // This prevents the pad from resizing itself wrongly
545 //
546 if (gPad->GetMother() != gPad)
547 {
548 w *= gPad->GetMother()->GetAbsWNDC();
549 h *= gPad->GetMother()->GetAbsHNDC();
550 }
551
552 //
553 // Set Range (coordinate system) of pad
554 //
555 gPad->Range(-range, -range, (2*ratio-1)*range, range);
556
557 //
558 // Resize Pad to given ratio
559 //
560 if (h<w)
561 gPad->SetPad((1.-h/w)/2, 0, (h/w+1.)/2, 1);
562 else
563 gPad->SetPad(0, (1.-w/h)/2, 1, (w/h+1.)/2);
564}
565
566// ------------------------------------------------------------------------
567//
568// Updates the pixel colors and paints the pixels
569//
570void MHCamera::Update(Bool_t islog, Bool_t isbox, Bool_t iscol)
571{
572 Double_t min = GetMinimum(kFALSE);
573 Double_t max = GetMaximum(kFALSE);
574 if (min==FLT_MAX)
575 {
576 min = 0;
577 max = 1;
578 }
579
580 if (min==max)
581 max += 1;
582
583 UpdateLegend(min, max, islog);
584
585 MHexagon hex;
586 for (Int_t i=0; i<fNcells-2; i++)
587 {
588 if (IsUsed(i) && iscol)
589 {
590 if (TMath::IsNaN(fArray[i+1]))
591 gLog << warn << "MHCamera::Update: " << GetName() << " <" << GetTitle() << "> - Pixel Index #" << i << " contents is NaN (Not a Number)..." << endl;
592
593 hex.SetFillColor(GetColor(GetBinContent(i+1), min, max, islog));
594 }
595 else
596 hex.SetFillColor(10);
597
598 MGeomPix &pix = (*fGeomCam)[i];
599 if (!isbox)
600 hex.PaintHexagon(pix.GetX(), pix.GetY(), pix.GetD());
601 else
602 if (IsUsed(i) && !TMath::IsNaN(fArray[i+1]))
603 {
604 Float_t size = pix.GetD()*(GetBinContent(i+1)-min)/(max-min);
605 if (size>pix.GetD())
606 size=pix.GetD();
607 hex.PaintHexagon(pix.GetX(), pix.GetY(), size);
608 }
609 }
610}
611
612// ------------------------------------------------------------------------
613//
614// Print minimum and maximum
615//
616void MHCamera::Print(Option_t *) const
617{
618 cout << "Minimum: " << GetMinimum();
619 if (fMinimum==-1111)
620 cout << " <autoscaled>";
621 cout << endl;
622 cout << "Maximum: " << GetMaximum();
623 if (fMaximum==-1111)
624 cout << " <autoscaled>";
625 cout << endl;
626}
627
628// ------------------------------------------------------------------------
629//
630// Paint the y-axis title
631//
632void MHCamera::PaintAxisTitle()
633{
634 const Float_t range = fGeomCam->GetMaxRadius()*1.05;
635 const Float_t w = (1 + 1.5/sqrt((float)(fNcells-2)))*range;
636
637 TLatex *ptitle = new TLatex(w, -.90*range, GetYaxis()->GetTitle());
638
639 ptitle->SetTextSize(0.05);
640 ptitle->SetTextAlign(21);
641
642 // box with the histogram title
643 ptitle->SetTextColor(gStyle->GetTitleTextColor());
644#if ROOT_VERSION_CODE > ROOT_VERSION(3,05,01)
645 ptitle->SetTextFont(gStyle->GetTitleFont(""));
646#endif
647 ptitle->Paint();
648}
649
650// ------------------------------------------------------------------------
651//
652// Paints the camera.
653//
654void MHCamera::Paint(Option_t *o)
655{
656 if (fNcells<=1)
657 return;
658
659 TString opt(o);
660 opt.ToLower();
661
662 if (opt.Contains("hist"))
663 {
664 opt.ReplaceAll("hist", "");
665 TH1D::Paint(opt);
666 return;
667 }
668
669 if (opt.Contains("proj"))
670 {
671 opt.ReplaceAll("proj", "");
672 Projection(GetName())->Paint(opt);
673 return;
674 }
675
676 gPad->Clear();
677
678 // Maintain aspect ratio
679 SetRange();
680
681 Bool_t isbox = opt.Contains("box");
682 Bool_t iscol = isbox ? !opt.Contains("nocol") : 1;
683
684 GetPainter();
685 if (fPainter)
686 {
687 if (!TestBit(TH1::kNoStats))
688 {
689 fPainter->SetHistogram(this);
690 fPainter->PaintStat(gStyle->GetOptStat(), NULL);
691 }
692
693 // Paint primitives (pixels, color legend, photons, ...)
694 if (fPainter->InheritsFrom(THistPainter::Class()))
695 ((THistPainter*)fPainter)->PaintTitle();
696 }
697
698 // Update Contents of the pixels and paint legend
699 Update(gPad->GetLogy(), isbox, iscol);
700 PaintAxisTitle();
701
702 if (opt.Contains("pixelindex"))
703 PaintIndices(0);
704 if (opt.Contains("sectorindex"))
705 PaintIndices(1);
706 if (opt.Contains("content"))
707 PaintIndices(2);
708}
709
710// ------------------------------------------------------------------------
711//
712// With this function you can change the color palette. For more
713// information see TStyle::SetPalette. Only palettes with 50 colors
714// are allowed.
715// In addition you can use SetPalette(52, 0) to create an inverse
716// deep blue sea palette
717//
718void MHCamera::SetPalette(Int_t ncolors, Int_t *colors)
719{
720 //
721 // If not enough colors are specified skip this.
722 //
723 if (ncolors>1 && ncolors<50)
724 {
725 cout << "MHCamera::SetPalette: Only default palettes with 50 colors are allowed... ignored." << endl;
726 return;
727 }
728
729 //
730 // If ncolors==52 create a reversed deep blue sea palette
731 //
732 if (ncolors==52)
733 {
734 gStyle->SetPalette(51, NULL);
735 TArrayI c(kItemsLegend);
736 for (int i=0; i<kItemsLegend; i++)
737 c[kItemsLegend-i-1] = gStyle->GetColorPalette(i);
738 gStyle->SetPalette(kItemsLegend, c.GetArray());
739 }
740 else
741 gStyle->SetPalette(ncolors, colors);
742
743 fColors.Set(kItemsLegend);
744 for (int i=0; i<kItemsLegend; i++)
745 fColors[i] = gStyle->GetColorPalette(i);
746}
747
748
749void MHCamera::SetPrettyPalette()
750{
751 if (!TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
752 SetPalette(1, 0);
753}
754
755void MHCamera::SetDeepBlueSeaPalette()
756{
757 if (!TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
758 SetPalette(51, 0);
759}
760
761void MHCamera::SetInvDeepBlueSeaPalette()
762{
763 if (!TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
764 SetPalette(52, 0);
765}
766
767void MHCamera::PaintIndices(Int_t type)
768{
769 if (fNcells<=1)
770 return;
771
772 const Double_t min = GetMinimum();
773 const Double_t max = GetMaximum();
774
775 if (type==2 && max==min)
776 return;
777
778 TText txt;
779 txt.SetTextFont(122);
780 txt.SetTextAlign(22); // centered/centered
781
782 for (Int_t i=0; i<fNcells-2; i++)
783 {
784 const MGeomPix &h = (*fGeomCam)[i];
785
786 TString num;
787 switch (type)
788 {
789 case 0: num += i; break;
790 case 1: num += h.GetSector(); break;
791 case 2: num += (Int_t)((fArray[i+1]-min)/(max-min)); break;
792 }
793
794 // FIXME: Should depend on the color of the pixel...
795 //(GetColor(GetBinContent(i+1), min, max, 0));
796 txt.SetTextColor(kRed);
797 txt.SetTextSize(0.3*h.GetD()/fGeomCam->GetMaxRadius()/1.05);
798 txt.PaintText(h.GetX(), h.GetY(), num);
799 }
800}
801
802// ------------------------------------------------------------------------
803//
804// Call this function to add a MCamEvent on top of the present contents.
805//
806void MHCamera::AddCamContent(const MCamEvent &event, Int_t type)
807{
808 if (fNcells<=1 || IsFreezed())
809 return;
810
811 // FIXME: Security check missing!
812 for (Int_t idx=0; idx<fNcells-2; idx++)
813 {
814 Double_t val=0;
815 if (event.GetPixelContent(val, idx, *fGeomCam, type)/* && !IsUsed(idx)*/)
816 SetUsed(idx);
817
818 Fill(idx, val); // FIXME: Slow!
819 }
820 fEntries++;
821}
822
823// ------------------------------------------------------------------------
824//
825// Call this function to add a MCamEvent on top of the present contents.
826//
827void MHCamera::SetCamError(const MCamEvent &evt, Int_t type)
828{
829
830 if (fNcells<=1 || IsFreezed())
831 return;
832
833 // FIXME: Security check missing!
834 for (Int_t idx=0; idx<fNcells-2; idx++)
835 {
836 Double_t val=0;
837 if (evt.GetPixelContent(val, idx, *fGeomCam, type)/* && !IsUsed(idx)*/)
838 SetUsed(idx);
839
840 SetBinError(idx+1, val); // FIXME: Slow!
841 }
842}
843
844Stat_t MHCamera::GetBinError(Int_t bin) const
845{
846 Double_t rc = 0;
847 if (TestBit(kVariance) && GetEntries()>0) // error on the mean
848 {
849 const Double_t error = fSumw2.fArray[bin]/GetEntries();
850 const Double_t val = fArray[bin]/GetEntries();
851 rc = TMath::Sqrt(error - val*val);
852 }
853 else
854 rc = TH1D::GetBinError(bin);
855
856 return Profile(rc);
857}
858
859// ------------------------------------------------------------------------
860//
861// Call this function to add a MHCamera on top of the present contents.
862// Type:
863// 0) bin content
864// 1) errors
865// 2) rel. errors
866//
867void MHCamera::AddCamContent(const MHCamera &d, Int_t type)
868{
869 if (fNcells!=d.fNcells || IsFreezed())
870 return;
871
872 // FIXME: Security check missing!
873 for (Int_t idx=0; idx<fNcells-2; idx++)
874 if (d.IsUsed(idx))
875 SetUsed(idx);
876
877 switch (type)
878 {
879 case 1:
880 for (Int_t idx=0; idx<fNcells-2; idx++)
881 Fill(idx, d.GetBinError(idx+1));
882 break;
883 case 2:
884 for (Int_t idx=0; idx<fNcells-2; idx++)
885 if (d.GetBinContent(idx+1)!=0)
886 Fill(idx, TMath::Abs(d.GetBinError(idx+1)/d.GetBinContent(idx+1)));
887 break;
888 default:
889 for (Int_t idx=0; idx<fNcells-2; idx++)
890 Fill(idx, d.GetBinContent(idx+1));
891 break;
892 }
893 fEntries++;
894}
895
896// ------------------------------------------------------------------------
897//
898// Call this function to add a TArrayD on top of the present contents.
899//
900void MHCamera::AddCamContent(const TArrayD &event, const TArrayC *used)
901{
902 if (event.GetSize()!=fNcells-2 || IsFreezed())
903 return;
904
905 if (used && used->GetSize()!=fNcells-2)
906 return;
907
908 for (Int_t idx=0; idx<fNcells-2; idx++)
909 {
910 Fill(idx, const_cast<TArrayD&>(event)[idx]); // FIXME: Slow!
911
912 if (!used || (*const_cast<TArrayC*>(used))[idx])
913 SetUsed(idx);
914 }
915 fEntries++;
916}
917
918// ------------------------------------------------------------------------
919//
920// Call this function to add a MCamEvent on top of the present contents.
921// 1 is added to each pixel if the contents of MCamEvent>threshold
922//
923void MHCamera::CntCamContent(const MCamEvent &event, Double_t threshold, Int_t type)
924{
925 if (fNcells<=1 || IsFreezed())
926 return;
927
928 // FIXME: Security check missing!
929 for (Int_t idx=0; idx<fNcells-2; idx++)
930 {
931 Double_t val=threshold;
932 if (event.GetPixelContent(val, idx, *fGeomCam, type)/* && !IsUsed(idx)*/)
933 SetUsed(idx);
934
935 if (val>threshold)
936 Fill(idx);
937 }
938 fEntries++;
939}
940
941// ------------------------------------------------------------------------
942//
943// Call this function to add a TArrayD on top of the present contents.
944// 1 is added to each pixel if the contents of MCamEvent>threshold
945//
946void MHCamera::CntCamContent(const TArrayD &event, Double_t threshold, Bool_t ispos)
947{
948 if (event.GetSize()!=fNcells-2 || IsFreezed())
949 return;
950
951 for (Int_t idx=0; idx<fNcells-2; idx++)
952 {
953 if (const_cast<TArrayD&>(event)[idx]>threshold)
954 Fill(idx);
955
956 if (!ispos || fArray[idx+1]>0)
957 SetUsed(idx);
958 }
959 fEntries++;
960}
961
962// ------------------------------------------------------------------------
963//
964// Fill the pixels with random contents.
965//
966void MHCamera::FillRandom()
967{
968 if (fNcells<=1 || IsFreezed())
969 return;
970
971 Reset();
972
973 // FIXME: Security check missing!
974 for (Int_t idx=0; idx<fNcells-2; idx++)
975 {
976 Fill(idx, gRandom->Uniform()*fGeomCam->GetPixRatio(idx));
977 SetUsed(idx);
978 }
979 fEntries=1;
980}
981
982
983// ------------------------------------------------------------------------
984//
985// The array must be in increasing order, eg: 2.5, 3.7, 4.9
986// The values in each bin are replaced by the interval in which the value
987// fits. In the example we have four intervals
988// (<2.5, 2.5-3.7, 3.7-4.9, >4.9). Maximum and minimum are set
989// accordingly.
990//
991void MHCamera::SetLevels(const TArrayF &arr)
992{
993 if (fNcells<=1)
994 return;
995
996 for (Int_t i=0; i<fNcells-2; i++)
997 {
998 if (!IsUsed(i))
999 continue;
1000
1001 Int_t j = arr.GetSize();
1002 while (j && fArray[i+1]<arr[j-1])
1003 j--;
1004
1005 fArray[i+1] = j;
1006 }
1007 SetMaximum(arr.GetSize());
1008 SetMinimum(0);
1009}
1010
1011// ------------------------------------------------------------------------
1012//
1013// Reset the all pixel colors to a default value
1014//
1015void MHCamera::Reset(Option_t *opt)
1016{
1017 if (fNcells<=1 || IsFreezed())
1018 return;
1019
1020 TH1::Reset(opt);
1021
1022 for (Int_t i=0; i<fNcells-2; i++)
1023 {
1024 fArray[i+1]=0;
1025 ResetUsed(i);
1026 }
1027 fArray[0] = 0;
1028 fArray[fNcells-1] = 0;
1029}
1030
1031// ------------------------------------------------------------------------
1032//
1033// Here we calculate the color index for the current value.
1034// The color index is defined with the class TStyle and the
1035// Color palette inside. We use the command gStyle->SetPalette(1,0)
1036// for the display. So we have to convert the value "wert" into
1037// a color index that fits the color palette.
1038// The range of the color palette is defined by the values fMinPhe
1039// and fMaxRange. Between this values we have 50 color index, starting
1040// with 0 up to 49.
1041//
1042Int_t MHCamera::GetColor(Float_t val, Float_t min, Float_t max, Bool_t islog)
1043{
1044 if (TMath::IsNaN(val)) // FIXME: gLog!
1045 return 10;
1046
1047 //
1048 // first treat the over- and under-flows
1049 //
1050 const Int_t maxcolidx = kItemsLegend-1;
1051
1052 if (val >= max)
1053 return fColors[maxcolidx];
1054
1055 if (val <= min)
1056 return fColors[0];
1057
1058 //
1059 // calculate the color index
1060 //
1061 Float_t ratio;
1062 if (islog && min>0)
1063 ratio = log10(val/min) / log10(max/min);
1064 else
1065 ratio = (val-min) / (max-min);
1066
1067 const Int_t colidx = (Int_t)(ratio*maxcolidx + .5);
1068 return fColors[colidx];
1069}
1070
1071TPaveStats *MHCamera::GetStatisticBox()
1072{
1073 TObject *obj = 0;
1074
1075 TIter Next(fFunctions);
1076 while ((obj = Next()))
1077 if (obj->InheritsFrom(TPaveStats::Class()))
1078 return static_cast<TPaveStats*>(obj);
1079
1080 return NULL;
1081}
1082
1083// ------------------------------------------------------------------------
1084//
1085// Change the text on the legend according to the range of the Display
1086//
1087void MHCamera::UpdateLegend(Float_t min, Float_t max, Bool_t islog)
1088{
1089 const Float_t range = fGeomCam->GetMaxRadius()*1.05;
1090
1091 TArrow arr;
1092 arr.PaintArrow(-range*.9, -range*.9, -range*.6, -range*.9, 0.025);
1093 arr.PaintArrow(-range*.9, -range*.9, -range*.9, -range*.6, 0.025);
1094
1095 TString text;
1096 text += (int)(range*.3);
1097 text += "mm";
1098
1099 TText newtxt2;
1100 newtxt2.SetTextSize(0.04);
1101 newtxt2.PaintText(-range*.85, -range*.85, text);
1102
1103 text = "";
1104 text += (float)((int)(range*.3*fGeomCam->GetConvMm2Deg()*10))/10;
1105 text += "\\circ";
1106 text = text.Strip(TString::kLeading);
1107
1108 TLatex latex;
1109 latex.PaintLatex(-range*.85, -range*.75, 0, 0.04, text);
1110
1111 if (TestBit(kNoLegend))
1112 return;
1113
1114 TPaveStats *stats = GetStatisticBox();
1115
1116 const Float_t hndc = 0.92 - (stats ? stats->GetY1NDC() : 1);
1117 const Float_t H = (0.75-hndc)*range;
1118 const Float_t offset = hndc*range;
1119
1120 const Float_t h = 2./kItemsLegend;
1121 const Float_t w = range/sqrt((float)(fNcells-2));
1122
1123 TBox newbox;
1124 TText newtxt;
1125 newtxt.SetTextSize(0.03);
1126 newtxt.SetTextAlign(12);
1127#if ROOT_VERSION_CODE > ROOT_VERSION(3,01,06)
1128 newtxt.SetBit(/*kNoContextMenu|*/kCannotPick);
1129 newbox.SetBit(/*kNoContextMenu|*/kCannotPick);
1130#endif
1131
1132 const Float_t step = (islog && min>0 ? log10(max/min) : max-min) / kItemsLegend;
1133 const Int_t firsts = step*3 < 1e-8 ? 8 : (Int_t)floor(log10(step*3));
1134 const TString opt = Form("%%.%if", firsts>0 ? 0 : TMath::Abs(firsts));
1135
1136 for (Int_t i=0; i<kItemsLegend+1; i+=3)
1137 {
1138 Float_t val;
1139 if (islog && min>0)
1140 val = pow(10, step*i) * min;
1141 else
1142 val = min + step*i;
1143
1144 //const bool dispexp = max-min>1.5 && fabs(val)>0.1 && fabs(val)<1e6;
1145 newtxt.PaintText(range+1.5*w, H*(i*h-1)-offset, Form(opt, val));
1146 }
1147
1148 for (Int_t i=0; i<kItemsLegend; i++)
1149 {
1150 newbox.SetFillColor(fColors[i]);
1151 newbox.PaintBox(range, H*(i*h-1)-offset, range+w, H*((i+1)*h-1)-offset);
1152 }
1153}
1154
1155// ------------------------------------------------------------------------
1156//
1157// Save primitive as a C++ statement(s) on output stream out
1158//
1159void MHCamera::SavePrimitive(ofstream &out, Option_t *opt)
1160{
1161 cout << "MHCamera::SavePrimitive: Must be rewritten!" << endl;
1162 /*
1163 if (!gROOT->ClassSaved(TCanvas::Class()))
1164 fDrawingPad->SavePrimitive(out, opt);
1165
1166 out << " " << fDrawingPad->GetName() << "->SetWindowSize(";
1167 out << fDrawingPad->GetWw() << "," << fDrawingPad->GetWh() << ");" << endl;
1168 */
1169}
1170
1171// ------------------------------------------------------------------------
1172//
1173// compute the distance of a point (px,py) to the Camera
1174// this functions needed for graphical primitives, that
1175// means without this function you are not able to interact
1176// with the graphical primitive with the mouse!!!
1177//
1178// All calcutations are done in pixel coordinates
1179//
1180Int_t MHCamera::DistancetoPrimitive(Int_t px, Int_t py)
1181{
1182 if (fNcells<=1)
1183 return 999999;
1184
1185 const Int_t kMaxDiff = 7;
1186
1187 TPaveStats *box = (TPaveStats*)gPad->GetPrimitive("stats");
1188 if (box)
1189 {
1190 const Double_t w = box->GetY2NDC()-box->GetY1NDC();
1191 box->SetX1NDC(gStyle->GetStatX()-gStyle->GetStatW());
1192 box->SetY1NDC(gStyle->GetStatY()-w);
1193 box->SetX2NDC(gStyle->GetStatX());
1194 box->SetY2NDC(gStyle->GetStatY());
1195 }
1196
1197 if (TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
1198 return TH1D::DistancetoPrimitive(px, py);
1199
1200 for (Int_t i=0; i<fNcells-2; i++)
1201 {
1202 MHexagon hex((*fGeomCam)[i]);
1203 if (hex.DistancetoPrimitive(px, py)==0)
1204 return 0;
1205 }
1206
1207 if (!box)
1208 return 999999;
1209
1210 const Int_t dist = box->DistancetoPrimitive(px, py);
1211 if (dist > kMaxDiff)
1212 return 999999;
1213
1214 gPad->SetSelected(box);
1215 return dist;
1216}
1217
1218// ------------------------------------------------------------------------
1219//
1220// Function introduced (31-01-03) WILL BE REMOVED IN THE FUTURE! DON'T
1221// USE IT!
1222//
1223void MHCamera::SetPix(const Int_t idx, const Int_t color, Float_t min, Float_t max)
1224{
1225 fArray[idx+1] = color;
1226 SetUsed(idx);
1227}
1228
1229// ------------------------------------------------------------------------
1230//
1231//
1232Int_t MHCamera::GetPixelIndex(Int_t px, Int_t py) const
1233{
1234 if (fNcells<=1)
1235 return -1;
1236
1237 Int_t i;
1238 for (i=0; i<fNcells-2; i++)
1239 {
1240 MHexagon hex((*fGeomCam)[i]);
1241 if (hex.DistancetoPrimitive(px, py)>0)
1242 continue;
1243
1244 return i;
1245 }
1246 return -1;
1247}
1248
1249// ------------------------------------------------------------------------
1250//
1251// Returns string containing info about the object at position (px,py).
1252// Returned string will be re-used (lock in MT environment).
1253//
1254char *MHCamera::GetObjectInfo(Int_t px, Int_t py) const
1255{
1256 if (TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
1257 return TH1D::GetObjectInfo(px, py);
1258
1259 static char info[128];
1260
1261 const Int_t idx=GetPixelIndex(px, py);
1262
1263 if (idx<0)
1264 return TObject::GetObjectInfo(px, py);
1265
1266 sprintf(info, "Software Pixel Index: %d (Hardware Id=%d)", idx, idx+1);
1267 return info;
1268}
1269
1270// ------------------------------------------------------------------------
1271//
1272// Execute a mouse event on the camera
1273//
1274void MHCamera::ExecuteEvent(Int_t event, Int_t px, Int_t py)
1275{
1276 if (TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
1277 {
1278 TH1D::ExecuteEvent(event, px, py);
1279 return;
1280 }
1281 //if (event==kMouseMotion && fStatusBar)
1282 // fStatusBar->SetText(GetObjectInfo(px, py), 0);
1283 if (event!=kButton1Down)
1284 return;
1285
1286 const Int_t idx = GetPixelIndex(px, py);
1287 if (idx<0)
1288 return;
1289
1290 cout << GetTitle() << " <" << GetName() << ">" << endl;
1291 cout << "Software Pixel Index: " << idx << endl;
1292 cout << "Hardware Pixel Id: " << idx+1 << endl;
1293 cout << "Contents: " << GetBinContent(idx+1);
1294 if (GetBinError(idx+1)>0)
1295 cout << " +/- " << GetBinError(idx+1);
1296 cout << " <" << (IsUsed(idx)?"on":"off") << ">" << endl;
1297
1298 if (fNotify && fNotify->GetSize()>0)
1299 {
1300 // FIXME: Is there a simpler and more convinient way?
1301
1302 // The name which is created here depends on the instance of
1303 // MHCamera and on the pad on which it is drawn --> The name
1304 // is unique. For ExecuteEvent gPad is always correctly set.
1305 const TString name = Form("%p;%p;PixelContent", this, gPad);
1306
1307 TCanvas *old = (TCanvas*)gROOT->GetListOfCanvases()->FindObject(name);
1308 if (old)
1309 old->cd();
1310 else
1311 new TCanvas(name);
1312
1313 /*
1314 TIter Next(gPad->GetListOfPrimitives());
1315 TObject *o;
1316 while (o=Next()) cout << o << ": " << o->GetName() << " " << o->IsA()->GetName() << endl;
1317 */
1318
1319 // FIXME: Make sure, that the old histograms are really deleted.
1320 // Are they already deleted?
1321 fNotify->ForEach(MCamEvent, DrawPixelContent)(idx);
1322 gPad->Modified();
1323 gPad->Update();
1324 }
1325}
1326
1327UInt_t MHCamera::GetNumPixels() const
1328{
1329 return fGeomCam->GetNumPixels();
1330}
1331
1332TH1 *MHCamera::DrawCopy() const
1333{
1334 gPad=NULL;
1335 return TH1D::DrawCopy(fName+";cpy");
1336}
Note: See TracBrowser for help on using the repository browser.