//  
// Author: Soeren Stamm <mailto: stamm@physik.hu-berlin.de>
// Update: $Id: AtlHistFactoryTask.h,v 1.15 2016/04/19 07:55:12 stamm Exp $
// Copyright: 2015 (C) Soeren Stamm
//
#ifndef ATLAS_AtlHistFactoryTask
#define ATLAS_AtlHistFactoryTask
#ifndef ATLAS_AtlTask
#include <AtlTask.h>
#endif
#include "RooStats/HistFactory/Measurement.h"
#include "RooStats/HistFactory/MakeModelAndMeasurementsFast.h"
#include <RooStats/ModelConfig.h>
#include <RooAbsData.h>

class AtlHistFactoryMeasurement;
class TList;
class TGraph;

class AtlHistFactoryTask : public AtlTask {
    
public:
    enum ERunMode {
	kUnknown,
	kCreateFitTemplates,
 	kCreateShapeTemplates,
	kPerformShapeTests,
 	kCreateWorkspace,
	kFitModel,
	kRateTable,	
	kHypoTestLimit };
    
protected:
    ERunMode fRunMode;
    AtlHistFactoryMeasurement *fMeasurement;
    TString *fHistFactoryDir;
    TString *fTemplateDir;
    Int_t fNPullsPerPad;       // Max. number of pulls per pad
    Double_t fPullScaleFactor; // Scale factor between pull x-axis and impact on poi x-axis
    TString *fScheme; // Name of plotting scheme
    
public:
    AtlHistFactoryTask(const char* name, const char* title);
    virtual ~AtlHistFactoryTask();

    virtual void Initialize();
    void SetRunMode(ERunMode mode); // *TOOGLE*

    virtual void ExecInteractiveJob(Option_t *option);
    virtual void ExecGridJob(Option_t *option);
    virtual Bool_t ExecBatchJob(Option_t *option);
    virtual void ExecNAFBatchJob(Option_t *option);
    virtual void CreateRunScript(Option_t *option = "");
    virtual void CreateNAFBatchRunScript();
    virtual void CreateGridRunScript();
    inline void SetMeasurement(AtlHistFactoryMeasurement *meas)
	{ fMeasurement = meas; }
    
protected:
    void CreateTemplates();
    void CreateWorkspace();
    void ExportRateUncertainties();
    void ExportPullPlots(const char* outfile,
			 TH1* h_pulls,
			 TH1* h_pulls_err_up,
			 TH1* h_pulls_err_down,
			 TH1* h_prefit_impact_up,
			 TH1* h_prefit_impact_down,
			 TH1* h_postfit_impact_up,
			 TH1* h_postfit_impact_down);
    void FitModel();
    void RunLimit();

    void GetTemplatesFileName(const char* channel,
			      TString &filename);
    void GetShapeTemplatesFileName(const char* channel,
				   TString &filename);
    void GetImpactOnPOI(RooWorkspace *wSpace, const char* data_name,
			const char* snapshot, RooRealVar *par,
			const char* poi_name, Double_t variation,
			TH1F *hresult, const char* logdir);
    TList* MergeListOfSystematics(TList *ch_systs,
				  TList *sample_systs);
    void PerformShapeTests();
    void RunFit(const char* ws_filename,
		const char* ws_name,
		const char* data_name);
    void CreatePullPlots(const char* ws_filename,
			 const char* ws_name,
			 const char* data_name);
    Bool_t IsNormParameter(const char* name);

    Double_t GetNormUncertainty(const char* name);
    Double_t GetGammaUncertainty(const char* name,
				 RooWorkspace *wSpace);
    TString* GetSystematicName(const char* par_name);
    TString* GetDirectoryName(const char* name);
    
