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

Last change on this file since 3444 was 3400, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 37.5 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, const TArrayI &sector, const TArrayI &aidx) const
167{
168 const MGeomPix &pix = (*fGeomCam)[idx];
169 return FindVal(sector, pix.GetSector()) && FindVal(aidx, pix.GetAidx());
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::GetMeanSectors(const TArrayI &sector, const TArrayI &aidx, 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, aidx))
284 {
285 mean += fArray[i+1];
286 n++;
287 }
288 }
289
290 return Profile(mean/n);
291}
292
293// ------------------------------------------------------------------------
294//
295// Return the variance 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::GetRmsSectors(const TArrayI &sector, const TArrayI &aidx, 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, aidx))
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::GetMinimumSectors(const TArrayI &sector, const TArrayI &aidx, 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, aidx) && (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::GetMaximumSectors(const TArrayI &sector, const TArrayI &aidx, 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, aidx) && (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(const TArrayI &sector, const TArrayI &aidx, 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.GetSize()>0)
509 {
510 pname += ";";
511 for (int i=0; i<sector.GetSize(); i++)
512 pname += sector[i];
513 }
514 if (aidx.GetSize()>0)
515 {
516 pname += ";";
517 for (int i=0; i<aidx.GetSize(); i++)
518 pname += aidx[i];
519 }
520 }
521
522 TH1D *h1=0;
523
524 //check if histogram with identical name exist
525 TObject *h1obj = gROOT->FindObject(pname);
526 if (h1obj && h1obj->InheritsFrom("TH1D")) {
527 h1 = (TH1D*)h1obj;
528 h1->Reset();
529 }
530
531 if (!h1)
532 {
533 Double_t min = GetMinimumSectors(sector, aidx);
534 Double_t max = GetMaximumSectors(sector, aidx);
535
536 Int_t newbins=0;
537
538 THLimitsFinder::OptimizeLimits(nbins, newbins, min, max, kFALSE);
539
540 h1 = new TH1D(pname, GetTitle(), nbins, min, max);
541 h1->SetDirectory(pname.IsNull() ? NULL : gROOT);
542 h1->SetXTitle(GetYaxis()->GetTitle());
543 h1->SetYTitle("Counts");
544 //h1->Sumw2();
545 }
546
547 // Fill the projected histogram
548 for (Int_t idx=0; idx<fNcells-2; idx++)
549 if (IsUsed(idx) && MatchSector(idx, sector, aidx))
550 h1->Fill(GetBinContent(idx+1));
551
552 return h1;
553}
554
555// ------------------------------------------------------------------------
556//
557// Resizes the current pad so that the camera is displayed in its
558// correct aspect ratio
559//
560void MHCamera::SetRange()
561{
562 const Float_t range = fGeomCam->GetMaxRadius()*1.05;
563
564 //
565 // Maintain aspect ratio
566 //
567 const float ratio = TestBit(kNoLegend) ? 1 : 1.15;
568
569 //
570 // Calculate width and height of the current pad in pixels
571 //
572 Float_t w = gPad->GetWw();
573 Float_t h = gPad->GetWh()*ratio;
574
575 //
576 // This prevents the pad from resizing itself wrongly
577 //
578 if (gPad->GetMother() != gPad)
579 {
580 w *= gPad->GetMother()->GetAbsWNDC();
581 h *= gPad->GetMother()->GetAbsHNDC();
582 }
583
584 //
585 // Set Range (coordinate system) of pad
586 //
587 gPad->Range(-range, -range, (2*ratio-1)*range, range);
588
589 //
590 // Resize Pad to given ratio
591 //
592 if (h<w)
593 gPad->SetPad((1.-h/w)/2, 0, (h/w+1.)/2, 1);
594 else
595 gPad->SetPad(0, (1.-w/h)/2, 1, (w/h+1.)/2);
596}
597
598// ------------------------------------------------------------------------
599//
600// Updates the pixel colors and paints the pixels
601//
602void MHCamera::Update(Bool_t islog, Bool_t isbox, Bool_t iscol)
603{
604 Double_t min = GetMinimum(kFALSE);
605 Double_t max = GetMaximum(kFALSE);
606 if (min==FLT_MAX)
607 {
608 min = 0;
609 max = 1;
610 }
611
612 if (min==max)
613 max += 1;
614
615 UpdateLegend(min, max, islog);
616
617 MHexagon hex;
618 for (Int_t i=0; i<fNcells-2; i++)
619 {
620 if (IsUsed(i) && iscol)
621 {
622 if (TMath::IsNaN(fArray[i+1]))
623 gLog << warn << "MHCamera::Update: " << GetName() << " <" << GetTitle() << "> - Pixel Index #" << i << " contents is NaN (Not a Number)..." << endl;
624
625 hex.SetFillColor(GetColor(GetBinContent(i+1), min, max, islog));
626 }
627 else
628 hex.SetFillColor(10);
629
630 MGeomPix &pix = (*fGeomCam)[i];
631 if (!isbox)
632 hex.PaintHexagon(pix.GetX(), pix.GetY(), pix.GetD());
633 else
634 if (IsUsed(i) && !TMath::IsNaN(fArray[i+1]))
635 {
636 Float_t size = pix.GetD()*(GetBinContent(i+1)-min)/(max-min);
637 if (size>pix.GetD())
638 size=pix.GetD();
639 hex.PaintHexagon(pix.GetX(), pix.GetY(), size);
640 }
641 }
642}
643
644// ------------------------------------------------------------------------
645//
646// Print minimum and maximum
647//
648void MHCamera::Print(Option_t *) const
649{
650 gLog << all << "Minimum: " << GetMinimum();
651 if (fMinimum==-1111)
652 gLog << " <autoscaled>";
653 gLog << endl;
654 gLog << "Maximum: " << GetMaximum();
655 if (fMaximum==-1111)
656 gLog << " <autoscaled>";
657 gLog << endl;
658}
659
660// ------------------------------------------------------------------------
661//
662// Paint the y-axis title
663//
664void MHCamera::PaintAxisTitle()
665{
666 const Float_t range = fGeomCam->GetMaxRadius()*1.05;
667 const Float_t w = (1 + 1.5/sqrt((float)(fNcells-2)))*range;
668
669 TLatex *ptitle = new TLatex(w, -.90*range, GetYaxis()->GetTitle());
670
671 ptitle->SetTextSize(0.05);
672 ptitle->SetTextAlign(21);
673
674 // box with the histogram title
675 ptitle->SetTextColor(gStyle->GetTitleTextColor());
676#if ROOT_VERSION_CODE > ROOT_VERSION(3,05,01)
677 ptitle->SetTextFont(gStyle->GetTitleFont(""));
678#endif
679 ptitle->Paint();
680}
681
682// ------------------------------------------------------------------------
683//
684// Paints the camera.
685//
686void MHCamera::Paint(Option_t *o)
687{
688 if (fNcells<=1)
689 return;
690
691 TString opt(o);
692 opt.ToLower();
693
694 if (opt.Contains("hist"))
695 {
696 opt.ReplaceAll("hist", "");
697 TH1D::Paint(opt);
698 return;
699 }
700
701 if (opt.Contains("proj"))
702 {
703 opt.ReplaceAll("proj", "");
704 Projection(GetName())->Paint(opt);
705 return;
706 }
707
708 gPad->Clear();
709
710 // Maintain aspect ratio
711 SetRange();
712
713 Bool_t isbox = opt.Contains("box");
714 Bool_t iscol = isbox ? !opt.Contains("nocol") : 1;
715
716 if (GetPainter())
717 {
718 // Paint statistics
719 if (!TestBit(TH1::kNoStats))
720 fPainter->PaintStat(gStyle->GetOptStat(), NULL);
721
722 // Paint primitives (pixels, color legend, photons, ...)
723 if (fPainter->InheritsFrom(THistPainter::Class()))
724 {
725 static_cast<THistPainter*>(fPainter)->MakeChopt("");
726 static_cast<THistPainter*>(fPainter)->PaintTitle();
727 }
728 }
729
730 // Update Contents of the pixels and paint legend
731 Update(gPad->GetLogy(), isbox, iscol);
732 PaintAxisTitle();
733
734 if (opt.Contains("pixelindex"))
735 PaintIndices(0);
736 if (opt.Contains("sectorindex"))
737 PaintIndices(1);
738 if (opt.Contains("content"))
739 PaintIndices(2);
740}
741
742// ------------------------------------------------------------------------
743//
744// With this function you can change the color palette. For more
745// information see TStyle::SetPalette. Only palettes with 50 colors
746// are allowed.
747// In addition you can use SetPalette(52, 0) to create an inverse
748// deep blue sea palette
749//
750void MHCamera::SetPalette(Int_t ncolors, Int_t *colors)
751{
752 //
753 // If not enough colors are specified skip this.
754 //
755 if (ncolors>1 && ncolors<50)
756 {
757 gLog << err << "MHCamera::SetPalette: Only default palettes with 50 colors are allowed... ignored." << endl;
758 return;
759 }
760
761 //
762 // If ncolors==52 create a reversed deep blue sea palette
763 //
764 if (ncolors==52)
765 {
766 gStyle->SetPalette(51, NULL);
767 TArrayI c(kItemsLegend);
768 for (int i=0; i<kItemsLegend; i++)
769 c[kItemsLegend-i-1] = gStyle->GetColorPalette(i);
770 gStyle->SetPalette(kItemsLegend, c.GetArray());
771 }
772 else
773 gStyle->SetPalette(ncolors, colors);
774
775 fColors.Set(kItemsLegend);
776 for (int i=0; i<kItemsLegend; i++)
777 fColors[i] = gStyle->GetColorPalette(i);
778}
779
780
781void MHCamera::SetPrettyPalette()
782{
783 if (!TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
784 SetPalette(1, 0);
785}
786
787void MHCamera::SetDeepBlueSeaPalette()
788{
789 if (!TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
790 SetPalette(51, 0);
791}
792
793void MHCamera::SetInvDeepBlueSeaPalette()
794{
795 if (!TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
796 SetPalette(52, 0);
797}
798
799void MHCamera::PaintIndices(Int_t type)
800{
801 if (fNcells<=1)
802 return;
803
804 const Double_t min = GetMinimum();
805 const Double_t max = GetMaximum();
806
807 if (type==2 && max==min)
808 return;
809
810 TText txt;
811 txt.SetTextFont(122);
812 txt.SetTextAlign(22); // centered/centered
813
814 for (Int_t i=0; i<fNcells-2; i++)
815 {
816 const MGeomPix &h = (*fGeomCam)[i];
817
818 TString num;
819 switch (type)
820 {
821 case 0: num += i; break;
822 case 1: num += h.GetSector(); break;
823 case 2: num += (Int_t)((fArray[i+1]-min)/(max-min)); break;
824 }
825
826 // FIXME: Should depend on the color of the pixel...
827 //(GetColor(GetBinContent(i+1), min, max, 0));
828 txt.SetTextColor(kRed);
829 txt.SetTextSize(0.3*h.GetD()/fGeomCam->GetMaxRadius()/1.05);
830 txt.PaintText(h.GetX(), h.GetY(), num);
831 }
832}
833
834// ------------------------------------------------------------------------
835//
836// Call this function to add a MCamEvent on top of the present contents.
837//
838void MHCamera::AddCamContent(const MCamEvent &event, Int_t type)
839{
840 if (fNcells<=1 || IsFreezed())
841 return;
842
843 // FIXME: Security check missing!
844 for (Int_t idx=0; idx<fNcells-2; idx++)
845 {
846 Double_t val=0;
847 if (event.GetPixelContent(val, idx, *fGeomCam, type)/* && !IsUsed(idx)*/)
848 SetUsed(idx);
849
850 Fill(idx, val); // FIXME: Slow!
851 }
852 fEntries++;
853}
854
855// ------------------------------------------------------------------------
856//
857// Call this function to add a MCamEvent on top of the present contents.
858//
859void MHCamera::SetCamError(const MCamEvent &evt, Int_t type)
860{
861
862 if (fNcells<=1 || IsFreezed())
863 return;
864
865 // FIXME: Security check missing!
866 for (Int_t idx=0; idx<fNcells-2; idx++)
867 {
868 Double_t val=0;
869 if (evt.GetPixelContent(val, idx, *fGeomCam, type)/* && !IsUsed(idx)*/)
870 SetUsed(idx);
871
872 SetBinError(idx+1, val); // FIXME: Slow!
873 }
874}
875
876Stat_t MHCamera::GetBinError(Int_t bin) const
877{
878 Double_t rc = 0;
879 if (TestBit(kVariance) && GetEntries()>0) // error on the mean
880 {
881 const Double_t error = fSumw2.fArray[bin]/GetEntries();
882 const Double_t val = fArray[bin]/GetEntries();
883 rc = TMath::Sqrt(error - val*val);
884 }
885 else
886 {
887 rc = TH1D::GetBinError(bin);
888 rc = Profile(rc);
889 }
890
891 return rc;
892}
893
894// ------------------------------------------------------------------------
895//
896// Call this function to add a MHCamera on top of the present contents.
897// Type:
898// 0) bin content
899// 1) errors
900// 2) rel. errors
901//
902void MHCamera::AddCamContent(const MHCamera &d, Int_t type)
903{
904 if (fNcells!=d.fNcells || IsFreezed())
905 return;
906
907 // FIXME: Security check missing!
908 for (Int_t idx=0; idx<fNcells-2; idx++)
909 if (d.IsUsed(idx))
910 SetUsed(idx);
911
912 switch (type)
913 {
914 case 1:
915 for (Int_t idx=0; idx<fNcells-2; idx++)
916 Fill(idx, d.GetBinError(idx+1));
917 break;
918 case 2:
919 for (Int_t idx=0; idx<fNcells-2; idx++)
920 if (d.GetBinContent(idx+1)!=0)
921 Fill(idx, TMath::Abs(d.GetBinError(idx+1)/d.GetBinContent(idx+1)));
922 break;
923 default:
924 for (Int_t idx=0; idx<fNcells-2; idx++)
925 Fill(idx, d.GetBinContent(idx+1));
926 break;
927 }
928 fEntries++;
929}
930
931// ------------------------------------------------------------------------
932//
933// Call this function to add a TArrayD on top of the present contents.
934//
935void MHCamera::AddCamContent(const TArrayD &event, const TArrayC *used)
936{
937 if (event.GetSize()!=fNcells-2 || IsFreezed())
938 return;
939
940 if (used && used->GetSize()!=fNcells-2)
941 return;
942
943 for (Int_t idx=0; idx<fNcells-2; idx++)
944 {
945 Fill(idx, const_cast<TArrayD&>(event)[idx]); // FIXME: Slow!
946
947 if (!used || (*const_cast<TArrayC*>(used))[idx])
948 SetUsed(idx);
949 }
950 fEntries++;
951}
952
953// ------------------------------------------------------------------------
954//
955// Call this function to add a MCamEvent on top of the present contents.
956// 1 is added to each pixel if the contents of MCamEvent>threshold
957//
958void MHCamera::CntCamContent(const MCamEvent &event, Double_t threshold, Int_t type)
959{
960 if (fNcells<=1 || IsFreezed())
961 return;
962
963 // FIXME: Security check missing!
964 for (Int_t idx=0; idx<fNcells-2; idx++)
965 {
966 Double_t val=threshold;
967 if (event.GetPixelContent(val, idx, *fGeomCam, type)/* && !IsUsed(idx)*/)
968 SetUsed(idx);
969
970 if (val>threshold)
971 Fill(idx);
972 }
973 fEntries++;
974}
975
976// ------------------------------------------------------------------------
977//
978// Call this function to add a TArrayD on top of the present contents.
979// 1 is added to each pixel if the contents of MCamEvent>threshold
980//
981void MHCamera::CntCamContent(const TArrayD &event, Double_t threshold, Bool_t ispos)
982{
983 if (event.GetSize()!=fNcells-2 || IsFreezed())
984 return;
985
986 for (Int_t idx=0; idx<fNcells-2; idx++)
987 {
988 if (const_cast<TArrayD&>(event)[idx]>threshold)
989 Fill(idx);
990
991 if (!ispos || fArray[idx+1]>0)
992 SetUsed(idx);
993 }
994 fEntries++;
995}
996
997// ------------------------------------------------------------------------
998//
999// Fill the pixels with random contents.
1000//
1001void MHCamera::FillRandom()
1002{
1003 if (fNcells<=1 || IsFreezed())
1004 return;
1005
1006 Reset();
1007
1008 // FIXME: Security check missing!
1009 for (Int_t idx=0; idx<fNcells-2; idx++)
1010 {
1011 Fill(idx, gRandom->Uniform()*fGeomCam->GetPixRatio(idx));
1012 SetUsed(idx);
1013 }
1014 fEntries=1;
1015}
1016
1017
1018// ------------------------------------------------------------------------
1019//
1020// The array must be in increasing order, eg: 2.5, 3.7, 4.9
1021// The values in each bin are replaced by the interval in which the value
1022// fits. In the example we have four intervals
1023// (<2.5, 2.5-3.7, 3.7-4.9, >4.9). Maximum and minimum are set
1024// accordingly.
1025//
1026void MHCamera::SetLevels(const TArrayF &arr)
1027{
1028 if (fNcells<=1)
1029 return;
1030
1031 for (Int_t i=0; i<fNcells-2; i++)
1032 {
1033 if (!IsUsed(i))
1034 continue;
1035
1036 Int_t j = arr.GetSize();
1037 while (j && fArray[i+1]<arr[j-1])
1038 j--;
1039
1040 fArray[i+1] = j;
1041 }
1042 SetMaximum(arr.GetSize());
1043 SetMinimum(0);
1044}
1045
1046// ------------------------------------------------------------------------
1047//
1048// Reset the all pixel colors to a default value
1049//
1050void MHCamera::Reset(Option_t *opt)
1051{
1052 if (fNcells<=1 || IsFreezed())
1053 return;
1054
1055 TH1::Reset(opt);
1056
1057 for (Int_t i=0; i<fNcells-2; i++)
1058 {
1059 fArray[i+1]=0;
1060 ResetUsed(i);
1061 }
1062 fArray[0] = 0;
1063 fArray[fNcells-1] = 0;
1064}
1065
1066// ------------------------------------------------------------------------
1067//
1068// Here we calculate the color index for the current value.
1069// The color index is defined with the class TStyle and the
1070// Color palette inside. We use the command gStyle->SetPalette(1,0)
1071// for the display. So we have to convert the value "wert" into
1072// a color index that fits the color palette.
1073// The range of the color palette is defined by the values fMinPhe
1074// and fMaxRange. Between this values we have 50 color index, starting
1075// with 0 up to 49.
1076//
1077Int_t MHCamera::GetColor(Float_t val, Float_t min, Float_t max, Bool_t islog)
1078{
1079 if (TMath::IsNaN(val)) // FIXME: gLog!
1080 return 10;
1081
1082 //
1083 // first treat the over- and under-flows
1084 //
1085 const Int_t maxcolidx = kItemsLegend-1;
1086
1087 if (val >= max)
1088 return fColors[maxcolidx];
1089
1090 if (val <= min)
1091 return fColors[0];
1092
1093 //
1094 // calculate the color index
1095 //
1096 Float_t ratio;
1097 if (islog && min>0)
1098 ratio = log10(val/min) / log10(max/min);
1099 else
1100 ratio = (val-min) / (max-min);
1101
1102 const Int_t colidx = (Int_t)(ratio*maxcolidx + .5);
1103 return fColors[colidx];
1104}
1105
1106TPaveStats *MHCamera::GetStatisticBox()
1107{
1108 TObject *obj = 0;
1109
1110 TIter Next(fFunctions);
1111 while ((obj = Next()))
1112 if (obj->InheritsFrom(TPaveStats::Class()))
1113 return static_cast<TPaveStats*>(obj);
1114
1115 return NULL;
1116}
1117
1118// ------------------------------------------------------------------------
1119//
1120// Change the text on the legend according to the range of the Display
1121//
1122void MHCamera::UpdateLegend(Float_t min, Float_t max, Bool_t islog)
1123{
1124 const Float_t range = fGeomCam->GetMaxRadius()*1.05;
1125
1126 TArrow arr;
1127 arr.PaintArrow(-range*.9, -range*.9, -range*.6, -range*.9, 0.025);
1128 arr.PaintArrow(-range*.9, -range*.9, -range*.9, -range*.6, 0.025);
1129
1130 TString text;
1131 text += (int)(range*.3);
1132 text += "mm";
1133
1134 TText newtxt2;
1135 newtxt2.SetTextSize(0.04);
1136 newtxt2.PaintText(-range*.85, -range*.85, text);
1137
1138 text = "";
1139 text += (float)((int)(range*.3*fGeomCam->GetConvMm2Deg()*10))/10;
1140 text += "\\circ";
1141 text = text.Strip(TString::kLeading);
1142
1143 TLatex latex;
1144 latex.PaintLatex(-range*.85, -range*.75, 0, 0.04, text);
1145
1146 if (TestBit(kNoLegend))
1147 return;
1148
1149 TPaveStats *stats = GetStatisticBox();
1150
1151 const Float_t hndc = 0.92 - (stats ? stats->GetY1NDC() : 1);
1152 const Float_t H = (0.75-hndc)*range;
1153 const Float_t offset = hndc*range;
1154
1155 const Float_t h = 2./kItemsLegend;
1156 const Float_t w = range/sqrt((float)(fNcells-2));
1157
1158 TBox newbox;
1159 TText newtxt;
1160 newtxt.SetTextSize(0.03);
1161 newtxt.SetTextAlign(12);
1162#if ROOT_VERSION_CODE > ROOT_VERSION(3,01,06)
1163 newtxt.SetBit(/*kNoContextMenu|*/kCannotPick);
1164 newbox.SetBit(/*kNoContextMenu|*/kCannotPick);
1165#endif
1166
1167 const Float_t step = (islog && min>0 ? log10(max/min) : max-min) / kItemsLegend;
1168 const Int_t firsts = step*3 < 1e-8 ? 8 : (Int_t)floor(log10(step*3));
1169 const TString opt = Form("%%.%if", firsts>0 ? 0 : TMath::Abs(firsts));
1170
1171 for (Int_t i=0; i<kItemsLegend+1; i+=3)
1172 {
1173 Float_t val;
1174 if (islog && min>0)
1175 val = pow(10, step*i) * min;
1176 else
1177 val = min + step*i;
1178
1179 //const bool dispexp = max-min>1.5 && fabs(val)>0.1 && fabs(val)<1e6;
1180 newtxt.PaintText(range+1.5*w, H*(i*h-1)-offset, Form(opt, val));
1181 }
1182
1183 for (Int_t i=0; i<kItemsLegend; i++)
1184 {
1185 newbox.SetFillColor(fColors[i]);
1186 newbox.PaintBox(range, H*(i*h-1)-offset, range+w, H*((i+1)*h-1)-offset);
1187 }
1188}
1189
1190// ------------------------------------------------------------------------
1191//
1192// Save primitive as a C++ statement(s) on output stream out
1193//
1194void MHCamera::SavePrimitive(ofstream &out, Option_t *opt)
1195{
1196 gLog << err << "MHCamera::SavePrimitive: Must be rewritten!" << endl;
1197 /*
1198 if (!gROOT->ClassSaved(TCanvas::Class()))
1199 fDrawingPad->SavePrimitive(out, opt);
1200
1201 out << " " << fDrawingPad->GetName() << "->SetWindowSize(";
1202 out << fDrawingPad->GetWw() << "," << fDrawingPad->GetWh() << ");" << endl;
1203 */
1204}
1205
1206// ------------------------------------------------------------------------
1207//
1208// compute the distance of a point (px,py) to the Camera
1209// this functions needed for graphical primitives, that
1210// means without this function you are not able to interact
1211// with the graphical primitive with the mouse!!!
1212//
1213// All calcutations are done in pixel coordinates
1214//
1215Int_t MHCamera::DistancetoPrimitive(Int_t px, Int_t py)
1216{
1217 if (fNcells<=1)
1218 return 999999;
1219
1220 const Int_t kMaxDiff = 7;
1221
1222 TPaveStats *box = (TPaveStats*)gPad->GetPrimitive("stats");
1223 if (box)
1224 {
1225 const Double_t w = box->GetY2NDC()-box->GetY1NDC();
1226 box->SetX1NDC(gStyle->GetStatX()-gStyle->GetStatW());
1227 box->SetY1NDC(gStyle->GetStatY()-w);
1228 box->SetX2NDC(gStyle->GetStatX());
1229 box->SetY2NDC(gStyle->GetStatY());
1230 }
1231
1232 if (TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
1233 return TH1D::DistancetoPrimitive(px, py);
1234
1235 for (Int_t i=0; i<fNcells-2; i++)
1236 {
1237 MHexagon hex((*fGeomCam)[i]);
1238 if (hex.DistancetoPrimitive(px, py)==0)
1239 return 0;
1240 }
1241
1242 if (!box)
1243 return 999999;
1244
1245 const Int_t dist = box->DistancetoPrimitive(px, py);
1246 if (dist > kMaxDiff)
1247 return 999999;
1248
1249 gPad->SetSelected(box);
1250 return dist;
1251}
1252
1253// ------------------------------------------------------------------------
1254//
1255//
1256Int_t MHCamera::GetPixelIndex(Int_t px, Int_t py) const
1257{
1258 if (fNcells<=1)
1259 return -1;
1260
1261 Int_t i;
1262 for (i=0; i<fNcells-2; i++)
1263 {
1264 MHexagon hex((*fGeomCam)[i]);
1265 if (hex.DistancetoPrimitive(px, py)>0)
1266 continue;
1267
1268 return i;
1269 }
1270 return -1;
1271}
1272
1273// ------------------------------------------------------------------------
1274//
1275// Returns string containing info about the object at position (px,py).
1276// Returned string will be re-used (lock in MT environment).
1277//
1278char *MHCamera::GetObjectInfo(Int_t px, Int_t py) const
1279{
1280 if (TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
1281 return TH1D::GetObjectInfo(px, py);
1282
1283 static char info[128];
1284
1285 const Int_t idx=GetPixelIndex(px, py);
1286
1287 if (idx<0)
1288 return TObject::GetObjectInfo(px, py);
1289
1290 sprintf(info, "Software Pixel Index: %d (Hardware Id=%d)", idx, idx+1);
1291 return info;
1292}
1293
1294// ------------------------------------------------------------------------
1295//
1296// Add a MCamEvent which should be displayed when the user clicks on a
1297// pixel.
1298// Warning: The object MUST inherit from TObject AND MCamEvent
1299//
1300void MHCamera::AddNotify(TObject *obj)
1301{
1302 // Make sure, that the object derives from MCamEvent!
1303 MCamEvent *evt = dynamic_cast<MCamEvent*>(obj);
1304 if (!evt)
1305 {
1306 gLog << err << "ERROR: MHCamera::AddNotify - TObject doesn't inherit from MCamEvent... ignored." << endl;
1307 return;
1308 }
1309
1310 // Make sure, that it is deleted from the list too, if the obj is deleted
1311 obj->SetBit(kMustCleanup);
1312
1313 // Add object to list
1314 fNotify->Add(obj);
1315}
1316
1317// ------------------------------------------------------------------------
1318//
1319// Execute a mouse event on the camera
1320//
1321void MHCamera::ExecuteEvent(Int_t event, Int_t px, Int_t py)
1322{
1323 if (TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
1324 {
1325 TH1D::ExecuteEvent(event, px, py);
1326 return;
1327 }
1328 //if (event==kMouseMotion && fStatusBar)
1329 // fStatusBar->SetText(GetObjectInfo(px, py), 0);
1330 if (event!=kButton1Down)
1331 return;
1332
1333 const Int_t idx = GetPixelIndex(px, py);
1334 if (idx<0)
1335 return;
1336
1337 gLog << all << GetTitle() << " <" << GetName() << ">" << endl;
1338 gLog << "Software Pixel Idx: " << idx << endl;
1339 gLog << "Hardware Pixel Id: " << idx+1 << endl;
1340 gLog << "Contents: " << GetBinContent(idx+1);
1341 if (GetBinError(idx+1)>0)
1342 gLog << " +/- " << GetBinError(idx+1);
1343 gLog << " <" << (IsUsed(idx)?"on":"off") << ">" << endl;
1344
1345 if (fNotify && fNotify->GetSize()>0)
1346 {
1347 // FIXME: Is there a simpler and more convinient way?
1348
1349 // The name which is created here depends on the instance of
1350 // MHCamera and on the pad on which it is drawn --> The name
1351 // is unique. For ExecuteEvent gPad is always correctly set.
1352 const TString name = Form("%p;%p;PixelContent", this, gPad);
1353
1354 TCanvas *old = (TCanvas*)gROOT->GetListOfCanvases()->FindObject(name);
1355 if (old)
1356 old->cd();
1357 else
1358 new TCanvas(name);
1359
1360 /*
1361 TIter Next(gPad->GetListOfPrimitives());
1362 TObject *o;
1363 while (o=Next()) cout << o << ": " << o->GetName() << " " << o->IsA()->GetName() << endl;
1364 */
1365
1366 // FIXME: Make sure, that the old histograms are really deleted.
1367 // Are they already deleted?
1368
1369 // The dynamic_cast is necessary here: We cannot use ForEach
1370 TIter Next(fNotify);
1371 MCamEvent *evt;
1372 while ((evt=dynamic_cast<MCamEvent*>(Next())))
1373 evt->DrawPixelContent(idx);
1374
1375 gPad->Modified();
1376 gPad->Update();
1377 }
1378}
1379
1380UInt_t MHCamera::GetNumPixels() const
1381{
1382 return fGeomCam->GetNumPixels();
1383}
1384
1385TH1 *MHCamera::DrawCopy() const
1386{
1387 gPad=NULL;
1388 return TH1D::DrawCopy(fName+";cpy");
1389}
Note: See TracBrowser for help on using the repository browser.