BgStats.C

Deil Christoph, 10/15/2012 11:16 AM

Download (24 KB)

 
1
/*
2
*/
3

    
4
#include <TH1D.h>
5
#include <TString.h>
6
#include <TCanvas.h>
7
#include <TVector2.h>
8

    
9
#include <iostream>
10
#include <stdexcept>
11

    
12
#include <sash/HESSArray.hh>
13
#include <sash/MonitorBase.hh>
14
#include <sash/EnvelopeEntry.hh>
15

    
16

    
17
#include <tools/EventStats.hh>
18
#include "BgStats.hh"
19

    
20
#define DEBUG 0 // change to 1 to enable debugging here. Then, use the
21
                // DEBUG_OUT macro defined in "debugging.hh" instead
22
                // of "std::cout" to write out debug info.
23
#include <utilities/debugging.hh>
24

    
25
using std::cout;
26
using std::endl;
27

    
28
ENVELOPE_ENTRY(Sash::HESSArray,Background::BgStats);
29

    
30
Background::BgStats::BgStats(Sash::HESSArray& h, const char * name) 
31
  : MonitorBase("Handle<Background::BgStats>",name,Sash::MonitorBase::NonSplit),
32
    fRateStats(),
33
    fOnRegion(),
34
    fOffRegion(),
35
    fOnExclusionRegion(),
36
    fOffExclusionRegion(),
37
    fIdNumber(0),
38
    fNumberOfTelescopes(0),
39
    fTelescopePattern(0),
40
    fSkippedFlag(true),
41
    fBadFlag(false),
42
    fOnFlag(false),
43
    fOffFlag(false),
44
    fNumOn(0),
45
    fNumOff(0),
46
    fNumSkippedOn(0),
47
    fNumSkippedOff(0),
48
    fNumBadOn(0),
49
    fNumBadOff(0),
50
    fZenithDistOn(),
51
    fZenithDistOff(),
52
    fAzimuthDistOn(),
53
    fAzimuthDistOff(),
54
    fOffsetDistOn(),
55
    fOffsetDistOff(),
56
    fZenithOffsetDistOn(),
57
    fZenithOffsetDistOff(),
58
    fIsCumulative(false),
59
    fObservationPosition(0,0,Stash::System::GetDummySystem()),
60
    fMuonEntry(0),
61
    fArrayMuonParameters(h),
62
    fConfigName("")
63
{
64
  SetHistNames(name);
65
}
66

    
67
Background::BgStats::BgStats(Sash::HESSArray& h, const BgStats &stats, const char * name) 
68
  : MonitorBase("Handle<Background::BgStats>",name,Sash::MonitorBase::NonSplit),
69
    fObservationPosition(0,0,Stash::System::GetDummySystem()),
70
    fArrayMuonParameters(h)
71
{
72
  ReplaceStatsFrom(stats,false);
73
}
74

    
75
Background::BgStats::BgStats(const char* handleFunc, Sash::HESSArray& h, const char * name) 
76
  : MonitorBase(handleFunc,name,Sash::MonitorBase::NonSplit),
77
    fRateStats(),
78
    fOnRegion(),
79
    fOffRegion(),
80
    fOnExclusionRegion(),
81
    fOffExclusionRegion(),
82
    fIdNumber(0),
83
    fNumberOfTelescopes(0),
84
    fTelescopePattern(0),
85
    fSkippedFlag(true),
86
    fBadFlag(false),
87
    fOnFlag(false),
88
    fOffFlag(false),
89
    fNumOn(0),
90
    fNumOff(0),
91
    fNumSkippedOn(0),
92
    fNumSkippedOff(0),
93
    fNumBadOn(0),
94
    fNumBadOff(0),
95
    fZenithDistOn(),
96
    fZenithDistOff(),
97
    fAzimuthDistOn(),
98
    fAzimuthDistOff(),
99
    fOffsetDistOn(),
100
    fOffsetDistOff(),
101
    fZenithOffsetDistOn(),
102
    fZenithOffsetDistOff(),
103
    fIsCumulative(false),
104
    fObservationPosition(0,0,Stash::System::GetDummySystem()),
105
    fMuonEntry(0),
106
    fArrayMuonParameters(h),
107
    fConfigName("")
