//____________________________________________________________________
//
// Class for storing HistFactory sample information
//
// The normfactor for each sample will be used to scale the
// expected number of events of this sample. The norm uncertainty
// (e.g. theory unc.) can be used to constrain the normfactor.
// Currently the distribution is fixed in the AtlHistFactoryTask.
//
// Each sample can hold its own list of systematics.
// - see AtlHistFactoryTask for more details
//
// If 'isSignal' is true, the normfactor will be unconstrained,
// even if a norm unc. is provided.
//
// The sample can also be used to save the results of the shape
// tests.
//
//  
// Author: Soeren Stamm <mailto: stamm@physik.hu-berlin.de>
// Update: $Id: AtlHistFactorySample.cxx,v 1.8 2016/04/19 07:47:50 stamm Exp $
// Copyright: 2015 (C) Soeren Stamm
//
#ifndef ATLAS_AtlHistFactorySample
#include <AtlHistFactorySample.h>
#endif
#include <AtlHistFactorySystematic.h>
#include <TList.h>
#include <TH1F.h>
#include <TFile.h>
#include <iostream>
#include <TSystem.h>

using namespace std;

#ifndef __CINT__
ClassImp(AtlHistFactorySample);
#endif

//____________________________________________________________________

AtlHistFactorySample::AtlHistFactorySample(const char* name,
					   const char* title,
					   const char* normfactor,
					   Double_t norm_unc,
					   Bool_t isSignal) :
    TNamed(name, title), fNormFactor(normfactor) {
    //
    // Default constructor
    //
    fNormUncertainty = norm_unc;

    fIsSignal = isSignal;
    fIsData = kFALSE; // assume this is a MC sample (default)
    
    fListOfSystematics = new TList;

    fHist_ShapeTest_pValue = 0;
    fHist_ShapeTest_KStest = 0;

    // Default signal range
    // Assuming histograms are always normalized to Lumi
    fSignalLow  = 0.;
    fSignalHigh = 5.;

    fUseLogNormal = kFALSE;
    fUseStatError = kTRUE;

    // Default draw style
    fFillColor = kWhite;
    fFillStyle = 0;
    fLineColor = kBlack;
    fLineStyle = kSolid;
}

//____________________________________________________________________

AtlHistFactorySample::~AtlHistFactorySample() {
    //
    // Default constructor
    //
    delete fListOfSystematics;

    if ( fHist_ShapeTest_pValue != 0 )
	delete fHist_ShapeTest_pValue;
    if ( fHist_ShapeTest_KStest != 0 )
	delete fHist_ShapeTest_KStest;
}

//____________________________________________________________________

void AtlHistFactorySample::AddSystematic(AtlHistFactorySystematic *syst) {
    //
    // Add Systematic to channel
    //
    
    fListOfSystematics->Add(syst);
}

//____________________________________________________________________

void AtlHistFactorySample::AddSystematics(TList *list) {
    //
    // Add Systematic to channel
    //
    
    fListOfSystematics->AddAll(list);
}

//____________________________________________________________________

void AtlHistFactorySample::BookHistograms(const char* channel) {
    //
    // Book histograms for this channel
    // They have only one bin, because TH1::Fill(name, weight)
    // will be used to add more bins as needed.
    
    // Delete any existing histograms
    if ( fHist_ShapeTest_pValue != 0 ) {
	delete fHist_ShapeTest_pValue;
	fHist_ShapeTest_pValue = 0;
    }
    
    fHist_ShapeTest_pValue = new TH1F(Form("%s_%s_pValue",
					   channel,
					   fName.Data()),
				      Form("p-value of Chi2 test for channel '%s'", channel),
				      1, 0., 1.);

    if ( fHist_ShapeTest_KStest != 0 ) {
	delete fHist_ShapeTest_KStest;
	fHist_ShapeTest_KStest = 0;
    }
    fHist_ShapeTest_KStest = new TH1F(Form("%s_%s_KStest",
					   channel,
					   fName.Data()),
				      Form("p-value of Chi2 test for channel '%s'", channel),
				      1, 0., 1.);
}

//____________________________________________________________________

void AtlHistFactorySample::FillHistograms(const char* syst,
					  Double_t pval,
					  Double_t kstest) {
    //
    // Fill shape test histograms
    //
    
    fHist_ShapeTest_pValue->Fill(syst, pval);
    fHist_ShapeTest_KStest->Fill(syst, kstest);
}

//____________________________________________________________________

