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

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