    ClassDef(AtlHistFactoryTask, 0) // DOCUMENT ME
};
#endif

 AtlHistFactoryTask.h:1
 AtlHistFactoryTask.h:2
 AtlHistFactoryTask.h:3
 AtlHistFactoryTask.h:4
 AtlHistFactoryTask.h:5
 AtlHistFactoryTask.h:6
 AtlHistFactoryTask.h:7
 AtlHistFactoryTask.h:8
 AtlHistFactoryTask.h:9
 AtlHistFactoryTask.h:10
 AtlHistFactoryTask.h:11
 AtlHistFactoryTask.h:12
 AtlHistFactoryTask.h:13
 AtlHistFactoryTask.h:14
 AtlHistFactoryTask.h:15
 AtlHistFactoryTask.h:16
 AtlHistFactoryTask.h:17
 AtlHistFactoryTask.h:18
 AtlHistFactoryTask.h:19
 AtlHistFactoryTask.h:20
 AtlHistFactoryTask.h:21
 AtlHistFactoryTask.h:22
 AtlHistFactoryTask.h:23
 AtlHistFactoryTask.h:24
 AtlHistFactoryTask.h:25
 AtlHistFactoryTask.h:26
 AtlHistFactoryTask.h:27
 AtlHistFactoryTask.h:28
 AtlHistFactoryTask.h:29
 AtlHistFactoryTask.h:30
 AtlHistFactoryTask.h:31
 AtlHistFactoryTask.h:32
 AtlHistFactoryTask.h:33
 AtlHistFactoryTask.h:34
 AtlHistFactoryTask.h:35
 AtlHistFactoryTask.h:36
 AtlHistFactoryTask.h:37
 AtlHistFactoryTask.h:38
 AtlHistFactoryTask.h:39
 AtlHistFactoryTask.h:40
 AtlHistFactoryTask.h:41
 AtlHistFactoryTask.h:42
 AtlHistFactoryTask.h:43
 AtlHistFactoryTask.h:44
 AtlHistFactoryTask.h:45
 AtlHistFactoryTask.h:46
 AtlHistFactoryTask.h:47
 AtlHistFactoryTask.h:48
 AtlHistFactoryTask.h:49
 AtlHistFactoryTask.h:50
 AtlHistFactoryTask.h:51
 AtlHistFactoryTask.h:52
 AtlHistFactoryTask.h:53
 AtlHistFactoryTask.h:54
 AtlHistFactoryTask.h:55
 AtlHistFactoryTask.h:56
 AtlHistFactoryTask.h:57
 AtlHistFactoryTask.h:58
 AtlHistFactoryTask.h:59
 AtlHistFactoryTask.h:60
 AtlHistFactoryTask.h:61
 AtlHistFactoryTask.h:62
 AtlHistFactoryTask.h:63
 AtlHistFactoryTask.h:64
 AtlHistFactoryTask.h:65
 AtlHistFactoryTask.h:66
 AtlHistFactoryTask.h:67
 AtlHistFactoryTask.h:68
 AtlHistFactoryTask.h:69
 AtlHistFactoryTask.h:70
 AtlHistFactoryTask.h:71
 AtlHistFactoryTask.h:72
 AtlHistFactoryTask.h:73
 AtlHistFactoryTask.h:74
 AtlHistFactoryTask.h:75
 AtlHistFactoryTask.h:76
 AtlHistFactoryTask.h:77
 AtlHistFactoryTask.h:78
 AtlHistFactoryTask.h:79
 AtlHistFactoryTask.h:80
 AtlHistFactoryTask.h:81
 AtlHistFactoryTask.h:82
 AtlHistFactoryTask.h:83
 AtlHistFactoryTask.h:84
 AtlHistFactoryTask.h:85
 AtlHistFactoryTask.h:86
 AtlHistFactoryTask.h:87
 AtlHistFactoryTask.h:88
 AtlHistFactoryTask.h:89
 AtlHistFactoryTask.h:90
 AtlHistFactoryTask.h:91
 AtlHistFactoryTask.h:92
 AtlHistFactoryTask.h:93
 AtlHistFactoryTask.h:94
 AtlHistFactoryTask.h:95
 AtlHistFactoryTask.h:96
 AtlHistFactoryTask.h:97
 AtlHistFactoryTask.h:98
 AtlHistFactoryTask.h:99
 AtlHistFactoryTask.h:100
 AtlHistFactoryTask.h:101
 AtlHistFactoryTask.h:102