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

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