108
{
109
  SetHistNames(name);
110
}
111

    
112
Background::BgStats::~BgStats() {
113
  
114
  DEBUG_OUT << "GOODBYE" << endl;
115

    
116
}
117

    
118

    
119
void Background::BgStats::SetHistNames(const char* name)
120
{
121
  std::string Name = name;
122

    
123
  GetOnRegion().SetName((Name+"_OnRegion").c_str());
124
  GetOffRegion().SetName((Name+"_OffRegion").c_str());
125
  GetOnExclusionRegion().SetName((Name+"_OnExclusionRegion").c_str());
126
  GetOffExclusionRegion().SetName((Name+"_OffExclusionRegion").c_str());
127

    
128
  TH1::AddDirectory(kFALSE);
129

    
130
  GetZenithDistOn().ResetBit(kCanDelete);
131
  GetZenithDistOff().ResetBit(kCanDelete);
132
  GetAzimuthDistOn().ResetBit(kCanDelete);
133
  GetAzimuthDistOff().ResetBit(kCanDelete);
134
  GetOffsetDistOn().ResetBit(kCanDelete);
135
  GetOffsetDistOff().ResetBit(kCanDelete);
136
  GetZenithOffsetDistOn().ResetBit(kCanDelete);
137
  GetZenithOffsetDistOff().ResetBit(kCanDelete);
138

    
139
  GetZenithDistOn().ResetBit(kMustCleanup);
140
  GetZenithDistOff().ResetBit(kMustCleanup);
141
  GetAzimuthDistOn().ResetBit(kMustCleanup);
142
  GetAzimuthDistOff().ResetBit(kMustCleanup);
143
  GetOffsetDistOn().ResetBit(kMustCleanup);
144
  GetOffsetDistOff().ResetBit(kMustCleanup);
145
  GetZenithOffsetDistOn().ResetBit(kMustCleanup);
146
  GetZenithOffsetDistOff().ResetBit(kMustCleanup);
147

    
148
  GetZenithDistOn().SetName((Name+"_ZenithDistOn").c_str());
149
  GetZenithDistOn().SetTitle("ON Zenith Distribution;zenith angle / deg");
150
  GetZenithDistOff().SetName((Name+ "_ZenithDistOff").c_str());
151
  GetZenithDistOff().SetTitle("OFF Zenith Distribution;zenith angle / deg");
152
  
153
  GetAzimuthDistOn().SetName((Name+"_AzimuthDistOn").c_str());
154
  GetAzimuthDistOn().SetTitle("ON Azimuth Distribution;azimuth angle / deg");
155
  GetAzimuthDistOff().SetName((Name+ "_AzimuthDistOff").c_str());
156
  GetAzimuthDistOff().SetTitle("OFF Azimuth Distribution;azimuth angle / deg");
157
  
158
  GetOffsetDistOn().SetName((Name+ "_OffsetDistOn").c_str());
159
  GetOffsetDistOn().SetTitle("ON Offset Distribution;offset / deg");
160
  GetOffsetDistOff().SetName((Name+ "_OffsetDistOff").c_str());
161
  GetOffsetDistOff().SetTitle("OFF Offset Distribution;offset / deg");
162
  
163
  GetZenithOffsetDistOn().SetName((Name+ "_ZenithOffsetDistOn").c_str());
164
  GetZenithOffsetDistOn().SetTitle("ON ZenithOffset Distribution;zenith angle / deg;offset / deg");
165
  GetZenithOffsetDistOff().SetName((Name+ "_ZenithOffsetDistOff").c_str());
166
  GetZenithOffsetDistOff().SetTitle("OFF ZenithOffset Distribution;zenith angle / deg;offset / deg");
167
  
168
  TH1::AddDirectory(kTRUE);
169

    
170
  return;
171
}
172

    
173

    
174
/**
175
 * clear all values in the BgStats
176
 */