void AtlHistFactorySample::WriteHistograms(TFile *fout) {
    //
    // Save histograms to file
    //

    // Change current directory to file in order to use TH1::Write()
    fout->cd();

    // Save an unsorted version
    TH1F *hist_pValue_unsort = (TH1F*) fHist_ShapeTest_pValue->Clone(Form("%s_unsrt", fHist_ShapeTest_pValue->GetName()));
    hist_pValue_unsort->LabelsDeflate();
    hist_pValue_unsort->SetMarkerStyle(20);
    hist_pValue_unsort->SetMarkerColor(kBlue);
    hist_pValue_unsort->LabelsOption("v");
    hist_pValue_unsort->Write();
    
    // Set some basic options
    fHist_ShapeTest_pValue->LabelsDeflate();
    fHist_ShapeTest_pValue->LabelsOption("<v");
    fHist_ShapeTest_pValue->SetMarkerStyle(20);
    fHist_ShapeTest_pValue->SetMarkerColor(kBlue);
    
    fHist_ShapeTest_pValue->Write();

    TH1F *hist_KStest_unsort = (TH1F*) fHist_ShapeTest_KStest->Clone(Form("%s_unsrt", fHist_ShapeTest_KStest->GetName()));
    hist_KStest_unsort->LabelsDeflate();
    hist_KStest_unsort->SetMarkerStyle(20);
    hist_KStest_unsort->SetMarkerColor(kBlue);
    hist_KStest_unsort->LabelsOption("v");
    hist_KStest_unsort->Write();

    // Set some basic options
    fHist_ShapeTest_KStest->LabelsDeflate();
    fHist_ShapeTest_KStest->LabelsOption("<v");
    fHist_ShapeTest_KStest->SetMarkerStyle(20);
    fHist_ShapeTest_KStest->SetMarkerColor(kBlue);
    
    fHist_ShapeTest_KStest->Write();
}

//____________________________________________________________________

void AtlHistFactorySample::SetSignalRange(Double_t low, Double_t high) {
    //
    // Defines the range for the NormFactor for this sample. Values
    // set here, only apply for signal samples.
    //
    // Background samples are constrained by the norm uncertainty,
    // which is provided during construction of this object.
    //

    if ( low > high ) {
	Error("SetSignalRange",
	      "Lower bound is greater than upper bound. Abort!");
	gSystem->Abort();
    }	      

    fSignalLow  = low;
    fSignalHigh = high;
}

//____________________________________________________________________

