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

Last change on this file since 2790 was 2790, checked in by gaug, 21 years ago
*** empty log message ***
File size: 33.6 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// 'proj' Display the y-projection of the histogram
378//
379void MHCamera::Draw(Option_t *option)
380{
381 // root 3.02:
382 // gPad->SetFixedAspectRatio()
383 const Color_t col = gPad ? gPad->GetFillColor() : 16;
384 TVirtualPad *pad = gPad ? gPad : MH::MakeDefCanvas("CamDisplay", "Mars Camera Display", 656, 600);
385 pad->SetBorderMode(0);
386 pad->SetFillColor(col);
387
388 //
389 // Create an own pad for the MHCamera-Object which can be
390 // resized in paint to keep the correct aspect ratio
391 //
392 pad->Divide(1, 1, 0, 0, col);
393 pad->cd(1);
394 gPad->SetBorderMode(0);
395
396 TString opt(option);
397 opt.ToLower();
398
399 AppendPad(option);
400
401 //
402 // Do not change gPad. The user should not see, that Draw
403 // changes gPad...
404 //
405 pad->cd();
406}
407
408// ------------------------------------------------------------------------
409//
410// This is TObject::DrawClone but completely ignores
411// gROOT->GetSelectedPad(). tbretz had trouble with this in the past.
412// If this makes trouble please write a bug report.
413//
414TObject *MHCamera::DrawClone(Option_t *option) const
415{
416 // Draw a clone of this object in the current pad
417
418 //TVirtualPad *pad = gROOT->GetSelectedPad();
419 TVirtualPad *padsav = gPad;
420 //if (pad) pad->cd();
421
422 TObject *newobj = Clone();
423
424 if (!newobj)
425 return 0;
426
427 /*
428 if (pad) {
429 if (strlen(option)) pad->GetListOfPrimitives()->Add(newobj,option);
430 else pad->GetListOfPrimitives()->Add(newobj,GetDrawOption());
431 pad->Modified(kTRUE);
432 pad->Update();
433 if (padsav) padsav->cd();
434 return newobj;
435 }
436 */
437
438 TString opt(option);
439 opt.ToLower();
440
441 newobj->Draw(opt.IsNull() ? GetDrawOption() : option);
442
443 if (padsav)
444 padsav->cd();
445
446 return newobj;
447}
448
449void MHCamera::CreateProjection()
450{
451
452 Int_t nbins = 50;
453
454 // Create the projection histogram
455 TString ytitle(GetYaxis()->GetTitle());
456 fYProj = new TH1D(ytitle.Data(),GetTitle(),nbins,GetMinimum()-0.1,GetMaximum()+0.1);
457 fYProj->SetXTitle(ytitle.Data());
458 fYProj->SetYTitle("Nr. of pixels");
459 fYProj->Sumw2();
460 fYProj->SetDirectory(NULL);
461
462 // Fill the projected histogram
463 Double_t cont;
464 for (Int_t binx =0;binx<=GetNbinsX()+1;binx++) {
465 cont = GetBinContent(binx);
466 if (cont)
467 fYProj->Fill(cont);
468 }
469}
470
471
472// ------------------------------------------------------------------------
473//
474// Resizes the current pad so that the camera is displayed in its
475// correct aspect ratio
476//
477void MHCamera::SetRange()
478{
479 const Float_t range = fGeomCam->GetMaxRadius()*1.05;
480
481 //
482 // Maintain aspect ratio
483 //
484 const float ratio = TestBit(kNoLegend) ? 1 : 1.15;
485
486 //
487 // Calculate width and height of the current pad in pixels
488 //
489 Float_t w = gPad->GetWw();
490 Float_t h = gPad->GetWh()*ratio;
491
492 //
493 // This prevents the pad from resizing itself wrongly
494 //
495 if (gPad->GetMother() != gPad)
496 {
497 w *= gPad->GetMother()->GetAbsWNDC();
498 h *= gPad->GetMother()->GetAbsHNDC();
499 }
500
501 //
502 // Set Range (coordinate system) of pad
503 //
504 gPad->Range(-range, -range, (2*ratio-1)*range, range);
505
506 //
507 // Resize Pad to given ratio
508 //
509 if (h<w)
510 gPad->SetPad((1.-h/w)/2, 0, (h/w+1.)/2, 1);
511 else
512 gPad->SetPad(0, (1.-w/h)/2, 1, (w/h+1.)/2);
513}
514
515// ------------------------------------------------------------------------
516//
517// Updates the pixel colors and paints the pixels
518//
519void MHCamera::Update(Bool_t islog, Bool_t isbox, Bool_t iscol)
520{
521 Double_t min = GetMinimum(kFALSE);
522 Double_t max = GetMaximum(kFALSE);
523 if (min==FLT_MAX)
524 {
525 min = 0;
526 max = 1;
527 }
528
529 if (min==max)
530 max += 1;
531
532 UpdateLegend(min, max, islog);
533
534 MHexagon hex;
535 for (Int_t i=0; i<fNcells-2; i++)
536 {
537 if (IsUsed(i) && iscol)
538 {
539 if (TMath::IsNaN(fArray[i+1]))
540 gLog << warn << "MHCamera::Update: " << GetName() << " <" << GetTitle() << "> - Pixel Index #" << i << " contents is NaN (Not a Number)..." << endl;
541
542 hex.SetFillColor(GetColor(GetBinContent(i+1), min, max, islog));
543 }
544 else
545 hex.SetFillColor(10);
546
547 MGeomPix &pix = (*fGeomCam)[i];
548 if (!isbox)
549 hex.PaintHexagon(pix.GetX(), pix.GetY(), pix.GetD());
550 else
551 if (IsUsed(i) && !TMath::IsNaN(fArray[i+1]))
552 {
553 Float_t size = pix.GetD()*(GetBinContent(i+1)-min)/(max-min);
554 if (size>pix.GetD())
555 size=pix.GetD();
556 hex.PaintHexagon(pix.GetX(), pix.GetY(), size);
557 }
558 }
559}
560
561// ------------------------------------------------------------------------
562//
563// Print minimum and maximum
564//
565void MHCamera::Print(Option_t *) const
566{
567 cout << "Minimum: " << GetMinimum();
568 if (fMinimum==-1111)
569 cout << " <autoscaled>";
570 cout << endl;
571 cout << "Maximum: " << GetMaximum();
572 if (fMaximum==-1111)
573 cout << " <autoscaled>";
574 cout << endl;
575}
576
577// ------------------------------------------------------------------------
578//
579// Paint the y-axis title
580//
581void MHCamera::PaintAxisTitle()
582{
583 const Float_t range = fGeomCam->GetMaxRadius()*1.05;
584 const Float_t w = (1 + 1.5/sqrt((float)(fNcells-2)))*range;
585
586 TLatex *ptitle = new TLatex(w, -.90*range, GetYaxis()->GetTitle());
587
588 ptitle->SetTextSize(0.05);
589 ptitle->SetTextAlign(21);
590
591 // box with the histogram title
592 ptitle->SetTextColor(gStyle->GetTitleTextColor());
593#if ROOT_VERSION_CODE > ROOT_VERSION(3,05,01)
594 ptitle->SetTextFont(gStyle->GetTitleFont(""));
595#endif
596 ptitle->Paint();
597}
598
599// ------------------------------------------------------------------------
600//
601// Paints the camera.
602//
603void MHCamera::Paint(Option_t *o)
604{
605 if (fNcells<=1)
606 return;
607
608 TString opt(o);
609 opt.ToLower();
610
611 if (opt.Contains("hist"))
612 {
613 opt.ReplaceAll("hist", "");
614 TH1D::Paint(opt);
615 return;
616 }
617
618 if (opt.Contains("proj"))
619 {
620
621 CreateProjection();
622 opt.ReplaceAll("proj", "");
623 Float_t he = gStyle->GetStatH();
624 Float_t wi = gStyle->GetStatH();
625 gStyle->SetStatH(0.4);
626 gStyle->SetStatW(0.25);
627 fYProj->Paint(opt);
628 gStyle->SetStatH(he);
629 gStyle->SetStatW(wi);
630 return;
631 }
632
633 gPad->Clear();
634
635 // Maintain aspect ratio
636 SetRange();
637
638 Bool_t isbox = opt.Contains("box");
639 Bool_t iscol = isbox ? !opt.Contains("nocol") : 1;
640
641 GetPainter();
642 if (fPainter)
643 {
644 if (!TestBit(TH1::kNoStats))
645 {
646 fPainter->SetHistogram(this);
647 fPainter->PaintStat(gStyle->GetOptStat(), NULL);
648 }
649
650 // Paint primitives (pixels, color legend, photons, ...)
651 if (fPainter->InheritsFrom(THistPainter::Class()))
652 ((THistPainter*)fPainter)->PaintTitle();
653 }
654
655 // Update Contents of the pixels and paint legend
656 Update(gPad->GetLogy(), isbox, iscol);
657 PaintAxisTitle();
658
659 if (opt.Contains("pixelindex"))
660 PaintIndices(0);
661 if (opt.Contains("sectorindex"))
662 PaintIndices(1);
663 if (opt.Contains("content"))
664 PaintIndices(2);
665}
666
667// ------------------------------------------------------------------------
668//
669// With this function you can change the color palette. For more
670// information see TStyle::SetPalette. Only palettes with 50 colors
671// are allowed.
672// In addition you can use SetPalette(52, 0) to create an inverse
673// deep blue sea palette
674//
675void MHCamera::SetPalette(Int_t ncolors, Int_t *colors)
676{
677 //
678 // If not enough colors are specified skip this.
679 //
680 if (ncolors>1 && ncolors<50)
681 {
682 cout << "MHCamera::SetPalette: Only default palettes with 50 colors are allowed... ignored." << endl;
683 return;
684 }
685
686 //
687 // If ncolors==52 create a reversed deep blue sea palette
688 //
689 if (ncolors==52)
690 {
691 gStyle->SetPalette(51, NULL);
692 TArrayI c(kItemsLegend);
693 for (int i=0; i<kItemsLegend; i++)
694 c[kItemsLegend-i-1] = gStyle->GetColorPalette(i);
695 gStyle->SetPalette(kItemsLegend, c.GetArray());
696 }
697 else
698 gStyle->SetPalette(ncolors, colors);
699
700 fColors.Set(kItemsLegend);
701 for (int i=0; i<kItemsLegend; i++)
702 fColors[i] = gStyle->GetColorPalette(i);
703}
704
705
706void MHCamera::SetPrettyPalette()
707{
708 if (!TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
709 SetPalette(1, 0);
710}
711
712void MHCamera::SetDeepBlueSeaPalette()
713{
714 if (!TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
715 SetPalette(51, 0);
716}
717
718void MHCamera::SetInvDeepBlueSeaPalette()
719{
720 if (!TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
721 SetPalette(52, 0);
722}
723
724void MHCamera::PaintIndices(Int_t type)
725{
726 if (fNcells<=1)
727 return;
728
729 const Double_t min = GetMinimum();
730 const Double_t max = GetMaximum();
731
732 if (type==2 && max==min)
733 return;
734
735 TText txt;
736 txt.SetTextFont(122);
737 txt.SetTextAlign(22); // centered/centered
738
739 for (Int_t i=0; i<fNcells-2; i++)
740 {
741 const MGeomPix &h = (*fGeomCam)[i];
742
743 TString num;
744 switch (type)
745 {
746 case 0: num += i; break;
747 case 1: num += h.GetSector(); break;
748 case 2: num += (Int_t)((fArray[i+1]-min)/(max-min)); break;
749 }
750
751 // FIXME: Should depend on the color of the pixel...
752 //(GetColor(GetBinContent(i+1), min, max, 0));
753 txt.SetTextColor(kRed);
754 txt.SetTextSize(0.3*h.GetD()/fGeomCam->GetMaxRadius()/1.05);
755 txt.PaintText(h.GetX(), h.GetY(), num);
756 }
757}
758
759// ------------------------------------------------------------------------
760//
761// Call this function to add a MCamEvent on top of the present contents.
762//
763void MHCamera::AddCamContent(const MCamEvent &event, Int_t type)
764{
765 if (fNcells<=1 || IsFreezed())
766 return;
767
768 // FIXME: Security check missing!
769 for (Int_t idx=0; idx<fNcells-2; idx++)
770 {
771 Double_t val=0;
772 if (event.GetPixelContent(val, idx, *fGeomCam, type)/* && !IsUsed(idx)*/)
773 SetUsed(idx);
774
775 Fill(idx, val); // FIXME: Slow!
776 }
777 fEntries++;
778}
779
780// ------------------------------------------------------------------------
781//
782// Call this function to add a MCamEvent on top of the present contents.
783//
784void MHCamera::SetCamError(const MCamEvent &evt, Int_t type)
785{
786
787 if (fNcells<=1 || IsFreezed())
788 return;
789
790 // FIXME: Security check missing!
791 for (Int_t idx=0; idx<fNcells-2; idx++)
792 {
793 Double_t val=0;
794 if (evt.GetPixelContent(val, idx, *fGeomCam, type)/* && !IsUsed(idx)*/)
795 SetUsed(idx);
796
797 SetBinError(idx+1, val); // FIXME: Slow!
798 }
799}
800
801Stat_t MHCamera::GetBinError(Int_t bin) const
802{
803 Double_t rc = 0;
804 if (TestBit(kVariance) && GetEntries()>0) // error on the mean
805 {
806 const Double_t error = fSumw2.fArray[bin]/GetEntries();
807 const Double_t val = fArray[bin]/GetEntries();
808 rc = TMath::Sqrt(error - val*val);
809 }
810 else
811 rc = TH1D::GetBinError(bin);
812
813 return Profile(rc);
814}
815
816// ------------------------------------------------------------------------
817//
818// Call this function to add a MHCamera on top of the present contents.
819// Type:
820// 0) bin content
821// 1) errors
822// 2) rel. errors
823//
824void MHCamera::AddCamContent(const MHCamera &d, Int_t type)
825{
826 if (fNcells!=d.fNcells || IsFreezed())
827 return;
828
829 // FIXME: Security check missing!
830 for (Int_t idx=0; idx<fNcells-2; idx++)
831 if (d.IsUsed(idx))
832 SetUsed(idx);
833
834 switch (type)
835 {
836 case 1:
837 for (Int_t idx=0; idx<fNcells-2; idx++)
838 Fill(idx, d.GetBinError(idx+1));
839 break;
840 case 2:
841 for (Int_t idx=0; idx<fNcells-2; idx++)
842 if (d.GetBinContent(idx+1)!=0)
843 Fill(idx, TMath::Abs(d.GetBinError(idx+1)/d.GetBinContent(idx+1)));
844 break;
845 default:
846 for (Int_t idx=0; idx<fNcells-2; idx++)
847 Fill(idx, d.GetBinContent(idx+1));
848 break;
849 }
850 fEntries++;
851}
852
853// ------------------------------------------------------------------------
854//
855// Call this function to add a TArrayD on top of the present contents.
856//
857void MHCamera::AddCamContent(const TArrayD &event, const TArrayC *used)
858{
859 if (event.GetSize()!=fNcells-2 || IsFreezed())
860 return;
861
862 if (used && used->GetSize()!=fNcells-2)
863 return;
864
865 for (Int_t idx=0; idx<fNcells-2; idx++)
866 {
867 Fill(idx, const_cast<TArrayD&>(event)[idx]); // FIXME: Slow!
868
869 if (!used || (*const_cast<TArrayC*>(used))[idx])
870 SetUsed(idx);
871 }
872 fEntries++;
873}
874
875// ------------------------------------------------------------------------
876//
877// Call this function to add a MCamEvent on top of the present contents.
878// 1 is added to each pixel if the contents of MCamEvent>threshold
879//
880void MHCamera::CntCamContent(const MCamEvent &event, Double_t threshold, Int_t type)
881{
882 if (fNcells<=1 || IsFreezed())
883 return;
884
885 // FIXME: Security check missing!
886 for (Int_t idx=0; idx<fNcells-2; idx++)
887 {
888 Double_t val=threshold;
889 if (event.GetPixelContent(val, idx, *fGeomCam, type)/* && !IsUsed(idx)*/)
890 SetUsed(idx);
891
892 if (val>threshold)
893 Fill(idx);
894 }
895 fEntries++;
896}
897
898// ------------------------------------------------------------------------
899//
900// Call this function to add a TArrayD on top of the present contents.
901// 1 is added to each pixel if the contents of MCamEvent>threshold
902//
903void MHCamera::CntCamContent(const TArrayD &event, Double_t threshold, Bool_t ispos)
904{
905 if (event.GetSize()!=fNcells-2 || IsFreezed())
906 return;
907
908 for (Int_t idx=0; idx<fNcells-2; idx++)
909 {
910 if (const_cast<TArrayD&>(event)[idx]>threshold)
911 Fill(idx);
912
913 if (!ispos || fArray[idx+1]>0)
914 SetUsed(idx);
915 }
916 fEntries++;
917}
918
919// ------------------------------------------------------------------------
920//
921// Fill the pixels with random contents.
922//
923void MHCamera::FillRandom()
924{
925 if (fNcells<=1 || IsFreezed())
926 return;
927
928 Reset();
929
930 // FIXME: Security check missing!
931 for (Int_t idx=0; idx<fNcells-2; idx++)
932 {
933 Fill(idx, gRandom->Uniform()*fGeomCam->GetPixRatio(idx));
934 SetUsed(idx);
935 }
936 fEntries=1;
937}
938
939
940// ------------------------------------------------------------------------
941//
942// The array must be in increasing order, eg: 2.5, 3.7, 4.9
943// The values in each bin are replaced by the interval in which the value
944// fits. In the example we have four intervals
945// (<2.5, 2.5-3.7, 3.7-4.9, >4.9). Maximum and minimum are set
946// accordingly.
947//
948void MHCamera::SetLevels(const TArrayF &arr)
949{
950 if (fNcells<=1)
951 return;
952
953 for (Int_t i=0; i<fNcells-2; i++)
954 {
955 if (!IsUsed(i))
956 continue;
957
958 Int_t j = arr.GetSize();
959 while (j && fArray[i+1]<arr[j-1])
960 j--;
961
962 fArray[i+1] = j;
963 }
964 SetMaximum(arr.GetSize());
965 SetMinimum(0);
966}
967
968// ------------------------------------------------------------------------
969//
970// Reset the all pixel colors to a default value
971//
972void MHCamera::Reset(Option_t *opt)
973{
974 if (fNcells<=1 || IsFreezed())
975 return;
976
977 TH1::Reset(opt);
978
979 for (Int_t i=0; i<fNcells-2; i++)
980 {
981 fArray[i+1]=0;
982 ResetUsed(i);
983 }
984 fArray[0] = 0;
985 fArray[fNcells-1] = 0;
986}
987
988// ------------------------------------------------------------------------
989//
990// Here we calculate the color index for the current value.
991// The color index is defined with the class TStyle and the
992// Color palette inside. We use the command gStyle->SetPalette(1,0)
993// for the display. So we have to convert the value "wert" into
994// a color index that fits the color palette.
995// The range of the color palette is defined by the values fMinPhe
996// and fMaxRange. Between this values we have 50 color index, starting
997// with 0 up to 49.
998//
999Int_t MHCamera::GetColor(Float_t val, Float_t min, Float_t max, Bool_t islog)
1000{
1001 if (TMath::IsNaN(val)) // FIXME: gLog!
1002 return 10;
1003
1004 //
1005 // first treat the over- and under-flows
1006 //
1007 const Int_t maxcolidx = kItemsLegend-1;
1008
1009 if (val >= max)
1010 return fColors[maxcolidx];
1011
1012 if (val <= min)
1013 return fColors[0];
1014
1015 //
1016 // calculate the color index
1017 //
1018 Float_t ratio;
1019 if (islog && min>0)
1020 ratio = log10(val/min) / log10(max/min);
1021 else
1022 ratio = (val-min) / (max-min);
1023
1024 const Int_t colidx = (Int_t)(ratio*maxcolidx + .5);
1025 return fColors[colidx];
1026}
1027
1028TPaveStats *MHCamera::GetStatisticBox()
1029{
1030 TObject *obj = 0;
1031
1032 TIter Next(fFunctions);
1033 while ((obj = Next()))
1034 if (obj->InheritsFrom(TPaveStats::Class()))
1035 return static_cast<TPaveStats*>(obj);
1036
1037 return NULL;
1038}
1039
1040// ------------------------------------------------------------------------
1041//
1042// Change the text on the legend according to the range of the Display
1043//
1044void MHCamera::UpdateLegend(Float_t min, Float_t max, Bool_t islog)
1045{
1046 const Float_t range = fGeomCam->GetMaxRadius()*1.05;
1047
1048 TArrow arr;
1049 arr.PaintArrow(-range*.9, -range*.9, -range*.6, -range*.9, 0.025);
1050 arr.PaintArrow(-range*.9, -range*.9, -range*.9, -range*.6, 0.025);
1051
1052 TString text;
1053 text += (int)(range*.3);
1054 text += "mm";
1055
1056 TText newtxt2;
1057 newtxt2.SetTextSize(0.04);
1058 newtxt2.PaintText(-range*.85, -range*.85, text);
1059
1060 text = "";
1061 text += (float)((int)(range*.3*fGeomCam->GetConvMm2Deg()*10))/10;
1062 text += "\\circ";
1063 text = text.Strip(TString::kLeading);
1064
1065 TLatex latex;
1066 latex.PaintLatex(-range*.85, -range*.75, 0, 0.04, text);
1067
1068 if (TestBit(kNoLegend))
1069 return;
1070
1071 TPaveStats *stats = GetStatisticBox();
1072
1073 const Float_t hndc = 0.92 - (stats ? stats->GetY1NDC() : 1);
1074 const Float_t H = (0.75-hndc)*range;
1075 const Float_t offset = hndc*range;
1076
1077 const Float_t h = 2./kItemsLegend;
1078 const Float_t w = range/sqrt((float)(fNcells-2));
1079
1080 TBox newbox;
1081 TText newtxt;
1082 newtxt.SetTextSize(0.03);
1083 newtxt.SetTextAlign(12);
1084#if ROOT_VERSION_CODE > ROOT_VERSION(3,01,06)
1085 newtxt.SetBit(/*kNoContextMenu|*/kCannotPick);
1086 newbox.SetBit(/*kNoContextMenu|*/kCannotPick);
1087#endif
1088
1089 const Float_t step = (islog && min>0 ? log10(max/min) : max-min) / kItemsLegend;
1090 const Int_t firsts = step*3 < 1e-8 ? 8 : (Int_t)floor(log10(step*3));
1091 const TString opt = Form("%%.%if", firsts>0 ? 0 : TMath::Abs(firsts));
1092
1093 for (Int_t i=0; i<kItemsLegend+1; i+=3)
1094 {
1095 Float_t val;
1096 if (islog && min>0)
1097 val = pow(10, step*i) * min;
1098 else
1099 val = min + step*i;
1100
1101 //const bool dispexp = max-min>1.5 && fabs(val)>0.1 && fabs(val)<1e6;
1102 newtxt.PaintText(range+1.5*w, H*(i*h-1)-offset, Form(opt, val));
1103 }
1104
1105 for (Int_t i=0; i<kItemsLegend; i++)
1106 {
1107 newbox.SetFillColor(fColors[i]);
1108 newbox.PaintBox(range, H*(i*h-1)-offset, range+w, H*((i+1)*h-1)-offset);
1109 }
1110}
1111
1112// ------------------------------------------------------------------------
1113//
1114// Save primitive as a C++ statement(s) on output stream out
1115//
1116void MHCamera::SavePrimitive(ofstream &out, Option_t *opt)
1117{
1118 cout << "MHCamera::SavePrimitive: Must be rewritten!" << endl;
1119 /*
1120 if (!gROOT->ClassSaved(TCanvas::Class()))
1121 fDrawingPad->SavePrimitive(out, opt);
1122
1123 out << " " << fDrawingPad->GetName() << "->SetWindowSize(";
1124 out << fDrawingPad->GetWw() << "," << fDrawingPad->GetWh() << ");" << endl;
1125 */
1126}
1127
1128// ------------------------------------------------------------------------
1129//
1130// compute the distance of a point (px,py) to the Camera
1131// this functions needed for graphical primitives, that
1132// means without this function you are not able to interact
1133// with the graphical primitive with the mouse!!!
1134//
1135// All calcutations are done in pixel coordinates
1136//
1137Int_t MHCamera::DistancetoPrimitive(Int_t px, Int_t py)
1138{
1139 if (fNcells<=1)
1140 return 999999;
1141
1142 const Int_t kMaxDiff = 7;
1143
1144 TPaveStats *box = (TPaveStats*)gPad->GetPrimitive("stats");
1145 if (box)
1146 {
1147 const Double_t w = box->GetY2NDC()-box->GetY1NDC();
1148 box->SetX1NDC(gStyle->GetStatX()-gStyle->GetStatW());
1149 box->SetY1NDC(gStyle->GetStatY()-w);
1150 box->SetX2NDC(gStyle->GetStatX());
1151 box->SetY2NDC(gStyle->GetStatY());
1152 }
1153
1154 if (TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
1155 return TH1D::DistancetoPrimitive(px, py);
1156
1157 for (Int_t i=0; i<fNcells-2; i++)
1158 {
1159 MHexagon hex((*fGeomCam)[i]);
1160 if (hex.DistancetoPrimitive(px, py)==0)
1161 return 0;
1162 }
1163
1164 if (!box)
1165 return 999999;
1166
1167 const Int_t dist = box->DistancetoPrimitive(px, py);
1168 if (dist > kMaxDiff)
1169 return 999999;
1170
1171 gPad->SetSelected(box);
1172 return dist;
1173}
1174
1175// ------------------------------------------------------------------------
1176//
1177// Function introduced (31-01-03) WILL BE REMOVED IN THE FUTURE! DON'T
1178// USE IT!
1179//
1180void MHCamera::SetPix(const Int_t idx, const Int_t color, Float_t min, Float_t max)
1181{
1182 fArray[idx+1] = color;
1183 SetUsed(idx);
1184}
1185
1186// ------------------------------------------------------------------------
1187//
1188//
1189Int_t MHCamera::GetPixelIndex(Int_t px, Int_t py) const
1190{
1191 if (fNcells<=1)
1192 return -1;
1193
1194 Int_t i;
1195 for (i=0; i<fNcells-2; i++)
1196 {
1197 MHexagon hex((*fGeomCam)[i]);
1198 if (hex.DistancetoPrimitive(px, py)>0)
1199 continue;
1200
1201 return i;
1202 }
1203 return -1;
1204}
1205
1206// ------------------------------------------------------------------------
1207//
1208// Returns string containing info about the object at position (px,py).
1209// Returned string will be re-used (lock in MT environment).
1210//
1211char *MHCamera::GetObjectInfo(Int_t px, Int_t py) const
1212{
1213 if (TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
1214 return TH1D::GetObjectInfo(px, py);
1215
1216 static char info[128];
1217
1218 const Int_t idx=GetPixelIndex(px, py);
1219
1220 if (idx<0)
1221 return TObject::GetObjectInfo(px, py);
1222
1223 sprintf(info, "Software Pixel Index: %d (Hardware Id=%d)", idx, idx+1);
1224 return info;
1225}
1226
1227// ------------------------------------------------------------------------
1228//
1229// Execute a mouse event on the camera
1230//
1231void MHCamera::ExecuteEvent(Int_t event, Int_t px, Int_t py)
1232{
1233 if (TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
1234 {
1235 TH1D::ExecuteEvent(event, px, py);
1236 return;
1237 }
1238 //if (event==kMouseMotion && fStatusBar)
1239 // fStatusBar->SetText(GetObjectInfo(px, py), 0);
1240 if (event!=kButton1Down)
1241 return;
1242
1243 const Int_t idx = GetPixelIndex(px, py);
1244 if (idx<0)
1245 return;
1246
1247 cout << GetTitle() << " <" << GetName() << ">" << endl;
1248 cout << "Software Pixel Index: " << idx << endl;
1249 cout << "Hardware Pixel Id: " << idx+1 << endl;
1250 cout << "Contents: " << GetBinContent(idx+1);
1251 if (GetBinError(idx+1)>0)
1252 cout << " +/- " << GetBinError(idx+1);
1253 cout << " <" << (IsUsed(idx)?"on":"off") << ">" << endl;
1254
1255 if (fNotify && fNotify->GetSize()>0)
1256 {
1257 // FIXME: Is there a simpler and more convinient way?
1258
1259 // The name which is created here depends on the instance of
1260 // MHCamera and on the pad on which it is drawn --> The name
1261 // is unique. For ExecuteEvent gPad is always correctly set.
1262 const TString name = Form("%p;%p;PixelContent", this, gPad);
1263
1264 TCanvas *old = (TCanvas*)gROOT->GetListOfCanvases()->FindObject(name);
1265 if (old)
1266 old->cd();
1267 else
1268 new TCanvas(name);
1269
1270 /*
1271 TIter Next(gPad->GetListOfPrimitives());
1272 TObject *o;
1273 while (o=Next()) cout << o << ": " << o->GetName() << " " << o->IsA()->GetName() << endl;
1274 */
1275
1276 // FIXME: Make sure, that the old histograms are really deleted.
1277 // Are they already deleted?
1278 fNotify->ForEach(MCamEvent, DrawPixelContent)(idx);
1279 gPad->Modified();
1280 gPad->Update();
1281 }
1282}
1283
1284UInt_t MHCamera::GetNumPixels() const
1285{
1286 return fGeomCam->GetNumPixels();
1287}
1288
1289TH1 *MHCamera::DrawCopy() const
1290{
1291 gPad=NULL;
1292 return TH1D::DrawCopy(fName+";cpy");
1293}
Note: See TracBrowser for help on using the repository browser.