177
void Background::BgStats::Clear(Option_t *option)
178
{
179
  DEBUG_OUT << "Clearing " << GetName() << " with option " << std::string(option) << std::endl;
180
 
181
  if(std::string(option) == std::string("online")) {
182
  } else {      
183
    GetOnRegion().Reset();
184
    GetOffRegion().Reset();
185
    GetOnExclusionRegion().Reset();
186
    GetOffExclusionRegion().Reset();
187
    GetIdNumber() = 0;
188
    GetNumberOfTelescopes() = 0;
189
    GetTelescopePattern() = 0;      
190
    GetOnFlag() = false;
191
    GetOffFlag() = false;
192
    GetMuonEntry() = 0;
193
    GetArrayMuonParameters().Clear();
194
    GetConfigName() = "";
195
  }
196
  GetRateStats().Clear(option);
197
  GetSkippedFlag() = false;
198
  GetBadFlag() = false;
199
  GetNumOn() = 0;
200
  GetNumOff() = 0;
201
  GetNumSkippedOn() = 0;
202
  GetNumSkippedOff() = 0;
203
  GetNumBadOn() = 0;
204
  GetNumBadOff() = 0;
205
  GetZenithDistOn().Reset();
206
  GetZenithDistOff().Reset();
207
  GetAzimuthDistOn().Reset();
208
  GetAzimuthDistOff().Reset();
209
  GetOffsetDistOn().Reset();
210
  GetOffsetDistOff().Reset();
211
  GetZenithOffsetDistOn().Reset();
212
  GetZenithOffsetDistOff().Reset();
213
  GetIsCumulative() = false;
214
  
215
  DEBUG_OUT << "ZenDistOff: " << GetZenithDistOff().GetName()
216
            << " entries: "<< GetZenithDistOff().GetEntries() << endl;
217
  
218
  return;
219
}
220

    
221

    
222
void Background::BgStats::print(std::ostream& os) const
223
{
224
  os << "[" << GetName() << "]" << std::endl;
225

    
226
  if (GetIsCumulative() == false) {
227
    os << "\tId            = " << GetIdNumber() << std::endl;
228
    os << "\tNtel          = " << GetNumberOfTelescopes() << std::endl;
229
    os << "\tTelPattern    = " << GetTelescopePattern() << std::endl;
230
    os << "\tMuonEntry     = " << GetMuonEntry() << std::endl;
231
    os << "\tArrayMuonPars = " << GetArrayMuonParameters() << std::endl;
232
    os << "\tIsOnRun       = " << GetOnFlag() << std::endl;
233
    os << "\tIsOffRun      = " << GetOffFlag() << std::endl;
234
  }  
235
  else {
236
    os << "\tNumOn         = " << GetNumOn() << " runs"<<std::endl
237
       << "\tNumSkippedOn  = " << GetNumSkippedOn() << " runs" << std::endl
238
       << "\tNumBadOn      = " << GetNumBadOn() << " runs"<< std::endl;
239
    
240
    os << "\tNumOff        = " << GetNumOff() << " runs" << std::endl
241
       << "\tNumSkippedOff = " << GetNumSkippedOff() << " runs" << std::endl
242
       << "\tNumBadOff     = " << GetNumBadOff() << " runs" << std::endl;
243
  }
244
  if (GetSkippedFlag())
245
    os << "\tSkipped       = true"<< std::endl;
246
  else 
247
    os << "\tSkipped       = false"<< std::endl;
248

    
249
  if (GetBadFlag())
250
    os << "\tBad           = true"<< std::endl;
251
  else 
252
    os << "\tBad           = false"<< std::endl;
253

    
254
  if (GetSkippedFlag() == false){
255
    os << "\tConfigName    = " << GetConfigName() << std::endl;
256
    os << "\tObservationPos= " << GetObservationPosition() << std::endl;
257
    os << "\tMeanZenithOn  = " << GetMeanZenithOn() << " deg" << std::endl;
258
    os << "\tMeanZenithOff = " << GetMeanZenithOff() << " deg" << std::endl;
259
    os << "\tMeanOffsetOn  = " << GetMeanOffsetOn() << " deg" << std::endl;
260
    os << "\tMeanOffsetOff = " << GetMeanOffsetOff() << " deg" << std::endl;
261
    os << "\tMeanAzimuthOn = " << GetMeanAzimuthOn() << " deg" << std::endl;
262
    os << "\tMeanAzimuthOff= " << GetMeanAzimuthOff() << " deg" << std::endl;
263

    
264
    os << GetRateStats() << std::endl;
265

    
266
  }
267
}
268

    
269

    
270
/**
271
 * Sum up stats from another BgStats object, making this one a
272
 * cumulative BgStats.
273
 *
274
 * \todo: check if ON region is the same and throw exception if it's
275
 * not. Also, "or" together the EXCLUSION regions.
276
 *
277
 * \todo: sum OFF regions too
278
 */
