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

Last change on this file since 2209 was 2209, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 26.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. The Pixels are displayed in
31// contents/area [somthing/mm^2]
32//
33// To change the scale to a logarithmic scale SetLogz() of the Pad.
34//
35//
36////////////////////////////////////////////////////////////////////////////
37#include "MHCamera.h"
38
39#include <fstream>
40#include <iostream>
41
42#include <TBox.h>
43#include <TArrow.h>
44#include <TLatex.h>
45#include <TStyle.h>
46#include <TMarker.h>
47#include <TCanvas.h>
48#include <TArrayF.h>
49#include <TRandom.h>
50#include <TPaveText.h>
51#include <TClonesArray.h>
52
53#include "MH.h"
54#include "MHexagon.h"
55
56#include "MGeomPix.h"
57#include "MGeomCam.h"
58
59#include "MRflEvtData.h"
60#include "MRflSinglePhoton.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#define kItemsLegend 48 // see SetPalette(1,0)
74
75ClassImp(MHCamera);
76
77using namespace std;
78
79// ------------------------------------------------------------------------
80//
81// Default Constructor. To be used by the root system ONLY.
82//
83MHCamera::MHCamera() : TH1D(), fGeomCam(NULL), fColors(kItemsLegend)
84{
85 SetDirectory(NULL);
86
87 fPhotons = NULL;
88 fNotify = NULL;
89
90#if ROOT_VERSION_CODE < ROOT_VERSION(3,01,06)
91 SetPalette(1, 0);
92#else
93 SetPalette(51, 0);
94#endif
95}
96
97// ------------------------------------------------------------------------
98//
99// Constructor. Makes a clone of MGeomCam.
100//
101MHCamera::MHCamera(const MGeomCam &geom, const char *name, const char *title)
102: TH1D(name, title, geom.GetNumPixels(), -0.5, geom.GetNumPixels()-0.5),
103fUsed(geom.GetNumPixels()), fColors(kItemsLegend)
104{
105 fGeomCam = (MGeomCam*)geom.Clone();
106
107 SetDirectory(NULL);
108 Sumw2();
109
110 SetLineColor(kGreen);
111 SetMarkerStyle(kFullDotMedium);
112
113 fNotify = new TList;
114
115 //
116 // create the hexagons of the display
117 //
118 // root 3.02
119 // * base/inc/TObject.h:
120 // register BIT(8) as kNoContextMenu. If an object has this bit set it will
121 // not get an automatic context menu when clicked with the right mouse button.
122
123 fPhotons = new TClonesArray("TMarker", 0);
124
125 //
126 // Construct all hexagons. Use new-operator with placement
127 //
128 for (Int_t i=0; i<fNcells-2; i++)
129 ResetUsed(i);
130
131#if ROOT_VERSION_CODE < ROOT_VERSION(3,01,06)
132 SetPalette(1, 0);
133#else
134 SetPalette(51, 0);
135#endif
136}
137
138// ------------------------------------------------------------------------
139//
140// Destructor. Deletes TClonesArrays for hexagons and legend elements.
141//
142MHCamera::~MHCamera()
143{
144 if (fPhotons)
145 {
146 fPhotons->Delete();
147 delete fPhotons;
148 }
149 if (fGeomCam)
150 delete fGeomCam;
151 if (fNotify)
152 delete fNotify;
153}
154
155Int_t MHCamera::Fill(Axis_t x)
156{
157// -*-*-*-*-*-*-*-*Increment bin with abscissa X by 1*-*-*-*-*-*-*-*-*-*-*
158// ==================================
159//
160// if x is less than the low-edge of the first bin, the Underflow bin is incremented
161// if x is greater than the upper edge of last bin, the Overflow bin is incremented
162//
163// If the storage of the sum of squares of weights has been triggered,
164// via the function Sumw2, then the sum of the squares of weights is incremented
165// by 1 in the bin corresponding to x.
166//
167// -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
168
169 if (fBuffer) return BufferFill(x,1);
170
171 const Int_t bin = (Int_t)x+1;
172 AddBinContent(bin);
173 if (fSumw2.fN)
174 fSumw2.fArray[bin]++;
175
176 if (bin<=0 || bin>fNcells-2)
177 return -1;
178
179 fTsumw++;
180 fTsumw2++;
181 fTsumwx += x;
182 fTsumwx2 += x*x;
183 return bin;
184}
185
186//______________________________________________________________________________
187Int_t MHCamera::Fill(Axis_t x, Stat_t w)
188{
189// -*-*-*-*-*-*Increment bin with abscissa X with a weight w*-*-*-*-*-*-*-*
190// =============================================
191//
192// if x is less than the low-edge of the first bin, the Underflow bin is incremented
193// if x is greater than the upper edge of last bin, the Overflow bin is incremented
194//
195// If the storage of the sum of squares of weights has been triggered,
196// via the function Sumw2, then the sum of the squares of weights is incremented
197// by w^2 in the bin corresponding to x.
198//
199// -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
200
201 if (fBuffer) return BufferFill(x,w);
202
203 const Int_t bin = (Int_t)x+1;
204 AddBinContent(bin, w);
205 if (fSumw2.fN)
206 fSumw2.fArray[bin] += w*w;
207
208 if (bin<=0 || bin>fNcells-2)
209 return -1;
210
211 const Stat_t z = (w > 0 ? w : -w);
212 fTsumw += z;
213 fTsumw2 += z*z;
214 fTsumwx += z*x;
215 fTsumwx2 += z*x*x;
216 return bin;
217}
218
219// ------------------------------------------------------------------------
220//
221// Return the minimum contents of all pixels (if all is set, otherwise
222// only of all 'used' pixels), fMinimum if fMinimum set
223//
224Double_t MHCamera::GetMinimum(Bool_t all) const
225{
226 if (fMinimum != -1111)
227 return fMinimum;
228
229 Double_t minimum=FLT_MAX;
230
231 if (all)
232 {
233 for (Int_t idx=0; idx<fNcells-2; idx++)
234 if (fArray[idx+1] < minimum)
235 minimum = fArray[idx+1];
236 }
237 else
238 {
239 for (Int_t idx=0; idx<fNcells-2; idx++)
240 if (IsUsed(idx) && fArray[idx+1] < minimum)
241 minimum = fArray[idx+1];
242 }
243 return minimum;
244}
245
246// ------------------------------------------------------------------------
247//
248// Return the maximum contents of all pixels (if all is set, otherwise
249// only of all 'used' pixels), fMaximum if fMaximum set
250//
251Double_t MHCamera::GetMaximum(Bool_t all) const
252{
253 if (fMaximum != -1111)
254 return fMaximum;
255
256 Double_t maximum=-FLT_MAX;
257 if (all)
258 {
259 for (Int_t idx=0; idx<fNcells-2; idx++)
260 if (fArray[idx+1] > maximum)
261 maximum = fArray[idx+1];
262 }
263 else
264 {
265 for (Int_t idx=0; idx<fNcells-2; idx++)
266 if (IsUsed(idx) && fArray[idx+1] > maximum)
267 maximum = fArray[idx+1];
268 }
269 return maximum;
270}
271
272// ------------------------------------------------------------------------
273//
274// Call this function to draw the camera layout into your canvas.
275// Setup a drawing canvas. Add this object and all child objects
276// (hexagons, etc) to the current pad. If no pad exists a new one is
277// created.
278//
279// To draw a camera into its own pad do something like:
280//
281// TCanvas *c = new TCanvas;
282// c->Divide(2,1);
283// MGeomCamMagic m;
284// MHCamera *d=new MHCamera(&m);
285// d->FillRandom();
286// c->cd(1);
287// gPad->SetBorderMode(0);
288// gPad->Divide(1,1);
289// gPad->cd(1);
290// d->Draw();
291// d->SetBit(kCanDelete);
292//
293void MHCamera::Draw(Option_t *option)
294{
295 // root 3.02:
296 // gPad->SetFixedAspectRatio()
297 Int_t col = 16;
298
299 if (gPad)
300 col = gPad->GetFillColor();
301
302 TVirtualPad *pad = gPad ? gPad : MH::MakeDefCanvas("CamDisplay", "Mars Camera Display", 656, 600);
303 pad->SetBorderMode(0);
304 pad->SetFillColor(col);
305
306 AppendPad(option);
307}
308
309
310void MHCamera::SetRange()
311{
312 const Float_t range = fGeomCam->GetMaxRadius();
313
314 //
315 // Maintain aspect ratio
316 //
317 const float ratio = 1.15;
318
319 //
320 // Calculate width and height of the current pad in pixels
321 //
322 Float_t w = gPad->GetWw();
323 Float_t h = gPad->GetWh()*ratio;
324
325 //
326 // This prevents the pad from resizing itself wrongly
327 //
328 if (gPad->GetMother() != gPad)
329 {
330 w *= gPad->GetMother()->GetAbsWNDC();
331 h *= gPad->GetMother()->GetAbsHNDC();
332 }
333
334 //
335 // Set Range (coordinate system) of pad
336 //
337 gPad->Range(-range, -range, (2*ratio-1)*range, range);
338
339 //
340 // Resize Pad to given ratio
341 //
342 if (h<w)
343 gPad->SetPad((1.-h/w)/2, 0, (h/w+1.)/2, 1);
344 else
345 gPad->SetPad(0, (1.-w/h)/2, 1, (w/h+1.)/2);
346}
347
348void MHCamera::Update(Bool_t islog, Bool_t isbox, Bool_t iscol)
349{
350 Double_t min = GetMinimum(kFALSE);
351 Double_t max = GetMaximum(kFALSE);
352 if (min==FLT_MAX)
353 {
354 min = 0;
355 max = 1;
356 }
357
358 UpdateLegend(min, max, islog);
359
360 MHexagon hex;
361 for (Int_t i=0; i<fNcells-2; i++)
362 {
363 if (IsUsed(i) && iscol)
364 hex.SetFillColor(GetColor(fArray[i+1], min, max, islog));
365 else
366 hex.SetFillColor(10);
367
368 MGeomPix &pix = (*fGeomCam)[i];
369 if (!isbox)
370 hex.PaintHexagon(pix.GetX(), pix.GetY(), pix.GetD());
371 else
372 if (IsUsed(i))
373 {
374 Float_t size = pix.GetD()*(fArray[i+1]-min)/(max-min);
375 if (size>pix.GetD())
376 size=pix.GetD();
377 hex.PaintHexagon(pix.GetX(), pix.GetY(), size);
378 }
379 }
380}
381
382void MHCamera::Print(Option_t *) const
383{
384 cout << "Minimum: " << GetMinimum();
385 if (fMinimum==-1111)
386 cout << " <autoscaled>";
387 cout << endl;
388 cout << "Maximum: " << GetMaximum();
389 if (fMaximum==-1111)
390 cout << " <autoscaled>";
391 cout << endl;
392}
393
394void MHCamera::PaintAxisTitle()
395{
396 Float_t fRange = fGeomCam->GetMaxRadius();
397
398 TLatex *ptitle = new TLatex(1.2*fRange, .97*fRange, GetXaxis()->GetTitle());
399
400 ptitle->SetTextSize(0.03);
401 ptitle->SetTextAlign(33);
402
403 // box with the histogram title
404 ptitle->SetTextColor(gStyle->GetTitleTextColor());
405 ptitle->SetTextFont(gStyle->GetTitleFont(""));
406 ptitle->Paint();
407}
408
409void MHCamera::PaintTitle()
410{
411// *-*-*-*-*-*-*-*-*-*Draw the histogram title*-*-*-*-*-*-*-*-*-*-*-*-*
412// ========================
413 //if (Hoption.Same) return;
414 if (TestBit(kNoTitle))
415 return;
416
417 const Int_t nt = strlen(GetTitle());
418
419 TPaveText *title = (TPaveText*)gPad->FindObject("title");
420 if (nt == 0 || gStyle->GetOptTitle() <= 0)
421 {
422 if (title)
423 delete title;
424 return;
425 }
426
427 Double_t ht = gStyle->GetTitleH();
428 Double_t wt = gStyle->GetTitleW();
429
430 if (ht <= 0)
431 ht = 0.05;
432 if (wt <= 0)
433 {
434 TLatex l;
435 l.SetTextSize(ht);
436 l.SetTitle(GetTitle());
437 Double_t wndc = l.GetXsize()/(gPad->GetX2() - gPad->GetX1());
438 wt = TMath::Min(0.7, 0.02+wndc);
439 }
440 if (title)
441 {
442 TText *t0 = (TText*)title->GetLine(0);
443 if (t0)
444 {
445 if (!strcmp(t0->GetTitle(), GetTitle()))
446 return;
447
448 t0->SetTitle(GetTitle());
449 if (wt > 0)
450 title->SetX2NDC(title->GetX1NDC()+wt);
451 }
452 return;
453 }
454
455 TPaveText *ptitle = new TPaveText(
456 gStyle->GetTitleX(),
457 gStyle->GetTitleY()-ht,
458 gStyle->GetTitleX()+wt,
459 gStyle->GetTitleY(),"blNDC");
460
461 // box with the histogram title
462 ptitle->SetFillColor(gStyle->GetTitleFillColor());
463 ptitle->SetFillStyle(gStyle->GetTitleStyle());
464 ptitle->SetName("title");
465 ptitle->SetBorderSize(gStyle->GetTitleBorderSize());
466 ptitle->SetTextColor(gStyle->GetTitleTextColor());
467 ptitle->SetTextFont(gStyle->GetTitleFont(""));
468 if (gStyle->GetTitleFont("")%10 > 2)
469 ptitle->SetTextSize(gStyle->GetTitleFontSize());
470 ptitle->AddText(GetTitle());
471 ptitle->SetBit(kCanDelete);
472 ptitle->Draw();
473 ptitle->Paint();
474}
475
476// ------------------------------------------------------------------------
477//
478// This is called at any time the canvas should get repainted.
479// Here we maintain an aspect ratio of 1.15. This makes sure,
480// that the camera image doesn't get distorted by resizing the canvas.
481//
482void MHCamera::Paint(Option_t *o)
483{
484 const TString opt(o);
485
486 if (opt.Contains("hist", TString::kIgnoreCase))
487 {
488 Int_t mode = gStyle->GetOptStat();
489 TVirtualPad *save = gPad;
490 gPad=NULL;
491 gStyle->SetOptStat(1000011);
492 gPad=save;
493 TH1D::Paint(o);
494 gPad=NULL;
495 gStyle->SetOptStat(mode);
496 gPad=save;
497 return;
498 }
499
500 // Maintain aspect ratio
501 SetRange();
502
503 Bool_t isbox = opt.Contains("box", TString::kIgnoreCase);
504 Bool_t iscol = isbox ? !opt.Contains("nocol", TString::kIgnoreCase) : 1;
505
506 // Update Contents of the pixels and paint legend
507 Update(gPad->GetLogz(), isbox, iscol);
508
509 // Paint primitives (pixels, color legend, photons, ...)
510 { fPhotons->ForEach(TObject, Paint)(); }
511
512 PaintTitle();
513 PaintAxisTitle();
514}
515
516// ------------------------------------------------------------------------
517//
518// With this function you can change the color palette. For more
519// information see TStyle::SetPalette. Only palettes with 50 colors
520// are allowed.
521// In addition you can use SetPalette(52, 0) to create an inverse
522// deep blue sea palette
523//
524void MHCamera::SetPalette(Int_t ncolors, Int_t *colors)
525{
526 //
527 // If not enough colors are specified skip this.
528 //
529 if (ncolors>1 && ncolors<50)
530 {
531 cout << "MHCamera::SetPalette: Only default palettes with 50 colors are allowed... ignored." << endl;
532 return;
533 }
534
535 //
536 // If ncolors==52 create a reversed deep blue sea palette
537 //
538 if (ncolors==52)
539 {
540 gStyle->SetPalette(51, NULL);
541 TArrayI c(kItemsLegend);
542 for (int i=0; i<kItemsLegend; i++)
543 c[kItemsLegend-i-1] = gStyle->GetColorPalette(i);
544 gStyle->SetPalette(kItemsLegend, c.GetArray());
545 }
546 else
547 gStyle->SetPalette(ncolors, colors);
548
549 fColors.Set(kItemsLegend);
550 for (int i=0; i<kItemsLegend; i++)
551 fColors[i] = gStyle->GetColorPalette(i);
552}
553
554
555void MHCamera::SetPrettyPalette()
556{
557 if (!TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
558 SetPalette(1, 0);
559}
560
561void MHCamera::SetDeepBlueSeaPalette()
562{
563 if (!TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
564 SetPalette(51, 0);
565}
566
567void MHCamera::SetInvDeepBlueSeaPalette()
568{
569 if (!TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
570 SetPalette(52, 0);
571}
572
573void MHCamera::DrawPixelNumbers()
574{
575 for (int i=0; i<kItemsLegend; i++)
576 fColors[i] = 16;
577
578 if (!gPad)
579 Draw();
580
581 TText txt;
582 txt.SetTextFont(122);
583 txt.SetTextAlign(22); // centered/centered
584
585 for (Int_t i=0; i<fNcells-2; i++)
586 {
587 TString num;
588 num += i;
589
590 const MGeomPix &h = (*fGeomCam)[i];
591 TText *nt = txt.DrawText(h.GetX(), h.GetY(), num);
592 nt->SetTextSize(0.3*h.GetD()/fGeomCam->GetMaxRadius());
593 }
594}
595
596// ------------------------------------------------------------------------
597//
598// Call this function to fill the currents
599//
600void MHCamera::AddCamContent(const MCamEvent &event, Int_t type)
601{
602 // FIXME: Security check missing!
603 for (Int_t idx=0; idx<fNcells-2; idx++)
604 {
605 Double_t val=0;
606 if (event.GetPixelContent(val, idx, *fGeomCam, type) && !IsUsed(idx))
607 SetUsed(idx);
608
609 Fill(idx, val); // FIXME: Slow!
610 //fArray[idx+1]+=val;
611 }
612 fEntries++;
613}
614
615// ------------------------------------------------------------------------
616//
617// Call this function to fill the currents
618//
619void MHCamera::AddCamContent(const MHCamera &d, Int_t type)
620{
621 if (fNcells!=d.fNcells)
622 return;
623
624 // FIXME: Security check missing!
625 for (Int_t idx=0; idx<fNcells-2; idx++)
626 if (d.IsUsed(idx))
627 SetUsed(idx);
628
629 switch (type)
630 {
631 case 1:
632 for (Int_t idx=0; idx<fNcells-2; idx++)
633 Fill(idx, d.GetBinError(idx+1));
634 break;
635 case 2:
636 for (Int_t idx=0; idx<fNcells-2; idx++)
637 if (d.GetBinContent(idx+1)!=0)
638 Fill(idx, fabs(d.GetBinError(idx+1)/d.GetBinContent(idx+1)));
639 break;
640 default:
641 for (Int_t idx=0; idx<fNcells-2; idx++)
642 Fill(idx, d.GetBinContent(idx+1));
643 break;
644 }
645 fEntries++;
646}
647
648// ------------------------------------------------------------------------
649//
650// Call this function to fill the currents
651//
652void MHCamera::AddCamContent(const TArrayD &event, Bool_t ispos)
653{
654 for (Int_t idx=0; idx<fNcells-2; idx++)
655 {
656 Fill(idx, event[idx]); // FIXME: Slow!
657 //fArray[idx+1]+=val;
658
659 if (!ispos || fArray[idx+1]>0)
660 SetUsed(idx);
661 }
662 fEntries++;
663}
664
665// ------------------------------------------------------------------------
666//
667// Call this function to fill the currents
668//
669void MHCamera::CntCamContent(const MCamEvent &event, Double_t threshold, Int_t type)
670{
671 // FIXME: Security check missing!
672 for (Int_t idx=0; idx<fNcells-2; idx++)
673 {
674 Double_t val=0;
675 if (event.GetPixelContent(val, idx, *fGeomCam, type) && !IsUsed(idx))
676 SetUsed(idx);
677
678 if (val>threshold)
679 Fill(idx);
680 }
681 fEntries++;
682}
683
684// ------------------------------------------------------------------------
685//
686// Call this function to fill the currents
687//
688void MHCamera::CntCamContent(const TArrayD &event, Double_t threshold, Bool_t ispos)
689{
690 for (Int_t idx=0; idx<fNcells-2; idx++)
691 {
692 if (event[idx]>threshold)
693 Fill(idx);
694
695 if (!ispos || fArray[idx+1]>0)
696 SetUsed(idx);
697 }
698 fEntries++;
699}
700
701void MHCamera::FillRandom()
702{
703 Reset();
704
705 // FIXME: Security check missing!
706 for (Int_t idx=0; idx<fNcells-2; idx++)
707 {
708 Fill(idx, gRandom->Uniform()*fGeomCam->GetPixRatio(idx));
709 SetUsed(idx);
710 }
711 fEntries=1;
712}
713
714
715// ------------------------------------------------------------------------
716//
717// Fill the colors in respect to the cleaning levels
718//
719void MHCamera::FillLevels(const MCerPhotEvt &event, Float_t lvl1, Float_t lvl2)
720{
721 SetCamContent(event, 2);
722
723 for (Int_t i=0; i<fNcells-2; i++)
724 {
725 if (!IsUsed(i))
726 continue;
727
728 if (fArray[i+1]>lvl1)
729 fArray[i+1] = 0;
730 else
731 if (fArray[i+1]>lvl2)
732 fArray[i+1] = 1;
733 else
734 fArray[i+1] = 2;
735 }
736}
737
738// ------------------------------------------------------------------------
739//
740// Fill the colors in respect to the cleaning levels
741//
742void MHCamera::FillLevels(const MCerPhotEvt &event, const MImgCleanStd &clean)
743{
744 FillLevels(event, clean.GetCleanLvl1(), clean.GetCleanLvl2());
745}
746
747// ------------------------------------------------------------------------
748//
749// Show a reflector event. EMarkerStyle is defined in root/include/Gtypes.h
750// To remove the photons from the display call FillRflEvent(NULL)
751//
752void MHCamera::ShowRflEvent(const MRflEvtData *event, EMarkerStyle ms)
753{
754 const Int_t num = event ? event->GetNumPhotons() : 0;
755
756 fPhotons->ExpandCreate(num);
757 if (num < 1)
758 return;
759
760 Int_t i=num-1;
761 do
762 {
763 const MRflSinglePhoton &ph = event->GetPhoton(i);
764 TMarker &m = *static_cast<TMarker*>(fPhotons->UncheckedAt(i));
765 m.SetX(ph.GetX());
766 m.SetY(ph.GetY());
767 m.SetMarkerStyle(ms);
768 } while (i--);
769}
770
771// ------------------------------------------------------------------------
772//
773// Reset the all pixel colors to a default value
774//
775void MHCamera::Reset(Option_t *opt)
776{
777 TH1::Reset(opt);
778
779 for (Int_t i=0; i<fNcells-2; i++)
780 {
781 fArray[i+1]=0;
782 ResetUsed(i);
783 }
784 fArray[0] = 0;
785 fArray[fNcells-1] = 0;
786}
787
788// ------------------------------------------------------------------------
789//
790// Here we calculate the color index for the current value.
791// The color index is defined with the class TStyle and the
792// Color palette inside. We use the command gStyle->SetPalette(1,0)
793// for the display. So we have to convert the value "wert" into
794// a color index that fits the color palette.
795// The range of the color palette is defined by the values fMinPhe
796// and fMaxRange. Between this values we have 50 color index, starting
797// with 0 up to 49.
798//
799Int_t MHCamera::GetColor(Float_t val, Float_t min, Float_t max, Bool_t islog)
800{
801 //
802 // first treat the over- and under-flows
803 //
804 const Int_t maxcolidx = kItemsLegend-1;
805
806 if (val >= max)
807 return fColors[maxcolidx];
808
809 if (val <= min)
810 return fColors[0];
811
812 //
813 // calculate the color index
814 //
815 Float_t ratio;
816 if (islog && min>0)
817 ratio = log10(val/min) / log10(max/min);
818 else
819 ratio = (val-min) / (max-min);
820 const Int_t colidx = (Int_t)(ratio*maxcolidx + .5);
821 return fColors[colidx];
822}
823
824// ------------------------------------------------------------------------
825//
826// Change the text on the legend according to the range of the Display
827//
828void MHCamera::UpdateLegend(Float_t minphe, Float_t maxphe, Bool_t islog)
829{
830 const Float_t range = fGeomCam->GetMaxRadius();
831
832 const Float_t H = 0.9*range;
833 const Float_t h = 2./kItemsLegend;
834 const Float_t offset = 0.04*range;
835
836 const Float_t w = range/sqrt((float)(fNcells-2));
837
838 TBox newbox;
839 TText newtxt;
840 newtxt.SetTextSize(0.025);
841 newtxt.SetTextAlign(12);
842#if ROOT_VERSION_CODE > ROOT_VERSION(3,01,06)
843 newtxt.SetBit(/*kNoContextMenu|*/kCannotPick);
844 newbox.SetBit(/*kNoContextMenu|*/kCannotPick);
845#endif
846
847 for (Int_t i=0; i<kItemsLegend+1; i+=3)
848 {
849 const Float_t pos = (Float_t)i/kItemsLegend;
850
851 Float_t val;
852 if (islog && minphe>0)
853 val = pow(10, log10(maxphe/minphe)*pos) * minphe;
854 else
855 val = minphe + pos * (maxphe-minphe);
856
857 newtxt.PaintText(range+1.5*w, H*(i*h-1)-offset, Form(val<1e6?"%5.1f":"%5.1e", val));
858 }
859
860 for (Int_t i=0; i<kItemsLegend; i++)
861 {
862 newbox.SetFillColor(fColors[i]);
863 newbox.PaintBox(range, H*(i*h-1)-offset, range+w, H*((i+1)*h-1)-offset);
864 }
865
866 TArrow arr;
867 arr.PaintArrow(-range*.9, -range*.9, -range*.6, -range*.9, 0.025);
868 arr.PaintArrow(-range*.9, -range*.9, -range*.9, -range*.6, 0.025);
869
870 TString text;
871 text += (int)(range*.3);
872 text += "mm";
873
874 TText newtxt2;
875 newtxt2.SetTextSize(0.04);
876 newtxt2.PaintText(-range*.85, -range*.85, text);
877
878 text = "";
879 text += (float)((int)(range*.3*fGeomCam->GetConvMm2Deg()*10))/10;
880 text += "\\circ";
881 text = text.Strip(TString::kLeading);
882
883 TLatex latex;
884 latex.PaintLatex(-range*.85, -range*.75, 0, 0.04, text);
885}
886
887// ------------------------------------------------------------------------
888//
889// Save primitive as a C++ statement(s) on output stream out
890//
891void MHCamera::SavePrimitive(ofstream &out, Option_t *opt)
892{
893 cout << "MHCamera::SavePrimitive: Must be rewritten!" << endl;
894 /*
895 if (!gROOT->ClassSaved(TCanvas::Class()))
896 fDrawingPad->SavePrimitive(out, opt);
897
898 out << " " << fDrawingPad->GetName() << "->SetWindowSize(";
899 out << fDrawingPad->GetWw() << "," << fDrawingPad->GetWh() << ");" << endl;
900 */
901}
902
903// ------------------------------------------------------------------------
904//
905// compute the distance of a point (px,py) to the Camera
906// this functions needed for graphical primitives, that
907// means without this function you are not able to interact
908// with the graphical primitive with the mouse!!!
909//
910// All calcutations are running in pixel coordinates
911//
912Int_t MHCamera::DistancetoPrimitive(Int_t px, Int_t py)
913{
914 if (TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
915 return TH1D::DistancetoPrimitive(px, py);
916
917 Int_t dist = 999999;
918
919 for (Int_t i=0; i<fNcells-2; i++)
920 {
921 MHexagon hex((*fGeomCam)[i]);
922 Int_t d = hex.DistancetoPrimitive(px, py);
923
924 if (d<dist)
925 dist=d;
926 }
927 return dist==0?0:999999;
928}
929
930// ------------------------------------------------------------------------
931//
932// Execute a mouse event on the camera
933//
934/*
935 void MHCamera::ExecuteEvent(Int_t event, Int_t px, Int_t py)
936 {
937 cout << "Execute Event Camera " << event << " @ " << px << " " << py << endl;
938 }
939 */
940
941
942// ------------------------------------------------------------------------
943//
944// Function introduced (31-01-03) WILL BE REMOVED IN THE FUTURE! DON'T
945// USE IT!
946//
947void MHCamera::SetPix(const Int_t idx, const Int_t color, Float_t min, Float_t max)
948{
949 fArray[idx+1] = color;
950 SetUsed(idx);
951}
952
953Int_t MHCamera::GetPixelIndex(Int_t px, Int_t py) const
954{
955 Int_t i;
956 for (i=0; i<fNcells-2; i++)
957 {
958 MHexagon hex((*fGeomCam)[i]);
959 if (hex.DistancetoPrimitive(px, py)>0)
960 continue;
961
962 return i;
963 }
964 return -1;
965}
966
967// ------------------------------------------------------------------------
968//
969// Returns string containing info about the object at position (px,py).
970// Returned string will be re-used (lock in MT environment).
971//
972char *MHCamera::GetObjectInfo(Int_t px, Int_t py) const
973{
974 if (TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
975 return TH1D::GetObjectInfo(px, py);
976
977 static char info[128];
978
979 const Int_t idx=GetPixelIndex(px, py);
980
981 if (idx<0)
982 return TObject::GetObjectInfo(px, py);
983
984 sprintf(info, "Software Pixel Index: %d (Hardware Id=%d)", idx, idx+1);
985 return info;
986}
987
988// ------------------------------------------------------------------------
989//
990// Execute a mouse event on the camera
991//
992void MHCamera::ExecuteEvent(Int_t event, Int_t px, Int_t py)
993{
994 if (TString(GetDrawOption()).Contains("hist", TString::kIgnoreCase))
995 {
996 TH1D::ExecuteEvent(event, px, py);
997 return;
998 }
999 //if (event==kMouseMotion && fStatusBar)
1000 // fStatusBar->SetText(GetObjectInfo(px, py), 0);
1001 if (event!=kButton1Down)
1002 return;
1003
1004 const Int_t idx = GetPixelIndex(px, py);
1005 if (idx<0)
1006 return;
1007
1008 cout << "Software Pixel Index: " << idx << endl;
1009 cout << "Hardware Pixel Id: " << idx+1 << endl;
1010 cout << "Contents: " << fArray[idx+1] << " <";
1011 cout << (IsUsed(idx)?"on":"off");
1012 cout << ">" << endl << endl;
1013
1014 if (fNotify && fNotify->GetSize()>0)
1015 new TCanvas;
1016 fNotify->ForEach(MCamEvent, DrawPixelContent)(idx);
1017}
Note: See TracBrowser for help on using the repository browser.