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

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