279
void
280
Background::BgStats::
281
AddStatsFrom(const BgStats &stats, bool online){
282

    
283
  DEBUG_OUT << "adding stats from: "<< stats.GetName() 
284
            << endl; 
285

    
286
  GetIsCumulative() = true;
287
  GetSkippedFlag() = false;
288

    
289
  // Don't accumulate from skipped or bad runs:
290
  if (stats.GetSkippedFlag() == true) {
291
    if (stats.GetOnFlag() == true )   GetNumSkippedOn()++;
292
    if (stats.GetOffFlag() == true )   GetNumSkippedOff()++;
293
    DEBUG_OUT << "Run "<< stats.GetIdNumber() << " was skipped" << endl;
294
    return;
295
  }
296

    
297
  if (stats.GetBadFlag() == true) {
298
    if (stats.GetOnFlag() == true )   GetNumBadOn()++;
299
    if (stats.GetOffFlag() == true )  GetNumBadOff()++;
300
    DEBUG_OUT << "Run "<< stats.GetIdNumber() << " was skipped as BAD" << endl;
301
    return;
302
  }
303

    
304
  // count number of accumulated on and off runs
305
  if (stats.GetOnFlag() == true )   GetNumOn()++;
306
  if (stats.GetOffFlag() == true )  GetNumOff()++;
307

    
308
  // update all rate stats:
309
  GetRateStats() += stats.GetRateStats();
310

    
311
  // other info
312
  GetIdNumber() = -1; // special ID for cumulative BgStats
313

    
314
  // number of telescopes
315
  int nTel = GetNumberOfTelescopes();
316
  if( nTel==0 ){
317
    GetNumberOfTelescopes() = stats.GetNumberOfTelescopes();
318
    GetTelescopePattern()   = stats.GetTelescopePattern();
319
  }
320
  else if( nTel == -1 ){
321
    // do nothing
322
  }
323
  else{
324
    if( nTel != stats.GetNumberOfTelescopes() ){
325
      GetNumberOfTelescopes() = -1; // from now on, mixed number of telescopes
326
      GetTelescopePattern()   = -1;
327
    }
328
  }
329

    
330
  // on/off/excl regions:
331

    
332
  DEBUG_OUT << "Add regions" << std::endl;
333

    
334
  // accumulate ON region (want to keep just the first one, so we
335
  // don't get hundreds of copies of the same one)
336

    
337
  if(!online) {
338
    if (GetOnRegion().GetNumRegions() == 0) {
339
      DEBUG_OUT << "Accumulating ON region..." << endl;
340
      GetOnRegion().AddRegion( &(stats.GetOnRegion()));
341
    }
342
    
343
    DEBUG_OUT << "Accumulating OFF regions..." << endl;
344
    GetOffRegion().AddRegion( &(stats.GetOffRegion() ) );
345
    
346
    
347
    DEBUG_OUT << "Accumulating EXCLUSION regions..." << endl;
348
    GetOnExclusionRegion().
349
      AddRegion( &(stats.GetOnExclusionRegion() ) );
350
    GetOffExclusionRegion().
351
      AddRegion( &(stats.GetOffExclusionRegion() ) );
352
  }
353

    
354
  // set the Observation Position:
355
  GetObservationPosition() = stats.GetObservationPosition();
356
    
357

    
358
  // Zenith and Offset Distributions:
359
  DEBUG_OUT << "Adding zenith and offset distributions..." 
360
            << endl;
361

    
362
  if (GetZenithDistOn().GetNbinsX() == stats.GetZenithDistOn().GetNbinsX()) 
363
    GetZenithDistOn().Add( &stats.GetZenithDistOn() );
364
  else if(GetZenithDistOn().GetNbinsX() <= 1)
365
    GetZenithDistOn() = stats.GetZenithDistOn();
366
  else
367
    throw std::range_error("Can't add ZenithDistOn with incompatible size");
368

    
369
  if (GetZenithDistOff().GetNbinsX() == stats.GetZenithDistOff().GetNbinsX()) 
370
    GetZenithDistOff().Add( &stats.GetZenithDistOff() );
371
  else if(GetZenithDistOff().GetNbinsX() <= 1)
372
    GetZenithDistOff() = stats.GetZenithDistOff();
373
  else 
374
    throw std::range_error("Can't add ZenithDistOff with incompatible size");
375
  
376
  if (GetAzimuthDistOn().GetNbinsX() == stats.GetAzimuthDistOn().GetNbinsX()) 
377
    GetAzimuthDistOn().Add( &stats.GetAzimuthDistOn() );
378
  else if(GetAzimuthDistOn().GetNbinsX() <= 1)
379
    GetAzimuthDistOn() = stats.GetAzimuthDistOn();
380
  else
381
    throw std::range_error("Can't add AzimuthDistOn with incompatible size");
382

    
383
  if (GetAzimuthDistOff().GetNbinsX() == stats.GetAzimuthDistOff().GetNbinsX()) 
384
    GetAzimuthDistOff().Add( &stats.GetAzimuthDistOff() );
385
  else if(GetAzimuthDistOff().GetNbinsX() <= 1)
386
    GetAzimuthDistOff() = stats.GetAzimuthDistOff();
387
  else 
388
    throw std::range_error("Can't add AzimuthDistOff with incompatible size");
389
  
390
  if (GetOffsetDistOn().GetNbinsX() == stats.GetOffsetDistOn().GetNbinsX()) 
391
    GetOffsetDistOn().Add( &stats.GetOffsetDistOn() );
392
  else if(GetOffsetDistOn().GetNbinsX() <= 1)
393
    GetOffsetDistOn() = stats.GetOffsetDistOn();
394
  else 
395
    throw std::range_error("Can't add OffsetDistOn with incompatible size");
396

    
397
  if (GetOffsetDistOff().GetNbinsX() == stats.GetOffsetDistOff().GetNbinsX()) 
398
    GetOffsetDistOff().Add( &stats.GetOffsetDistOff() );
399
  else if(GetOffsetDistOff().GetNbinsX() <= 1)
400
    GetOffsetDistOff() = stats.GetOffsetDistOff();
401
  else 
402
    throw std::range_error("Can't add OffsetDistOff with incompatible size");
403

    
404
  if (GetZenithOffsetDistOn().GetNbinsX() == stats.GetZenithOffsetDistOn().GetNbinsX() &&
405
      GetZenithOffsetDistOn().GetNbinsY() == stats.GetZenithOffsetDistOn().GetNbinsY()) 
406
    GetZenithOffsetDistOn().Add( &stats.GetZenithOffsetDistOn() );
407
  else if((GetZenithOffsetDistOn().GetNbinsX() <= 1) &&
408
          (GetZenithOffsetDistOn().GetNbinsY() <= 1))
409
    GetZenithOffsetDistOn() = stats.GetZenithOffsetDistOn();
410
  else 
411
    throw std::range_error("ZenithOffsetDistOn out of range");
412

    
413
  if (GetZenithOffsetDistOff().GetNbinsX() == stats.GetZenithOffsetDistOff().GetNbinsX() &&
414
      GetZenithOffsetDistOff().GetNbinsY() == stats.GetZenithOffsetDistOff().GetNbinsY()) 
415
    GetZenithOffsetDistOff().Add( &stats.GetZenithOffsetDistOff() );
416
  else if((GetZenithOffsetDistOff().GetNbinsX() <= 1) &&
417
          (GetZenithOffsetDistOff().GetNbinsY() <= 1))
418
    GetZenithOffsetDistOff() = stats.GetZenithOffsetDistOff();
419
  else 
420
    throw std::range_error("ZenithOffsetDistOff out of range");
421

    
422
  // check and set the ConfigName:
423
  if (GetConfigName() =="") {
424
    GetConfigName() = stats.GetConfigName();
425
  }
426
  else {
427
    if (GetConfigName().find(stats.GetConfigName())==std::string::npos) {
428
      WARN_OUT << "You have added statistics using " 
429
               << stats.GetConfigName() <<  " to a run which was analyzed "
430
               << "with "<<GetConfigName()<<"! Are you sure that's wise?"
431
               << endl;
432
      GetConfigName() += "+"+stats.GetConfigName();
433
    }
434
  }
435
  
436
  DEBUG_OUT << "Done adding histograms" << endl;
437

    
438
}
439

    
440
void Background::BgStats::ReplaceStatsFrom(const BgStats &stats, bool online)
441
{
442
  std::string Name = GetName();
443
  GetRateStats() = stats.GetRateStats();
444
  if(!online) {
445
    GetOnRegion() = *(stats.GetOnRegion().Clone((Name+"_OnRegion").c_str()));
446
    GetOffRegion() = *(stats.GetOffRegion().Clone((Name+"_OffRegion").c_str()));
447
    GetOnExclusionRegion() = *(stats.GetOnExclusionRegion().Clone((Name+"_OnExclusionRegion").c_str()));
448
    GetOffExclusionRegion() = *(stats.GetOffExclusionRegion().Clone((Name+"_OffExclusionRegion").c_str()));
449
  }
450
  GetIdNumber() = stats.GetIdNumber();
451
  GetNumberOfTelescopes() = stats.GetNumberOfTelescopes();
452
  GetTelescopePattern() = stats.GetTelescopePattern();
453
  GetSkippedFlag() = stats.GetSkippedFlag();
454
  GetBadFlag() = stats.GetBadFlag();
455
  GetOnFlag() = stats.GetOnFlag();
456
  GetOffFlag() = stats.GetOffFlag();
457
  GetNumOn() = stats.GetNumOn();
458
  GetNumOff() = stats.GetNumOff();
459
  GetNumSkippedOn() = stats.GetNumSkippedOn();
460
  GetNumSkippedOff() = stats.GetNumSkippedOff();
461
  GetNumBadOn() = stats.GetNumBadOn();
462
  GetNumBadOff() = stats.GetNumBadOff();
463
  GetZenithDistOn() = stats.GetZenithDistOn();
464
  GetZenithDistOn().SetName((Name+"_ZenithDistOn").c_str());
465
  GetZenithDistOff() = stats.GetZenithDistOff();
466
  GetZenithDistOff().SetName((Name+"_ZenithDistOff").c_str());
467
  GetAzimuthDistOn() = stats.GetAzimuthDistOn();
468
  GetAzimuthDistOn().SetName((Name+"_AzimuthDistOn").c_str());
469
  GetAzimuthDistOff() = stats.GetAzimuthDistOff();
470
  GetAzimuthDistOff().SetName((Name+"_AzimuthDistOff").c_str());
471
  GetOffsetDistOn() = stats.GetOffsetDistOn();
472
  GetOffsetDistOn().SetName((Name+"_OffsetDistOn").c_str());
473
  GetOffsetDistOff() = stats.GetOffsetDistOff();
474
  GetOffsetDistOff().SetName((Name+"_OffsetDistOff").c_str());
475
  GetZenithOffsetDistOn() = stats.GetZenithOffsetDistOn();
476
  GetZenithOffsetDistOn().SetName((Name+"_ZenithOffsetDistOn").c_str());
477
  GetZenithOffsetDistOff() = stats.GetZenithOffsetDistOff();
478
  GetZenithOffsetDistOff().SetName((Name+"_ZenithOffsetDistOff").c_str());
479
  GetIsCumulative() = stats.GetIsCumulative();
480
  GetObservationPosition() = stats.GetObservationPosition();
481
  GetMuonEntry() = stats.GetMuonEntry();
482
  GetArrayMuonParameters() = stats.GetArrayMuonParameters();
483
  GetConfigName() = stats.GetConfigName();
484
}
485

    
486
/**
487
 * Fills zenith and offset distributions, assuming the ON and OFF
488
 * regions have been set already. If you only want to fill ON or
489
 * OFF distributions use the flags 'fillon' and 'filloff'. This
490
 * might be necessary, if ON and OFF data are taken from different
491
 * runs.
492
 *
493
 * \param obspos - observation position
494
 * \param starttime - starting time of the run
495
 * \param endtime - ending time of the run
496
 * \param psicut - fill offset distribut only with region fullfilling
497
 *                 the psi cut; ignore the psi, if parameter < 0
498
 * \param fillon - fill ON distributions (default: true)
499
 * \param filloff - fill OFF distributions (default: true)
500
 *
501
 * \note this assumes the correct ON and OFF regions have already been
502
 * set up.
503
 */
