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

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