source: trunk/MagicSoft/Mars/mhist/MH3.cc@ 1481

Last change on this file since 1481 was 1481, checked in by tbretz, 23 years ago
*** empty log message ***
File size: 11.1 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 2002 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2002
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MH3
28//
29// With this histogram you can fill a histogram with up to three
30// variables from Mars parameter containers in an eventloop.
31//
32// In the constructor you can give up to three variables which should be
33// filled in the histogram. Dependend on the number of given variables
34// (data members) a TH1F, TH2F or TH3F is created.
35// Specify the data mamber like the following:
36// "MHillas.fLength"
37// Where MHillas is the name of the parameter container in the parameter
38// list and fLength is the name of the data member which should be filled
39// in the histogram. Assuming that your MHillas container has a different
40// name (MyHillas) the name to give would be:
41// "MyHillas.fLength"
42//
43// The axis binning is retrieved from the parameter list, too. Create a
44// MBinning with the name "Binning" plus the name of your MH3 container
45// plus the axis name ("X", "Y" or "Z") and add it to the parameter list.
46//
47// If you want to use a different unit for histogramming use SetScaleX,
48// SetScaleY and SetScaleZ.
49//
50// For example:
51// MH3 myhist("MHillas.fLength");
52// myhist.SetName("MyHist");
53// myhist.SetScaleX(geomcam.GetConvMm2Deg()); //convert length to degree
54// MBinning bins("BinningMyHistX");
55// bins.SetEdges(10, 0, 150);
56// plist.AddToList(&myhist);
57// plist.AddToList(&bins);
58//
59/////////////////////////////////////////////////////////////////////////////
60#include "MH3.h"
61
62#include <fstream.h>
63
64#include <TH2.h>
65#include <TH3.h>
66#include <TProfile.h>
67#include <TProfile2D.h>
68#include <TPad.h>
69#include <TCanvas.h>
70
71#include "MLog.h"
72#include "MLogManip.h"
73
74#include "MParList.h"
75
76#include "MDataChain.h"
77
78ClassImp(MH3);
79
80MH3::MH3() : fDimension(0), fHist(NULL)
81{
82 fName = "MH3";
83 fTitle = "Container for a 1D Mars Histogram";
84
85 fData[0] = fData[1] = fData[2] = NULL;
86 fScale[0] = fScale[1] = fScale[2] = 1;
87}
88
89// --------------------------------------------------------------------------
90//
91// Creates an TH1F. memberx is filled into the X-bins. For a more detailed
92// description see the class description above.
93//
94MH3::MH3(const char *memberx)
95 : fDimension(1)
96{
97 fHist = new TH1F;
98
99 fData[0] = new MDataChain(memberx);
100 fData[1] = NULL;
101 fData[2] = NULL;
102
103 fName = "MH3";
104 fTitle = "Container for a 1D Mars Histogram";
105
106 fHist->SetDirectory(NULL);
107 fHist->SetYTitle("Counts");
108
109 fScale[0] = 1;
110 fScale[1] = 1;
111 fScale[2] = 1;
112}
113
114// --------------------------------------------------------------------------
115//
116// Creates an TH2F. memberx is filled into the X-bins. membery is filled
117// into the Y-bins. For a more detailed description see the class
118// description above.
119//
120MH3::MH3(const char *memberx, const char *membery)
121 : fDimension(2)
122{
123 fHist = new TH2F;
124
125 fData[0] = new MDataChain(memberx);
126 fData[1] = new MDataChain(membery);
127 fData[2] = NULL;
128
129 fName = "MH3";
130 fTitle = "Container for a 2D Mars Histogram";
131
132 fHist->SetDirectory(NULL);
133 fHist->SetZTitle("Counts");
134
135 fScale[0] = 1;
136 fScale[1] = 1;
137 fScale[2] = 1;
138}
139
140// --------------------------------------------------------------------------
141//
142// Creates an TH3F. memberx is filled into the X-bins. membery is filled
143// into the Y-bins. membery is filled into the Z-bins. For a more detailed
144// description see the class description above.
145//
146MH3::MH3(const char *memberx, const char *membery, const char *memberz)
147 : fDimension(3)
148{
149 fHist = new TH3F;
150
151 fData[0] = new MDataChain(memberx);
152 fData[1] = new MDataChain(membery);
153 fData[2] = new MDataChain(memberz);
154
155 fName = "MH3";
156 fTitle = "Container for a 3D Mars Histogram";
157
158 fHist->SetDirectory(NULL);
159
160 fScale[0] = 1;
161 fScale[1] = 1;
162 fScale[2] = 1;
163}
164
165// --------------------------------------------------------------------------
166//
167// Deletes the histogram
168//
169MH3::~MH3()
170{
171 delete fHist;
172
173 for (int i=0; i<3; i++)
174 if (fData[i])
175 delete fData[i];
176}
177
178// --------------------------------------------------------------------------
179//
180// Setup the Binning for the histograms automatically if the correct
181// instances of MBinning are found in the parameter list
182// For a more detailed description see class description above.
183//
184Bool_t MH3::SetupFill(const MParList *plist)
185{
186 TString bname("Binning");
187 bname += fName;
188
189 MBinning *binsx = NULL;
190 MBinning *binsy = NULL;
191 MBinning *binsz = NULL;
192 switch (fDimension)
193 {
194 case 3:
195 binsz = (MBinning*)plist->FindObject(bname+"Z");
196 if (!binsz)
197 {
198 *fLog << err << dbginf << "MBinning '" << bname << "X' not found... aborting." << endl;
199 return kFALSE;
200 }
201 fHist->SetZTitle(fData[2]->GetTitle());
202 if (!fData[2]->PreProcess(plist))
203 return kFALSE;
204 case 2:
205 binsy = (MBinning*)plist->FindObject(bname+"Y");
206 if (!binsy)
207 {
208 *fLog << err << dbginf << "MBinning '" << bname << "Y' not found... aborting." << endl;
209 return kFALSE;
210 }
211 fHist->SetYTitle(fData[1]->GetTitle());
212 if (!fData[1]->PreProcess(plist))
213 return kFALSE;
214 case 1:
215 binsx = (MBinning*)plist->FindObject(bname+"X");
216 if (!binsx)
217 {
218 *fLog << err << dbginf << "MBinning '" << bname << "X' not found... aborting." << endl;
219 return kFALSE;
220 }
221 fHist->SetXTitle(fData[0]->GetTitle());
222 if (!fData[0]->PreProcess(plist))
223 return kFALSE;
224 }
225
226 fHist->SetName(fName);
227
228 TString title("Histogram for ");
229 title += fName;
230
231 switch (fDimension)
232 {
233 case 1:
234 fHist->SetTitle(title+" (1D)");
235 SetBinning(fHist, binsx);
236 return kTRUE;
237 case 2:
238 fHist->SetTitle(title+" (2D)");
239 SetBinning((TH2*)fHist, binsx, binsy);
240 return kTRUE;
241 case 3:
242 fHist->SetTitle(title+" (3D)");
243 SetBinning((TH3*)fHist, binsx, binsy, binsz);
244 return kTRUE;
245 }
246
247 return kTRUE;
248}
249
250// --------------------------------------------------------------------------
251//
252// Set the name of the histogram ant the MH3 container
253//
254void MH3::SetName(const char *name)
255{
256 fHist->SetName(name);
257 MParContainer::SetName(name);
258}
259
260// --------------------------------------------------------------------------
261//
262// Set the title of the histogram ant the MH3 container
263//
264void MH3::SetTitle(const char *title)
265{
266 fHist->SetTitle(title);
267 MParContainer::SetTitle(title);
268}
269
270// --------------------------------------------------------------------------
271//
272// Fills the one, two or three data members into our histogram
273//
274Bool_t MH3::Fill(const MParContainer *par)
275{
276 Double_t x=0;
277 Double_t y=0;
278 Double_t z=0;
279
280 switch (fDimension)
281 {
282 case 3:
283 z = fData[2]->GetValue()*fScale[2];
284 case 2:
285 y = fData[1]->GetValue()*fScale[1];
286 case 1:
287 x = fData[0]->GetValue()*fScale[0];
288 }
289
290 switch (fDimension)
291 {
292 case 3:
293 ((TH3*)fHist)->Fill(x, y, z);
294 return kTRUE;
295 case 2:
296 ((TH2*)fHist)->Fill(x, y);
297 return kTRUE;
298 case 1:
299 fHist->Fill(x);
300 return kTRUE;
301 }
302
303 return kFALSE;
304}
305
306// --------------------------------------------------------------------------
307//
308// Draw clone of histogram. So that the object can be deleted
309// and the histogram is still visible in the canvas.
310// The cloned object are deleted together with the canvas if the canvas is
311// destroyed. If you want to handle destroying the canvas you can get a
312// pointer to it from this function
313//
314TObject *MH3::DrawClone(Option_t *opt) const
315{
316 TCanvas &c = *MH::MakeDefCanvas(fHist);
317
318 //
319 // This is necessary to get the expected bahviour of DrawClone
320 //
321 gROOT->SetSelectedPad(NULL);
322
323 fHist->DrawCopy(opt);
324
325 TString str(opt);
326 if (str.Contains("PROFX", TString::kIgnoreCase) && fDimension==2)
327 {
328 TProfile *p = ((TH2*)fHist)->ProfileX();
329 p->Draw("same");
330 p->SetBit(kCanDelete);
331 p->SetDirectory(NULL);
332 }
333 if (str.Contains("PROFY", TString::kIgnoreCase) && fDimension==2)
334 {
335 TProfile *p = ((TH2*)fHist)->ProfileY();
336 p->Draw("same");
337 p->SetBit(kCanDelete);
338 p->SetDirectory(NULL);
339 }
340
341 c.Modified();
342 c.Update();
343
344 return &c;
345}
346
347// --------------------------------------------------------------------------
348//
349// Creates a new canvas and draws the histogram into it.
350// Be careful: The histogram belongs to this object and won't get deleted
351// together with the canvas.
352//
353void MH3::Draw(Option_t *opt)
354{
355 if (!gPad)
356 MH::MakeDefCanvas(fHist);
357
358 fHist->Draw(opt);
359
360 TString str(opt);
361 if (str.Contains("PROFX", TString::kIgnoreCase) && fDimension==2)
362 {
363 TProfile *p = ((TH2*)fHist)->ProfileX();
364 p->Draw("same");
365 p->SetBit(kCanDelete);
366 p->SetDirectory(NULL);
367 }
368 if (str.Contains("PROFY", TString::kIgnoreCase) && fDimension==2)
369 {
370 TProfile *p = ((TH2*)fHist)->ProfileY();
371 p->Draw("same");
372 p->SetBit(kCanDelete);
373 p->SetDirectory(NULL);
374 }
375
376 gPad->Modified();
377 gPad->Update();
378}
379
380// --------------------------------------------------------------------------
381//
382// Implementation of SavePrimitive. Used to write the call to a constructor
383// to a macro. In the original root implementation it is used to write
384// gui elements to a macro-file.
385//
386void MH3::StreamPrimitive(ofstream &out) const
387{
388 TString name = ToLower(fName);
389
390 out << " MH3 " << name << "(\"";
391 out << fData[0]->GetRule() << "\"";
392 if (fDimension>1)
393 out << ", \"" << fData[1]->GetRule() << "\"";
394 if (fDimension>2)
395 out << ", \"" << fData[2]->GetRule() << "\"";
396
397 out << ");" << endl;
398
399 out << " " << name << ".SetName(\"" << fName << "\");" << endl;
400 out << " " << name << ".SetTitle(\"" << fTitle << "\");" << endl;
401
402 switch (fDimension)
403 {
404 case 3:
405 if (fScale[2]!=1)
406 out << " " << name << ".SetScaleZ(" << fScale[2] << ");" << endl;
407 case 2:
408 if (fScale[1]!=1)
409 out << " " << name << ".SetScaleY(" << fScale[1] << ");" << endl;
410 case 1:
411 if (fScale[0]!=1)
412 out << " " << name << ".SetScaleX(" << fScale[0] << ");" << endl;
413 }
414}
Note: See TracBrowser for help on using the repository browser.