504
void
505
Background::BgStats::
506
FillDistributions( const Stash::Coordinate &obspos,
507
                   const Sash::Time starttime, 
508
                   const Sash::Time endtime,
509
                   double psicut, bool fillon, bool filloff) {
510

    
511
  const double MAX_OFFSET = 4.0;
512
  const double MAX_ZENITH = 90.0;
513
  const int NBINS_OFFSET = 80;
514
  const int NBINS_ZENITH = 90;
515
  const int NBINS_AZIMUTH = 90;
516
  const double MAX_AZIMUTH = 360.0;
517

    
518
  DEBUG_OUT << "Filling Zenith and Offset distributions " << endl;
519
  if (!fillon && filloff) {
520
    DEBUG_OUT << "only fill OFF histograms" << endl;
521
  }
522
  if (fillon && !filloff) {
523
    DEBUG_OUT << "only fill ON histograms" << endl;
524
  }
525
  if (!fillon && !filloff) {
526
    WARN_OUT << "don't fill either ON nor OFF histograms" << endl;
527
    return;
528
  }
529

    
530
  if (GetOnRegion().GetArea() < 1.0e-9) {
531
    WARN_OUT << GetName()
532
             << ": There is no ON region: ON zenith distribution will "
533
             << "not be calculated correctly!" << endl;
534
  }
535

    
536
  if (GetOffRegion().GetArea() < 1.0e-9 ) {
537
    if( TString(GetName()).Contains("AcceptanceLookupBgMaker") )
538
      filloff = false;
539
    else
540
      WARN_OUT << GetName()
541
               << ": There is no OFF region: OFF zenith distribution will "
542
               << "not be calculated correctly!" << endl;
543
  }
544

    
545
  if (fillon) {
546
    GetZenithDistOn().SetBins( NBINS_ZENITH, 0.0, MAX_ZENITH );
547
    GetOffsetDistOn().SetBins( NBINS_OFFSET, 0.0, MAX_OFFSET );
548
    GetZenithOffsetDistOn().SetBins( NBINS_ZENITH,0.0,MAX_ZENITH,
549
                                     NBINS_OFFSET,0.0,MAX_OFFSET );
550

    
551
    GetAzimuthDistOn().SetBins( NBINS_AZIMUTH, 0.0, MAX_AZIMUTH );
552
    
553

    
554
    GetOnRegion().FillZenithOrAzimuthDistribution( GetZenithDistOn(),starttime,endtime,true);
555
    GetOnRegion().FillZenithOrAzimuthDistribution( GetAzimuthDistOn(),starttime,endtime,false);
556
    GetOnRegion().FillOffsetDistribution( obspos, GetOffsetDistOn(), psicut );
557

    
558
    GetOnRegion().FillZenithOffsetDistribution( obspos, 
559
                                                GetZenithOffsetDistOn(),
560
                                                starttime,endtime,
561
                                                psicut );
562
  }
563

    
564
  if (filloff) {
565
    GetZenithDistOff().SetBins( NBINS_ZENITH, 0.0, MAX_ZENITH );
566
    GetOffsetDistOff().SetBins( NBINS_OFFSET, 0.0, MAX_OFFSET );
567
    GetZenithOffsetDistOff().SetBins( NBINS_ZENITH,0.0,MAX_ZENITH,
568
                                      NBINS_OFFSET,0.0,MAX_OFFSET );
569

    
570
    GetAzimuthDistOff().SetBins( NBINS_AZIMUTH, 0.0, MAX_AZIMUTH );
571

    
572
    GetOffRegion().FillZenithOrAzimuthDistribution(GetZenithDistOff(),starttime,endtime,true);
573
    GetOffRegion().FillZenithOrAzimuthDistribution(GetAzimuthDistOff(),starttime,endtime,false);
574
    GetOffRegion().FillOffsetDistribution( obspos, GetOffsetDistOff(), psicut );
575
    GetOffRegion().FillZenithOffsetDistribution( obspos, 
576
                                                 GetZenithOffsetDistOff(),
577
                                                 starttime, endtime, psicut );
578
  }
579
  
580
}
581

    
582

    
583
Stash::Lambda
584
Background::BgStats::
585
GetMaximumOffset() {
586

    
587
  if (GetOffsetDistOn().GetEntries() == 0 ) {
588
    WARN_OUT << "No entries in ON offset distribution!" << endl;
589
    return Stash::Lambda(180.0, Stash::Angle::Degrees);
590
  }
591

    
592
  // check overflow bin - /todo is this bin filled ?
593
  if (GetOffsetDistOn().GetBinContent( GetOffsetDistOn().GetNbinsX()+1 ) > 0) {
594
    // low edge of overflow bin
595
    Stash::Lambda ret = Stash::Lambda( GetOffsetDistOn().
596
                                       GetBinLowEdge( GetOffsetDistOn().GetNbinsX() ),
597
                                       Stash::Angle::Degrees );
598

    
599
    WARN_OUT << "overflow bin of OffsetDistOn is not empty, "
600
             << "the maximum offset might be larger than " << ret << endl;
601
    return ret;
602
  }
603

    
604
  for (int i=GetOffsetDistOn().GetNbinsX()-1; i>0; i--) {
605

    
606
    if ( GetOffsetDistOn().GetBinContent( i ) > 0 ) {
607
      return Stash::Lambda( GetOffsetDistOn().GetBinLowEdge(i+1),
608
                            Stash::Angle::Degrees );
609
    }
610

    
611
  }
612

    
613
  // not found, this code shouldn't be reached
614
  WARN_OUT << "Cannot find a valid entry (entries: " 
615
           << GetOffsetDistOn().GetEntries()
616
           << ")" << endl;
617
  return Stash::Lambda(180, Stash::Angle::Degrees);
618

    
619
}
620

    
621

    
622
/**
623
 * \returns fraction of time the ON region spent above the given offset. 
624
 * This is a replacement for the old "PsiSqr" run cut, 
625
 * where runs further away than a given radius were thrown out. Instead, we check that the
626
 */
