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

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