test_effArea.C

Deil Christoph, 10/10/2012 11:46 AM

Download (15.9 KB)

 
1

    
2
#include <response/EffectiveAreaLookupIR.hh>
3
#include <utilities/StringTools.hh>
4
#include <muon/ArrayMuonParameters.hh>
5
#include <sash/HESSArray.hh>
6

    
7
#include <TH1.h>
8
#include <TCanvas.h>
9
#include <TLine.h>
10
#include <TGraph.h>
11
#include <TF1.h>
12
#include <TLegend.h>
13

    
14
#include <iostream>
15
#include <sstream>
16

    
17

    
18
int test_effArea() {
19

    
20
  // standard tests for fixed optical configuration
21

    
22
  Response::EffectiveAreaLookupIR lookup( "std", false );
23

    
24
  int muonEntry        = 100;
25
  int telescopePattern = 30;
26
  double azimuth       = 197.467;
27
  double zenith        = 29.9336;
28
  double offset        = 0.450788;
29
  double energy        = 0.404803;
30

    
31
  // lookup.PrintLookupArrays();
32

    
33
  double effArea;
34
  bool success = lookup.InterpolateEffectiveArea(energy,zenith,offset,muonEntry,telescopePattern,azimuth,effArea);
35

    
36
  std::cout << "EffArea: " << effArea << " success: " << success << std::endl;
37

    
38
  double thr,emax;
39
  lookup.GetRange(zenith,offset,muonEntry,telescopePattern,azimuth,thr,emax);
40

    
41
  std::cout << "Range: " << thr << " .. " << emax << std::endl;
42

    
43
  return 0;
44
}
45

    
46
int test_effArea_opticalConfiguration(){
47

    
48
  // test interpolation based on optical efficiencies
49

    
50
  Response::EffectiveAreaLookupIR lookup( "std", false );
51

    
52
  int telescopePattern = 30;
53
  double azimuth       = 359.704;
54
  double zenith        = 45.2264;
55
  double offset        = 0.446313;
56
  double energy        = 0.541777;
57

    
58
  Muon::ArrayMuonParameters ap(Sash::HESSArray::GetHESSArray());
59
  ap.GetMcCorrectionFactorForSecondMuonEntry()  = 1.172;
60
  ap.GetDataCorrectionFactorForFirstMuonEntry() = 1.20635;
61
  ap.GetFirstMuonEntry()   = 101;
62
  ap.GetSecondMuonEntry()  = 102;
63
  ap.GetTelescopePattern() = telescopePattern;
64

    
65
  double spectralIndex = 2.5;
66
  double E1 = 1.0;
67
  double E2 = 100.0;
68
  double livetime = 1.0;
69

    
70
  double effArea;
71
  bool success = lookup.InterpolateEffectiveArea(&ap,energy,zenith,offset,azimuth,effArea);
72

    
73
  std::cout << "EffArea: " << effArea << " success: " << success << std::endl;
74

    
75
  double thr,emax;
76
  lookup.GetRange(&ap,zenith,offset,azimuth,thr,emax);
77

    
78
  std::cout << "Range: " << thr << " .. " << emax << std::endl;
79
  std::cout << "Energy was: " << energy << std::endl;
80

    
81
  int nbinsfailed;
82
  double xi = lookup.CalculateExpectedCounts(&ap,spectralIndex,E1,E2,zenith,offset,azimuth,livetime,nbinsfailed);
83

    
84
  std::cout << "Expected counts: " << xi << " (" << nbinsfailed << " bins failed)" << std::endl;
85

    
86
  return 0;
87
}
88

    
89

    
90
//
91
// Plot effective area as a function of the parameter given as first argument.
92
//
93
// example: plot_effArea(3,"muonRun=101,telPattern=28,azimuth=0.,offset=0.2,energy=40.0")
94
//          plot_effArea(1,"muonRun=101,azimuth=0.,zenith=8.3,offset=0.2,energy=40.0")
95
//          plot_effArea(5,"muonRun=101,azimuth=0.,zenith=8.3,offset=0.2,telPattern=28") (also plots threshold!)
96
//
97
TH1F* plot_effArea( unsigned int parameterToVary, const char* parameters = "" ){
98

    
99
  Response::EffectiveAreaLookupIR lookup( "std", true );
100

    
101
  std::string parstr(parameters);
102

    
103
  bool success = true;
104

    
105
  int muonRun      = ValueFromString(parstr,"muonRun",success);
106
  int telPattern   = ValueFromString(parstr,"telPattern",success);
107
  double azimuth   = ValueFromString(parstr,"azimuth",success);
108
  double zenith    = ValueFromString(parstr,"zenith",success);
109
  double offset    = ValueFromString(parstr,"offset",success);
110
  double energy    = ValueFromString(parstr,"energy",success);
111

    
112
  int nbins = 100;
113
  double xmin = 0.;
114
  double xmax = 0.;
115
  bool logx = false;
116

    
117
  switch( parameterToVary ){
118

    
119
  case 0: // optical efficiency
120
    nbins = 3;
121
    xmin = 100.;
122
    xmax = 103.;
123
    break;
124
  case 1: // telPattern
125
    nbins = 21;
126
    xmin = 11.5;
127
    xmax = 32.5;
128
    break;
129
  case 2: // azimuth
130
    nbins = 90;
131
    xmin = 0.;
132
    xmax = 360.;
133
    break;
134
  case 3: // zenith
135
    nbins = 70;
136
    xmin = 0.;
137
    xmax = 70.;
138
    break;
139
  case 4: // offset
140
    nbins = 50;
141
    xmin = 0.;
142
    xmax = 3.0;
143
    break;
144
  case 5: // energy
145
    nbins = 100;
146
    xmin = 0.1;
147
    xmax = 20.0;
148
    logx = true;
149
    break;
150
  default:
151
    return 0;
152
    break;
153
  }
154

    
155
  TH1F* histo = lookup.CalculateInterpolatedHistogram( energy, zenith, offset, muonRun, telPattern, azimuth, parameterToVary,
156
                                                       nbins, xmin, xmax, logx );
157

    
158
  histo->SetStats(false);
159

    
160
  TCanvas* c = new TCanvas( "c", "effArea", 900, 700 );
161
  c->cd();
162

    
163
  histo->Draw();
164

    
165
  if(parameterToVary==5){
166
    double threshold, energymax;
167
    lookup.GetRange( zenith, offset, 
168
                     muonRun,
169
                     telPattern, azimuth,
170
                     threshold, energymax );
171

    
172
    TLine* thrLine = new TLine(threshold,histo->GetMinimum(),
173
                               threshold,histo->GetMaximum()*1.05);
174
    thrLine->SetLineColor(2);
175
    thrLine->SetLineStyle(2);
176

    
177
    thrLine->Draw();
178
  }
179

    
180
  if( logx )
181
    gPad->SetLogx();
182

    
183
  return histo;
184
}
185

    
186

    
187
int compare_thresholds() {
188

    
189
  Response::EffectiveAreaLookupIR lookup( "std", true );
190

    
191
  int telPattern   = 30;
192
  double azimuth   = 180.;
193
  double zenith    = 20.;
194
  double offset    = 0.5;
195
  double dummy = 0.;
196

    
197
  int nbins = 400;
198
  double xmin = 0.1;
199
  double xmax = 20.0;
200
  bool logx = true;
201

    
202
  double ymin = 1.e4;
203
  double ymax = 3.e5;
204

    
205
  int muonRun = 0;
206
  double threshold, energymax;
207

    
208
  TH1F* histo100 = lookup.CalculateInterpolatedHistogram( dummy, zenith, offset,
209
                                                          muonRun = 100,
210
                                                          telPattern, azimuth, 5,
211
                                                          nbins, xmin, xmax, logx );
212
  histo100->SetLineColor(1);
213
  lookup.GetRange( zenith, offset, 
214
                   muonRun,
215
                   telPattern, azimuth,
216
                   threshold, energymax );
217
  TLine* thrLine100 = new TLine(threshold,ymin,
218
                                threshold,ymax*1.05);
219
  thrLine100->SetLineColor(1);
220
  thrLine100->SetLineWidth(2);
221
  std::cout << "Thr 100: " << threshold << std::endl;
222

    
223
  TH1F* histo101 = lookup.CalculateInterpolatedHistogram( dummy, zenith, offset,
224
                                                          muonRun = 101,
225
                                                          telPattern, azimuth, 5,
226
                                                          nbins, xmin, xmax, logx );
227
  histo101->SetLineColor(2);
228
  lookup.GetRange( zenith, offset, 
229
                   muonRun,
230
                   telPattern, azimuth,
231
                   threshold, energymax );
232
  TLine* thrLine101 = new TLine(threshold,ymin,
233
                                threshold,ymax*1.05);
234
  thrLine101->SetLineColor(2);
235
  thrLine101->SetLineWidth(2);
236
  std::cout << "Thr 101: " << threshold << std::endl;
237

    
238
  double corrFactor101 = 1.454;
239
  TLine* thrLine101Comparison = new TLine(corrFactor101*thrLine100->GetX1(),ymin,
240
                                          corrFactor101*thrLine100->GetX2(),ymax*1.05);
241
  thrLine101Comparison->SetLineColor(2);
242
  thrLine101Comparison->SetLineStyle(2);
243

    
244

    
245
  TH1F* histo102 = lookup.CalculateInterpolatedHistogram( dummy, zenith, offset,
246
                                                          muonRun = 102,
247
                                                          telPattern, azimuth, 5,
248
                                                          nbins, xmin, xmax, logx );
249
  histo102->SetLineColor(3);
250
  lookup.GetRange( zenith, offset, 
251
                   muonRun,
252
                   telPattern, azimuth,
253
                   threshold, energymax );
254
  TLine* thrLine102 = new TLine(threshold,ymin,
255
                                threshold,ymax*1.05);
256
  thrLine102->SetLineColor(3);
257
  thrLine102->SetLineWidth(2);
258
  std::cout << "Thr 102: " << threshold << std::endl;
259

    
260
  double corrFactor102 = 1.17;
261
  TLine* thrLine102Comparison = new TLine(corrFactor102*thrLine101->GetX1(),ymin,
262
                                          corrFactor102*thrLine101->GetX2(),ymax*1.05);
263
  thrLine102Comparison->SetLineColor(3);
264
  thrLine102Comparison->SetLineStyle(2);
265

    
266

    
267

    
268
  TCanvas* thrCanvas = new TCanvas( "thrCanvas", "thresholds", 1100, 800 );
269
  thrCanvas->cd();
270

    
271
  histo100->Draw("HIST");
272
  histo100->SetMinimum(ymin);
273
  histo100->SetMaximum(ymax);
274
  thrLine100->Draw();
275
  histo101->Draw("HIST SAME");
276
  thrLine101->Draw();
277
  histo102->Draw("HIST SAME");
278
  thrLine102->Draw();
279

    
280
  thrLine101Comparison->Draw();
281
  thrLine102Comparison->Draw();
282

    
283
  gPad->SetLogx();
284
  gPad->SetLogy();
285

    
286
  return 0;
287
}
288

    
289
int comparePatterns(){
290

    
291
  TH1F* histo = plot_effArea(1,"muonRun=103,azimuth=0.,zenith=25.,offset=0.5,energy=1.0");
292
//   TH1F* histo = plot_effArea(1,"muonRun=103,azimuth=0.,zenith=45.,offset=0.5,energy=0.8");
293
  TH1F* dhisto = (TH1F*)histo->Clone("dhisto");
294
  dhisto->Reset();
295

    
296
  int patterns[5] = { 14, 22, 26, 28, 30 };
297
  for( int i=0 ; i<5 ; ++i ){
298
    int bin = histo->FindBin(patterns[i]);
299
    dhisto->SetBinContent(bin,histo->GetBinContent(bin));
300
    dhisto->SetBinError(bin,histo->GetBinError(bin));
301
  }
302

    
303
  dhisto->Draw();
304

    
305
  return 0;
306
}
307

    
308

    
309
int compareExpectedCounts(){
310

    
311
  Response::EffectiveAreaLookupIR lookup( "std", false );
312

    
313
  TH1F* histo = new TH1F( "histo", "expected counts;pattern;expected counts / a.u.", 25, 10.5, 35.5 );
314

    
315
  int muonEntry        = 103;
316
  double azimuth       = 0.;
317
  double zenith        = 55.;
318
  double offset        = 0.5;
319
  double spectralIndex = 2.5;
320
  double E2            = 90.;
321

    
322
  int telPatterns[5] = { 14, 22, 26, 28, 30 };
323
  for( int i=0 ; i<5 ; ++i ){
324

    
325
    int pattern = telPatterns[i];
326

    
327
    double thr,emax;
328
    int nBinsFailed;
329
    lookup.GetRange(zenith,offset,muonEntry,pattern,azimuth,thr,emax);
330

    
331
    std::cout << "Pattern: " << pattern << " Range: " << thr << " .. " << emax << std::endl;
332

    
333
    double expCounts = lookup.CalculateExpectedCounts( spectralIndex, thr, E2,
334
                                                       zenith, offset, azimuth,
335
                                                       muonEntry, pattern,
336
                                                       1., nBinsFailed );
337
    std::cout << "Exp counts: " << expCounts << std::endl;
338
    histo->Fill(pattern,expCounts);
339
  }
340

    
341
  TCanvas* c = new TCanvas( "expCountsCanvas", "expected counts", 1100, 800 );
342
  c->cd();
343

    
344
  histo->SetStats(0);
345
  histo->Draw();
346

    
347
  return 0;
348
}
349

    
350
int calcExpCounts(){
351

    
352
  Response::EffectiveAreaLookupIR lookup( "hard", true );
353
  int nb;
354
  double expCounts = lookup.CalculateExpectedCounts(2.5,1.,50.,25.,0.5,335.,103,30,3600.,nb);
355

    
356
  std::cout << "EXP: " << expCounts << std::endl;
357

    
358
  return 0;
359
}
360

    
361
int expCountsVsAzm(){
362

    
363
  Response::EffectiveAreaLookupIR lookup( "hard", true );
364
  int nb;
365

    
366
  TH1D* histo = new TH1D( "histo", "exp counts vs azm", 360, 0., 360. );
367

    
368
  for( double azm = 0.0 ; azm < 360. ; azm += 5.0 ){
369

    
370
    double expCounts = lookup.CalculateExpectedCounts(2.5,1.,50.,25.,0.5,azm,103,30,3600.,nb);
371

    
372
    histo->Fill( azm, expCounts );
373
  }
374

    
375
  histo->SetStats(0);
376
  new TCanvas;
377
  histo->Draw();
378

    
379
  return 0;
380
}
381

    
382

    
383
int indexDependenceOfIntegralFlux( bool useRawEnergy = true ){
384

    
385
  gROOT->SetStyle("Plain");
386

    
387
  Response::EffectiveAreaLookupIR lookup( "std", true, useRawEnergy );
388

    
389
  double referenceIndex = 2.5; //3.0;
390
  double indexRange = 0.6;
391
  double indexStep = 0.05;
392

    
393
  double thresholdEnergies[2] = { 0.2, 1.0 };
394
  double zeniths[3] = { 20., 40., 60. };
395
  double offsets[3] = { 0.5, 0.75, 1.0};
396
  double azimuth       = 180.;
397
  int muonEntry        = 101;
398
  int telescopePattern = 30;
399

    
400
  double Emax = 100.;
401

    
402
  TGraph* graphs[2][3][3];
403

    
404
  for( int ene = 0 ; ene < 2 ; ++ene ){
405

    
406
    double E1 = thresholdEnergies[ene];
407

    
408
    for( int zen = 0 ; zen < 3 ; ++zen ){
409

    
410
      double zenith = zeniths[zen];
411

    
412
      for( int off = 0 ; off < 3 ; ++off ){
413

    
414
        double offset = offsets[off];
415

    
416
        int nBinsFailed;
417
        double expCountsRef = lookup.CalculateExpectedCounts( referenceIndex, E1, Emax,
418
                                                              zenith, offset, azimuth,
419
                                                              muonEntry, telescopePattern,
420
                                                              1.0, nBinsFailed );
421

    
422
        TGraph*& graph = graphs[ene][zen][off];
423
        graph = new TGraph;
424

    
425
        double index = referenceIndex - indexRange;
426
        int i=0;
427
        while( index <= referenceIndex + indexRange ){
428

    
429
          double expCounts = lookup.CalculateExpectedCounts( index, E1, Emax,
430
                                                             zenith, offset, azimuth,
431
                                                             muonEntry, telescopePattern,
432
                                                             1.0, nBinsFailed );
433

    
434
          double fluxRatio = expCountsRef / expCounts;
435
          double deviation = 100.0 * (fluxRatio - 1.0);
436

    
437
          graph->SetPoint(i,index,deviation);
438

    
439
          index += indexStep;
440
          ++i;
441
        }
442
      }
443
    }
444
  }
445

    
446
  TCanvas* canv = new TCanvas( "indexDependenceOfIntegralFluxCanvas", "index dependence of integral flux", 900, 600 );
447
  canv->Divide(2,1);
448

    
449
  TLegend* legend = 0;
450

    
451
  for( int ene = 0 ; ene < 2 ; ++ene ){
452

    
453
    canv->cd(ene+1);
454

    
455
    legend = new TLegend(0.2,0.6,0.4,0.85);
456

    
457
    graphs[ene][0][0]->SetLineColor(2);
458
    graphs[ene][0][0]->Draw("AL");
459
    std::stringstream title;
460
    title << "E > " << thresholdEnergies[ene] << " TeV";
461
    graphs[ene][0][0]->GetHistogram()->SetTitle(title.str().c_str());
462
    graphs[ene][0][0]->GetHistogram()->SetMaximum(200.);
463
    graphs[ene][0][0]->GetHistogram()->SetMinimum(-200.);
464
    graphs[ene][0][0]->GetHistogram()->GetXaxis()->SetTitle("index");
465
    graphs[ene][0][0]->GetHistogram()->GetYaxis()->SetTitle("deviation (%)");
466
    for( int zen = 0 ; zen < 3 ; ++zen ){
467
      for( int off = 0 ; off < 3 ; ++off ){
468
        if( !zen && !off ) continue;
469

    
470
        graphs[ene][zen][off]->SetLineColor(zen+2);
471
        graphs[ene][zen][off]->SetLineStyle(off+1);
472
        graphs[ene][zen][off]->Draw("L");
473

    
474
      }
475

    
476
      std::stringstream text;
477
      text << zeniths[zen] << " deg zenith";
478
      legend->AddEntry(graphs[ene][zen][0],text.str().c_str(),"L");
479
    }
480

    
481
    if( thresholdEnergies[ene] == 1.0 ){
482

    
483
      TF1* expectation = new TF1( "expectation", "100*((x-1)/([0]-1)-1)", referenceIndex-indexRange,  referenceIndex+indexRange);
484
      expectation->SetParameter(0,referenceIndex);
485
      expectation->SetLineStyle(2);
486
      expectation->SetLineColor(1);
487
      expectation->Draw("L SAME");
488
      cout << expectation->Eval(2.) << " " << expectation->Eval(3.) << " " << expectation->Eval(4.) << endl;
489
      cout << "Drawing expectation." << endl;
490
      legend->AddEntry(expectation,"expectation","L");
491
    }
492

    
493
    legend->SetFillColor(10);
494
    legend->SetLineColor(10);
495
    legend->Draw();
496
  }
497

    
498
  return 0;
499
}
500

    
501
int test_GaussianEnergyResolution() {
502

    
503
  // standard tests for fixed optical configuration
504

    
505
  Response::EffectiveAreaLookupIR lookup( "std", false );
506

    
507
  int muonEntry        = 100;
508
  int telescopePattern = 30;
509
  double azimuth       = 197.467;
510
  double zenith        = 29.9336;
511
  double offset        = 0.450788;
512
  double energy        = 0.404803;
513

    
514
//   lookup.PrintLookupArrays();
515

    
516
  double energyResolution, energyBias;
517
  bool success1 = lookup.EnergyResolution(energy,zenith,offset,muonEntry,telescopePattern,azimuth,energyResolution);
518
  bool success2 = lookup.EnergyBias(energy,zenith,offset,muonEntry,telescopePattern,azimuth,energyBias);
519

    
520
  std::cout << "EnergyResolution: " << energyResolution << " success: " << success1 << std::endl;
521
  std::cout << "EnergyBias:       " << energyBias << " success: " << success2 << std::endl;
522

    
523
  return 0;
524
}
525

    
526

    
527
int test_GaussianEnergyResolution_opticalConfiguration(){
528

    
529
  // test interpolation based on optical efficiencies
530

    
531
  Response::EffectiveAreaLookupIR lookup( "std", false );
532

    
533
  int telescopePattern = 30;
534
  double azimuth       = 0.0;
535
  double zenith        = 13.0;
536
  double offset        = 0.4;
537
  double energy        = 1.4;
538

    
539
  Muon::ArrayMuonParameters ap(Sash::HESSArray::GetHESSArray());
540
  ap.GetMcCorrectionFactorForSecondMuonEntry()  = 1.4;
541
  ap.GetDataCorrectionFactorForFirstMuonEntry() = 1.1;
542
  ap.GetFirstMuonEntry()   = 101;
543
  ap.GetSecondMuonEntry()  = 102;
544
  ap.GetTelescopePattern() = telescopePattern;
545

    
546
  double energyResolution, energyBias;
547
  bool success1 = lookup.EnergyResolution(&ap,energy,zenith,offset,azimuth,energyResolution);
548
  bool success2 = lookup.EnergyBias(&ap,energy,zenith,offset,azimuth,energyBias);
549

    
550
  std::cout << "EnergyResolution: " << energyResolution << " success: " << success1 << std::endl;
551
  std::cout << "EnergyBias:       " << energyBias << " success: " << success2 << std::endl;
552

    
553
  return 0;
554
}
555

    
556

    
557
int plot_GaussianEnergyResolution( double zenith = 10., double azimuth = 42., double offset = 0.42,
558
                                   int muonEntry = 100, int telPattern = 30 ) {
559

    
560
  Response::EffectiveAreaLookupIR lookup( "std", false );
561

    
562
  TH1* eres = new TH1D("eres", "Energy Resolution", 100, -2, 2);
563
  eres->GetXaxis()->SetTitle("log_{10}(E)");
564
  eres->GetYaxis()->SetTitle("relative energy resolution");
565

    
566
  TH1* ebias = new TH1D("ebias", "Energy Resolution", 100, -2, 2);
567
  ebias->GetXaxis()->SetTitle("log_{10}(E)");
568
  ebias->GetYaxis()->SetTitle("relative energy bias");
569

    
570
  for (int bin = 1; bin <= eres->GetNbinsX(); bin++) {
571
    double energy = pow10(eres->GetBinCenter(bin));
572
    double energyResolution, energyBias;
573
    lookup.EnergyResolution(energy,zenith,offset,muonEntry,telPattern,azimuth,energyResolution);
574
    lookup.EnergyBias(energy,zenith,offset,muonEntry,telPattern,azimuth,energyBias);
575
    eres->SetBinContent(bin, energyResolution);
576
    ebias->SetBinContent(bin, energyBias);
577
  }
578

    
579
  TCanvas* eresCanvas = new TCanvas( "eresCanvas", "energy resolution", 1200, 800 );
580
  eresCanvas->Divide(2,1);
581

    
582
  eresCanvas->cd(1);
583
  ebias->Draw();
584

    
585
  eresCanvas->cd(2);
586
  eres->Draw();
587

    
588
  return 0;
589
}