source: trunk/MagicSoft/Mars/mhistmc/MHMcCollectionArea.cc@ 2075

Last change on this file since 2075 was 2075, checked in by moralejo, 23 years ago
*** empty log message ***
File size: 9.6 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz 12/2000 <mailto:tbretz@astro.uni-wuerzburg.de>
19! Author(s): Harald Kornmayer 1/2001
20!
21! Copyright: MAGIC Software Development, 2000-2002
22!
23!
24\* ======================================================================== */
25
26//////////////////////////////////////////////////////////////////////////////
27// //
28// MHMcCollectionArea //
29// //
30//////////////////////////////////////////////////////////////////////////////
31
32#include "MHMcCollectionArea.h"
33
34#include <TH2.h>
35#include <TCanvas.h>
36
37#include "MH.h"
38#include "MBinning.h"
39#include "MHMcEfficiency.h"
40#include "MHMcEnergyImpact.h"
41
42#include "MLog.h"
43#include "MLogManip.h"
44
45ClassImp(MHMcCollectionArea);
46
47// --------------------------------------------------------------------------
48//
49// Creates the three necessary histograms:
50// - selected showers (input)
51// - all showers (input)
52// - collection area (result)
53//
54MHMcCollectionArea::MHMcCollectionArea(const char *name, const char *title)
55{
56 // initialize the histogram for the distribution r vs E
57 //
58 // we set the energy range from 10 Gev to 20000 GeV (in log 5 orders
59 // of magnitude) and for each order we take 10 subdivision --> 50 xbins
60 //
61 // we set the radius range from 0 m to 500 m with 10 m bin --> 50 ybins
62 //
63 fName = name ? name : "MHMcCollectionArea";
64 fTitle = title ? title : "Collection Area vs. Energy";
65
66 MBinning binsx;
67 MBinning binsy;
68 binsx.SetEdgesLog(50, 10, 20000);
69 binsy.SetEdges (50, 0, 500);
70
71 fHistAll = new TH2D;
72 fHistSel = new TH2D;
73 fHistCol = new TH1D;
74
75 MH::SetBinning(fHistAll, &binsx, &binsy);
76 MH::SetBinning(fHistSel, &binsx, &binsy);
77
78 fHistCol->SetName(fName);
79 fHistAll->SetName("AllEvents");
80 fHistSel->SetName("SelectedEvents");
81
82 fHistCol->SetTitle(fTitle);
83 fHistAll->SetTitle("All showers - Radius vs Energy distribution");
84 fHistSel->SetTitle("Selected showers - Radius vs Energy distribution");
85
86 fHistAll->SetDirectory(NULL);
87 fHistSel->SetDirectory(NULL);
88 fHistCol->SetDirectory(NULL);
89
90 fHistAll->SetXTitle("E [GeV]");
91 fHistAll->SetYTitle("r [m]");
92 fHistAll->SetZTitle("N");
93
94 fHistSel->SetXTitle("E [GeV]");
95 fHistSel->SetYTitle("r [m]");
96 fHistSel->SetYTitle("N");
97
98 fHistCol->SetXTitle("E [GeV]");
99 fHistCol->SetYTitle("A [m^{2}]");
100}
101
102// --------------------------------------------------------------------------
103//
104// Delete the three histograms
105//
106MHMcCollectionArea::~MHMcCollectionArea()
107{
108 delete fHistAll;
109 delete fHistSel;
110 delete fHistCol;
111}
112
113// --------------------------------------------------------------------------
114//
115// Fill data into the histogram which contains all showers
116//
117void MHMcCollectionArea::FillAll(Double_t energy, Double_t radius)
118{
119 fHistAll->Fill(energy, radius);
120}
121
122// --------------------------------------------------------------------------
123//
124// Fill data into the histogram which contains the selected showers
125//
126void MHMcCollectionArea::FillSel(Double_t energy, Double_t radius)
127{
128 fHistSel->Fill(energy, radius);
129}
130
131// --------------------------------------------------------------------------
132//
133// Draw the histogram with all showers
134//
135void MHMcCollectionArea::DrawAll(Option_t* option)
136{
137 if (!gPad)
138 MH::MakeDefCanvas(fHistAll);
139
140 fHistAll->Draw(option);
141
142 gPad->SetLogx();
143
144 gPad->Modified();
145 gPad->Update();
146}
147
148// --------------------------------------------------------------------------
149//
150// Draw the histogram with the selected showers only.
151//
152void MHMcCollectionArea::DrawSel(Option_t* option)
153{
154 if (!gPad)
155 MH::MakeDefCanvas(fHistSel);
156
157 fHistSel->Draw(option);
158
159 gPad->SetLogx();
160
161 gPad->Modified();
162 gPad->Update();
163}
164
165void MHMcCollectionArea::Draw(Option_t* option)
166{
167 TVirtualPad *pad = gPad ? gPad : MakeDefCanvas(this);
168 pad->SetBorderMode(0);
169
170 fHistCol->Draw(option);
171
172 pad->SetLogx();
173
174 pad->Modified();
175 pad->Update();
176}
177
178// --------------------------------------------------------------------------
179//
180// Calculate the Efficiency (collection area) and set the 'ReadyToSave'
181// flag
182//
183void MHMcCollectionArea::CalcEfficiency()
184{
185 Calc(*fHistSel, *fHistAll);
186}
187
188// --------------------------------------------------------------------------
189//
190// Calculate the Efficiency (collection area) and set the 'ReadyToSave'
191// flag
192//
193void MHMcCollectionArea::Calc(const MHMcEnergyImpact &mcsel, const MHMcEnergyImpact &mcall)
194{
195 Calc((TH2D&)*mcsel.GetHist(), (TH2D&)*mcall.GetHist());
196}
197
198// --------------------------------------------------------------------------
199//
200// Calculate the Efficiency (collection area) and set the 'ReadyToSave'
201// flag
202//
203void MHMcCollectionArea::Calc(TH2D &hsel, TH2D &hall)
204{
205 MH::SetBinning(fHistCol, hsel.GetXaxis());
206
207 fHistCol->Sumw2();
208
209 TH1D &psel = *hsel.ProjectionX();
210 TH1D &pall = *hall.ProjectionX();
211
212 TH1D &pally = *hall.ProjectionY();
213
214 Double_t max = 0.;
215 for (Int_t i = hall.GetNbinsY(); i > 0; i--)
216 if (pally.GetBinContent(i) > 0)
217 {
218 max = pally.GetBinLowEdge(i+1);
219 break;
220 }
221
222 fHistCol->Divide(&psel, &pall, TMath::Pi()*max*max, 1);
223 fHistCol->SetEntries(hsel.GetEntries());
224
225 delete &psel;
226 delete &pall;
227
228 SetReadyToSave();
229}
230
231// --------------------------------------------------------------------------
232//
233// Calculate the Efficiency (collection area) and set the 'ReadyToSave'
234// flag
235//
236void MHMcCollectionArea::CalcEfficiency(UInt_t numevts, Float_t emin, Float_t emax, Float_t index)
237{
238 // Here we estimate the total number of showers in each energy bin
239 // known the energy range and spectral index of the generated showers
240 // (set now by hand since the info is not available in the header!)
241
242 TH1D &histsel = *fHistSel->ProjectionX();
243
244 TH1D histall;
245
246 TAxis &xaxis = *histsel.GetXaxis();
247
248 MH::SetBinning(&histall, &xaxis);
249 MH::SetBinning(fHistCol, &xaxis);
250
251 const Float_t expo = 1+index;
252
253 const Float_t k = (Float_t)numevts / (pow(emax,expo) - pow(emin,expo));
254
255 const Int_t nbinx = xaxis.GetNbins();
256
257 for (Int_t i=1; i<=nbinx; i++)
258 {
259 const Float_t e1 = histall.GetBinLowEdge(i);
260 const Float_t e2 = histall.GetBinLowEdge(i+1);
261
262 if (e1 < emin || e2 > emax)
263 continue;
264
265 const Float_t events = k * (pow(e2, expo) - pow(e1, expo));
266
267 histall.SetBinContent(i, events);
268 histall.SetBinError(i, sqrt(events));
269
270 }
271
272 // -----------------------------------------------------------
273 //
274 // Impact parameter range: TO BE FIXED! Impact parameter shoud be
275 // read from run header, but it is not yet in!!
276 //
277 const Float_t r1 = 0;
278 const Float_t r2 = 300;
279
280 *fLog << warn << endl << dbginf << "WARNING! I will assume a maximum impact parameter of 300 meters for the MC events. Check that this is the true one!" <<endl<<endl;
281 const Float_t area = TMath::Pi() * (r2*r2 - r1*r1);
282
283 for (Int_t ix=1; ix<=nbinx; ix++)
284 {
285 const Float_t Na = histall.GetBinContent(ix);
286
287 if (Na <= 0)
288 continue;
289
290 const Float_t Ns = histsel.GetBinContent(ix);
291
292 // Since Na is an estimate of the total number of showers generated
293 // in the energy bin, it may happen that Ns (triggered showers) is
294 // larger than Na. In that case, the bin is skipped:
295
296 if (Na < Ns)
297 continue;
298
299 const Double_t eff = Ns/Na;
300
301 const Double_t efferr = sqrt((1.-eff)*Ns)/Na;
302
303 fHistCol->SetBinContent(ix, eff*area);
304 fHistCol->SetBinError(ix, efferr*area);
305 }
306
307 delete &histsel;
308
309 SetReadyToSave();
310}
311
312// --------------------------------------------------------------------------
313//
314// Calculate the Efficiency (collection area) and set the 'ReadyToSave'
315// flag
316//
317void MHMcCollectionArea::Calc(const MHMcEfficiency &heff)
318{
319 //
320 // now calculate the Collection area for different
321 // energies
322 //
323 TH2D &h = (TH2D&)*heff.GetHist();
324
325 MH::SetBinning(fHistCol, h.GetXaxis());
326
327 const Int_t nbinx = h.GetXaxis()->GetNbins();
328 const Int_t nbiny = h.GetYaxis()->GetNbins();
329
330 for (Int_t ix=1; ix<=nbinx; ix++)
331 {
332 Double_t errA = 0;
333 Double_t colA = 0;
334
335 for (Int_t iy=1; iy<=nbiny; iy++)
336 {
337 TAxis *yaxis = h.GetYaxis();
338
339 const Double_t r1 = yaxis->GetBinLowEdge(iy);
340 const Double_t r2 = yaxis->GetBinLowEdge(iy+1);
341
342 const Double_t A = TMath::Pi() * (r2*r2 - r1*r1);
343
344 const Double_t eff = h.GetCellContent(ix, iy);
345 const Double_t efferr = h.GetCellError(ix, iy);
346
347 colA += eff*A;
348 errA += A*A * efferr*efferr;
349 }
350
351 fHistCol->SetBinContent(ix, colA);
352 fHistCol->SetBinError(ix, sqrt(errA));
353 }
354
355 SetReadyToSave();
356}
Note: See TracBrowser for help on using the repository browser.