void AtlHistFactorySample::AddShapeSystematic(const char* name,
					      TList *systs) {
    //
    // Add shape systematic to this sample
    //
    // The systematic is taken from the provided list, cloned and then
    // added to the list of systematics.
    //
    
    AtlHistFactorySystematic *syst_org   = (AtlHistFactorySystematic*) systs->FindObject(name);
    if ( syst_org == 0 ) {
	Error("AddShapeSystematic",
	      "Could not find any sytematic with name %s. Abort!",
	      name);
	gSystem->Abort();
    }
    AtlHistFactorySystematic *syst_shape = (AtlHistFactorySystematic*) syst_org->Clone();
    syst_shape->SetUseShape(kTRUE);

    fListOfSystematics->Add(syst_shape);
}
 AtlHistFactorySample.cxx:1
 AtlHistFactorySample.cxx:2
 AtlHistFactorySample.cxx:3
 AtlHistFactorySample.cxx:4
 AtlHistFactorySample.cxx:5
 AtlHistFactorySample.cxx:6
 AtlHistFactorySample.cxx:7
 AtlHistFactorySample.cxx:8
 AtlHistFactorySample.cxx:9
 AtlHistFactorySample.cxx:10
 AtlHistFactorySample.cxx:11
 AtlHistFactorySample.cxx:12
 AtlHistFactorySample.cxx:13
 AtlHistFactorySample.cxx:14
 AtlHistFactorySample.cxx:15
 AtlHistFactorySample.cxx:16
 AtlHistFactorySample.cxx:17
 AtlHistFactorySample.cxx:18
 AtlHistFactorySample.cxx:19
 AtlHistFactorySample.cxx:20
 AtlHistFactorySample.cxx:21
 AtlHistFactorySample.cxx:22
 AtlHistFactorySample.cxx:23
 AtlHistFactorySample.cxx:24
 AtlHistFactorySample.cxx:25
 AtlHistFactorySample.cxx:26
 AtlHistFactorySample.cxx:27
 AtlHistFactorySample.cxx:28
 AtlHistFactorySample.cxx:29
 AtlHistFactorySample.cxx:30
 AtlHistFactorySample.cxx:31
 AtlHistFactorySample.cxx:32
 AtlHistFactorySample.cxx:33
 AtlHistFactorySample.cxx:34
 AtlHistFactorySample.cxx:35
 AtlHistFactorySample.cxx:36
 AtlHistFactorySample.cxx:37
 AtlHistFactorySample.cxx:38
 AtlHistFactorySample.cxx:39
 AtlHistFactorySample.cxx:40
 AtlHistFactorySample.cxx:41
 AtlHistFactorySample.cxx:42
 AtlHistFactorySample.cxx:43
 AtlHistFactorySample.cxx:44
 AtlHistFactorySample.cxx:45
 AtlHistFactorySample.cxx:46
 AtlHistFactorySample.cxx:47
 AtlHistFactorySample.cxx:48
 AtlHistFactorySample.cxx:49
 AtlHistFactorySample.cxx:50
 AtlHistFactorySample.cxx:51
 AtlHistFactorySample.cxx:52
 AtlHistFactorySample.cxx:53
 AtlHistFactorySample.cxx:54
 AtlHistFactorySample.cxx:55
 AtlHistFactorySample.cxx:56
 AtlHistFactorySample.cxx:57
 AtlHistFactorySample.cxx:58
 AtlHistFactorySample.cxx:59
 AtlHistFactorySample.cxx:60
 AtlHistFactorySample.cxx:61
 AtlHistFactorySample.cxx:62
 AtlHistFactorySample.cxx:63
 AtlHistFactorySample.cxx:64
 AtlHistFactorySample.cxx:65
 AtlHistFactorySample.cxx:66
 AtlHistFactorySample.cxx:67
 AtlHistFactorySample.cxx:68
 AtlHistFactorySample.cxx:69
 AtlHistFactorySample.cxx:70
 AtlHistFactorySample.cxx:71
 AtlHistFactorySample.cxx:72
 AtlHistFactorySample.cxx:73
 AtlHistFactorySample.cxx:74
 AtlHistFactorySample.cxx:75
 AtlHistFactorySample.cxx:76
 AtlHistFactorySample.cxx:77
 AtlHistFactorySample.cxx:78
 AtlHistFactorySample.cxx:79
 AtlHistFactorySample.cxx:80
 AtlHistFactorySample.cxx:81
 AtlHistFactorySample.cxx:82
 AtlHistFactorySample.cxx:83
 AtlHistFactorySample.cxx:84
 AtlHistFactorySample.cxx:85
 AtlHistFactorySample.cxx:86
 AtlHistFactorySample.cxx:87
 AtlHistFactorySample.cxx:88
 AtlHistFactorySample.cxx:89
 AtlHistFactorySample.cxx:90
 AtlHistFactorySample.cxx:91
 AtlHistFactorySample.cxx:92
 AtlHistFactorySample.cxx:93
 AtlHistFactorySample.cxx:94
 AtlHistFactorySample.cxx:95
 AtlHistFactorySample.cxx:96
 AtlHistFactorySample.cxx:97
 AtlHistFactorySample.cxx:98
 AtlHistFactorySample.cxx:99
 AtlHistFactorySample.cxx:100
 AtlHistFactorySample.cxx:101
 AtlHistFactorySample.cxx:102
 AtlHistFactorySample.cxx:103
 AtlHistFactorySample.cxx:104
 AtlHistFactorySample.cxx:105
 AtlHistFactorySample.cxx:106
 AtlHistFactorySample.cxx:107
 AtlHistFactorySample.cxx:108
 AtlHistFactorySample.cxx:109
 AtlHistFactorySample.cxx:110
 AtlHistFactorySample.cxx:111
 AtlHistFactorySample.cxx:112
 AtlHistFactorySample.cxx:113
 AtlHistFactorySample.cxx:114
 AtlHistFactorySample.cxx:115
 AtlHistFactorySample.cxx:116
 AtlHistFactorySample.cxx:117
 AtlHistFactorySample.cxx:118
 AtlHistFactorySample.cxx:119
 AtlHistFactorySample.cxx:120
 AtlHistFactorySample.cxx:121
 AtlHistFactorySample.cxx:122
 AtlHistFactorySample.cxx:123
 AtlHistFactorySample.cxx:124
 AtlHistFactorySample.cxx:125
 AtlHistFactorySample.cxx:126
 AtlHistFactorySample.cxx:127
 AtlHistFactorySample.cxx:128
 AtlHistFactorySample.cxx:129
 AtlHistFactorySample.cxx:130
 AtlHistFactorySample.cxx:131
 AtlHistFactorySample.cxx:132
 AtlHistFactorySample.cxx:133
 AtlHistFactorySample.cxx:134
 AtlHistFactorySample.cxx:135
 AtlHistFactorySample.cxx:136
 AtlHistFactorySample.cxx:137
 AtlHistFactorySample.cxx:138
 AtlHistFactorySample.cxx:139
 AtlHistFactorySample.cxx:140
 AtlHistFactorySample.cxx:141
 AtlHistFactorySample.cxx:142
 AtlHistFactorySample.cxx:143
 AtlHistFactorySample.cxx:144
 AtlHistFactorySample.cxx:145
 AtlHistFactorySample.cxx:146
 AtlHistFactorySample.cxx:147
 AtlHistFactorySample.cxx:148
 AtlHistFactorySample.cxx:149
 AtlHistFactorySample.cxx:150
 AtlHistFactorySample.cxx:151
 AtlHistFactorySample.cxx:152
 AtlHistFactorySample.cxx:153
 AtlHistFactorySample.cxx:154
 AtlHistFactorySample.cxx:155
 AtlHistFactorySample.cxx:156
 AtlHistFactorySample.cxx:157
 AtlHistFactorySample.cxx:158
 AtlHistFactorySample.cxx:159
 AtlHistFactorySample.cxx:160
 AtlHistFactorySample.cxx:161
 AtlHistFactorySample.cxx:162
 AtlHistFactorySample.cxx:163
 AtlHistFactorySample.cxx:164
 AtlHistFactorySample.cxx:165
 AtlHistFactorySample.cxx:166
 AtlHistFactorySample.cxx:167
 AtlHistFactorySample.cxx:168
 AtlHistFactorySample.cxx:169
 AtlHistFactorySample.cxx:170
 AtlHistFactorySample.cxx:171
 AtlHistFactorySample.cxx:172
 AtlHistFactorySample.cxx:173
 AtlHistFactorySample.cxx:174
 AtlHistFactorySample.cxx:175
 AtlHistFactorySample.cxx:176
 AtlHistFactorySample.cxx:177
 AtlHistFactorySample.cxx:178
 AtlHistFactorySample.cxx:179
 AtlHistFactorySample.cxx:180
 AtlHistFactorySample.cxx:181
 AtlHistFactorySample.cxx:182
 AtlHistFactorySample.cxx:183
 AtlHistFactorySample.cxx:184
 AtlHistFactorySample.cxx:185
 AtlHistFactorySample.cxx:186
 AtlHistFactorySample.cxx:187
 AtlHistFactorySample.cxx:188
 AtlHistFactorySample.cxx:189
 AtlHistFactorySample.cxx:190
 AtlHistFactorySample.cxx:191
 AtlHistFactorySample.cxx:192
 AtlHistFactorySample.cxx:193
 AtlHistFactorySample.cxx:194
 AtlHistFactorySample.cxx:195
 AtlHistFactorySample.cxx:196
 AtlHistFactorySample.cxx:197
 AtlHistFactorySample.cxx:198
 AtlHistFactorySample.cxx:199
 AtlHistFactorySample.cxx:200
 AtlHistFactorySample.cxx:201
 AtlHistFactorySample.cxx:202
 AtlHistFactorySample.cxx:203
 AtlHistFactorySample.cxx:204
 AtlHistFactorySample.cxx:205
 AtlHistFactorySample.cxx:206
 AtlHistFactorySample.cxx:207
 AtlHistFactorySample.cxx:208
 AtlHistFactorySample.cxx:209
 AtlHistFactorySample.cxx:210
 AtlHistFactorySample.cxx:211
 AtlHistFactorySample.cxx:212
 AtlHistFactorySample.cxx:213
 AtlHistFactorySample.cxx:214
 AtlHistFactorySample.cxx:215
 AtlHistFactorySample.cxx:216
 AtlHistFactorySample.cxx:217
 AtlHistFactorySample.cxx:218
 AtlHistFactorySample.cxx:219
 AtlHistFactorySample.cxx:220
 AtlHistFactorySample.cxx:221
 AtlHistFactorySample.cxx:222
 AtlHistFactorySample.cxx:223
 AtlHistFactorySample.cxx:224
 AtlHistFactorySample.cxx:225
 AtlHistFactorySample.cxx:226
 AtlHistFactorySample.cxx:227
 AtlHistFactorySample.cxx:228
 AtlHistFactorySample.cxx:229
 AtlHistFactorySample.cxx:230
 AtlHistFactorySample.cxx:231
 AtlHistFactorySample.cxx:232
 AtlHistFactorySample.cxx:233
 AtlHistFactorySample.cxx:234
 AtlHistFactorySample.cxx:235
 AtlHistFactorySample.cxx:236
 AtlHistFactorySample.cxx:237
 AtlHistFactorySample.cxx:238
 AtlHistFactorySample.cxx:239