//____________________________________________________________________
//
// Class for a pair of systematics
//
// The 'standard' case for most of the systematics. There exist two
// variations, up and down which will be used as variation templates.
// No computation needed.
// 
//  
// Author: Soeren Stamm <mailto: stamm@physik.hu-berlin.de>
// Update: $Id: AtlHistFactorySystPdf.cxx,v 1.3 2016/04/19 07:46:17 stamm Exp $
// Copyright: 2015 (C) Soeren Stamm
//
#ifndef ATLAS_AtlHistFactorySystPdf
#include <AtlHistFactorySystPdf.h>
#endif
#include <TFile.h>
#include <HepDataMCPlot.h>
#include <iostream>

using namespace std;

#ifndef __CINT__
ClassImp(AtlHistFactorySystPdf);
#endif

//____________________________________________________________________

AtlHistFactorySystPdf::AtlHistFactorySystPdf() {
    //
    // Default constructor
    //
}

//____________________________________________________________________

AtlHistFactorySystPdf::AtlHistFactorySystPdf(const char* systname,
					     const char *systtitle,
					     AtlTopLevelAnalysis::ESystematic SystUp,
					     AtlTopLevelAnalysis::ESystematic SystDown,
					     Bool_t useShape) :
    AtlHistFactorySystematic(systname, systtitle, useShape) {
    //
    // Default constructor
    //

    fSystUp      = SystUp;
    fSystDown    = SystDown;
    fNominal     = AtlTopLevelAnalysis::kNOMINAL;

    fPlotterFileUp   = 0;
    fPlotterFileDown = 0;
    fPlotterFileNom  = 0;

    fHistsUp   = 0;
    fHistsDown = 0;
    fHistsNom  = 0;
}

//____________________________________________________________________

AtlHistFactorySystPdf::~AtlHistFactorySystPdf() {
    //
    // Default destructor
    //
    
    if ( fPlotterFileUp   != 0 ) delete fPlotterFileUp;
    if ( fPlotterFileDown != 0 ) delete fPlotterFileDown;
    if ( fPlotterFileNom  != 0 ) delete fPlotterFileNom;

    if ( fHistsUp != 0 ) {
	fHistsUp->Delete();
	delete fHistsUp;
    }
    if ( fHistsDown != 0 ) {
	fHistsDown->Delete();
	delete fHistsDown;
    }    
    if ( fHistsNom != 0 ) {
	fHistsNom->Delete();
	delete fHistsNom;
    }    
}

//____________________________________________________________________

void AtlHistFactorySystPdf::Initialize(const char* BaseDir,
				       const char* scheme) {
    //
    // Initialize for MCPlotter files
    //
    // Set the MCPlotter files for the up-var. and down variation.
    //
    // The file directory is
    // 'BaseDir' + 'systname' + 'scheme' + "MCPlotter.root"
    //
    // Get the list of templates from the HepDataMCPlots
    //
    
    if ( fPlotterFileUp != 0 ) delete fPlotterFileUp;
    fPlotterFileUp = TFile::Open(Form("%s/%s/%s/MCPlotter.root",
				      BaseDir,
				      AtlTopLevelAnalysis::fgSystematicNames[fSystUp],
				      scheme));
    
    if ( fPlotterFileDown != 0 ) delete fPlotterFileDown;
    fPlotterFileDown = TFile::Open(Form("%s/%s/%s/MCPlotter.root",
					BaseDir,
					AtlTopLevelAnalysis::fgSystematicNames[fSystDown],
					scheme));

        if ( fPlotterFileNom != 0 ) delete fPlotterFileNom;
    fPlotterFileNom = TFile::Open(Form("%s/%s/%s/MCPlotter.root",
				       BaseDir,
				       AtlTopLevelAnalysis::fgSystematicNames[fNominal],
				       scheme));
    
    if ( fPlotterFileUp == 0 || fPlotterFileDown == 0 || fPlotterFileNom == 0 ) {
	Error("Initialize",
	      "Could not find MCPlotter files. Abort!");
	gSystem->Abort();
    }
    
    if ( fDiscriminant == 0 ) {
	Error("Initialize",
	      "Discriminant not set. Please use SetDiscriminant(..). Abort!");
	gSystem->Abort();
    }	
    
    HepDataMCPlot *HepUp = (HepDataMCPlot*) fPlotterFileUp->Get(fDiscriminant->Data());
    HepDataMCPlot *HepDown = (HepDataMCPlot*) fPlotterFileDown->Get(fDiscriminant->Data());
    HepDataMCPlot *HepNom = (HepDataMCPlot*) fPlotterFileNom->Get(fDiscriminant->Data());

    if ( HepUp == 0 || HepDown == 0 || HepNom == 0 ) {
	Error("Initialize",
	      "Could not find discriminant '%s' in file.",
	      fDiscriminant->Data());
	Error("Initialize", "Files are:\n%s\n%s",
	      fPlotterFileUp->GetName(),
	      fPlotterFileDown->GetName());
	gSystem->Abort();
    }

    fHistsUp   = HepUp->GetListOfMCTemplates(AtlTopLevelAnalysis::fgSystematicNames[fSystUp]);
    fHistsDown = HepDown->GetListOfMCTemplates(AtlTopLevelAnalysis::fgSystematicNames[fSystDown]);
    fHistsNom  = HepNom->GetListOfMCTemplates(AtlTopLevelAnalysis::fgSystematicNames[fNominal]);
}