627
double
628
Background::BgStats::
629
GetFractionOfTimeAboveOffset( double offs_deg ) {
630

    
631

    
632
  // KPK: removed the offset_deg check below because it breaks the
633
  // skip-run logic. If we return 0 here, the run will never be
634
  // skipped, which isn't what is intended by this function... we
635
  // should fall back to an estimate instead.
636

    
637
  // if ( offs_deg > GetOffsetDistOn().GetBinLowEdge( GetOffsetDistOn().GetNbinsX() ) ) {
638
  //   WARN_OUT << "cannot test for offset angles of " << offs_deg << "deg; " 
639
  //              << "OffsetDistOn stops at " 
640
  //              << GetOffsetDistOn().GetBinLowEdge( GetOffsetDistOn().GetNbinsX() )
641
  //              << endl;
642
  //       return 0.0;
643
  // }
644

    
645
  Int_t bin = GetOffsetDistOn().FindBin( offs_deg );
646
  Int_t maxbin = GetOffsetDistOn().GetNbinsX();
647
  
648
  Double_t integral_above, integral;
649

    
650
  // note Integral() integrates the histogram while GetIntegral()
651
  // gives you the sum of all points filled including the overflow!
652

    
653
  integral_above = GetOffsetDistOn().Integral( bin, maxbin );
654
  integral = GetOffsetDistOn().Integral();
655

    
656
  double frac;
657

    
658
  if (integral>1.0e-10) 
659
    frac = (double)integral_above/(double)integral;
660
  else
661
    frac = 1.0; // all bad, since no data!
662

    
663
  return frac;
664

    
665
}
666

    
667
double 
668
Background::BgStats::
669
GetMeanMuonCorrection () {
670

    
671
  int count = 0;
672
  double avg = 0.;
673

    
674
  for(TelMap_t::iterator j = GetMuonCorrections().begin(); 
675
      j !=  GetMuonCorrections().end(); ++j) {        
676
    avg += (*j).second;
677
    count++;
678
  }
679

    
680
  if (count) avg /= count;
681

    
682
  return avg;
683
}
684

    
685

    
686
void 
687
Background::BgStats::
688
Draw(const Option_t* opt) {
689

    
690
  TCanvas *canv = new TCanvas("BgStatsCanvas","BgStatsCanvas",900,600);
691

    
692
  canv->Divide(3,2);
693

    
694
  canv->cd(1);
695
  GetZenithDistOn().Draw(opt);
696
  GetZenithDistOff().SetLineColor( kRed );
697
  GetZenithDistOff().Draw("same");
698

    
699
  canv->cd(2);
700
  GetAzimuthDistOn().Draw(opt);
701
  GetAzimuthDistOff().SetLineColor( kRed );
702
  GetAzimuthDistOff().Draw("same");
703

    
704
  canv->cd(3);
705
  GetOffsetDistOn().Draw(opt);
706
  GetOffsetDistOff().SetLineColor( kRed );
707
  GetOffsetDistOff().Draw("same");
708

    
709
  canv->cd(4);
710
  GetZenithOffsetDistOn().Draw("colz");
711

    
712
  canv->cd(5);
713
  GetZenithOffsetDistOff().Draw("colz");
714

    
715
}
716

    
717
/**
718
 * \brief Calculate mean azimuth angle taking the discontinuity at 0 deg = 360 deg into account.
719
 */
720
double Background::BgStats::MeanAzimuth(const TH1& azimuthDist) {
721

    
722
  // idea: sum up vectors on the unit circle for each histogram entry,
723
  // then calculate mean vector and transform back
724

    
725
  TVector2 weightedVector(0.,0.);
726
  double sumOfWeights = 0.0;
727

    
728
  for( int i=1 ; i<=azimuthDist.GetNbinsX() ; ++i ){
729

    
730
    double w = azimuthDist.GetBinContent(i);
731
    if( w == 0.0 ) continue;
732
    double d = azimuthDist.GetBinCenter(i);
733
    TVector2 v(TMath::Cos(d*TMath::DegToRad()),TMath::Sin(d*TMath::DegToRad()));
734
    
735
    weightedVector += (w*v);
736
    sumOfWeights   +=  w;
737
  }
738

    
739
  if( sumOfWeights <= 0.0 ){
740
    WARN_OUT << "Empty azimuth distribution??? Returning south." << std::endl;
741
    return 180.;
742
  }
743

    
744
  weightedVector /= sumOfWeights;
745

    
746
  double phi = weightedVector.Phi()*TMath::RadToDeg();
747

    
748
  return phi;
749
}
750

    
751
ClassImp(Background::BgStats)