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

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