//____________________________________________________________________

void AtlHistFactorySystPdf::GetHistsFromFile(const char* process) {
    //
    // Change the process and load the corresponding informations
    // into the class data members.
    //
    
    // we get the histos directly from the template file
    fHistUp = (TH1*) fTemplateFile->Get(Form("%s_%s",
					     process,
					     AtlTopLevelAnalysis::fgSystematicNames[fSystUp]));
    fHistDown = (TH1*) fTemplateFile->Get(Form("%s_%s",
					       process,
					       AtlTopLevelAnalysis::fgSystematicNames[fSystDown]));
}

//____________________________________________________________________

void AtlHistFactorySystPdf::ComputeUpDownVariation(const char* process) {
    //
    // Compute the up var. and down variation for this systematic pair
    //

    // The Pdf uncertainty does not necessarily use the nominal samples
    // Therefore, compute the relative uncertainty per bin and then
    // use the nominal template to get the correct shape

    TH1 *h_pdf_up = (TH1*) fHistsUp->FindObject(Form("%s_%s",
						     process,
						     AtlTopLevelAnalysis::fgSystematicNames[fSystUp]));
    TH1 *h_pdf_down = (TH1*) fHistsDown->FindObject(Form("%s_%s",
							 process,
							 AtlTopLevelAnalysis::fgSystematicNames[fSystDown]));
    TH1 *h_nom = (TH1*) fHistsNom->FindObject(Form("%s_%s",
						   process,
						   AtlTopLevelAnalysis::fgSystematicNames[fNominal]));
    
    // Compute the center of the envelope
    // (up + down)/2.
    TH1 *h_pdf_center = (TH1*) h_pdf_up->Clone("pdf_center");
    h_pdf_center->Add(h_pdf_down);
    h_pdf_center->Scale(0.5);
    
    // Compute the relative uncertainty per bin
    TH1 *h_pdf_rel = (TH1*) h_pdf_up->Clone("pdf_rel");
    h_pdf_rel->Add(h_pdf_center, -1.);
    h_pdf_rel->Divide(h_pdf_center);

    // Compute the pdf templates
    fHistUp   = (TH1*) h_nom->Clone(Form("%s_%s",
    					 process,
    					 AtlTopLevelAnalysis::fgSystematicNames[fSystUp]));
    fHistDown = (TH1*) h_nom->Clone(Form("%s_%s",
    					 process,
    					 AtlTopLevelAnalysis::fgSystematicNames[fSystDown]));

    // pdf_up/down = (1 +- pdf_rel)*nominal
    fHistUp->Multiply(h_pdf_rel);
    fHistUp->Add(h_nom);
    
    fHistDown->Multiply(h_pdf_rel);
    fHistDown->Scale(-1.);
    fHistDown->Add(h_nom);
}

//____________________________________________________________________

void AtlHistFactorySystPdf::Clear(Option_t *option) {
    //
    // Close MCPlottr files
    // - Do not close the template file. The class does not own this
    //   file. The owner of this file is responsible of closing it.
    //

    if ( fPlotterFileUp != 0 ) {
	fPlotterFileUp->Close();
	delete fPlotterFileUp;
	fPlotterFileUp = 0;
    }

    if ( fPlotterFileDown != 0 ) {
	fPlotterFileDown->Close();
	delete fPlotterFileDown;
	fPlotterFileDown = 0;
    }

    if ( fPlotterFileNom != 0 ) {
	fPlotterFileNom->Close();
	delete fPlotterFileNom;
	fPlotterFileNom = 0;
    }

    if ( fHistsUp != 0 ) {
	// fHistsUp->Delete();
	delete fHistsUp;
	fHistsUp = 0;
    }

    if ( fHistsDown != 0 ) {
	// fHistsDown->Delete();
	delete fHistsDown;
	fHistsDown = 0;
    }
    
    if ( fHistsNom != 0 ) {
	// fHistsNom->Delete();
	delete fHistsNom;
	fHistsNom = 0;
    }

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