// @(#)root/roostats:$Id:  cranmer $
// Author: Kyle Cranmer, Akira Shibata
/*************************************************************************
 * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOSTATS_HISTOTOWORKSPACEFACTORYFAST
#define ROOSTATS_HISTOTOWORKSPACEFACTORYFAST

#include <vector>
#include <string>
#include <map>
#include <iostream>
#include <sstream>

#include <RooPlot.h>
#include <RooArgSet.h>
#include <RooFitResult.h>
#include <RooAbsReal.h>
#include <RooRealVar.h>
#include <RooWorkspace.h>
#include <TObject.h>
#include <TH1.h>
#include <TDirectory.h>

#include "RooStats/HistFactory/Systematics.h"
class ParamHistFunc;


namespace RooStats{
  namespace HistFactory{

    // Forward Declarations FTW
    class Measurement;
    class Channel;
    class Sample;

    class HistoToWorkspaceFactoryFast: public TObject {
    
    public:


      HistoToWorkspaceFactoryFast();
      HistoToWorkspaceFactoryFast(  RooStats::HistFactory::Measurement& Meas );
      virtual ~HistoToWorkspaceFactoryFast();

      static void ConfigureWorkspaceForMeasurement( const std::string& ModelName, 
						    RooWorkspace* ws_single, 
						    Measurement& measurement );
    
      RooWorkspace* MakeSingleChannelModel( Measurement& measurement, Channel& channel );
      RooWorkspace*  MakeCombinedModel(std::vector<std::string>, std::vector<RooWorkspace*>);
    
      static RooWorkspace* MakeCombinedModel( Measurement& measurement );
      static void PrintCovarianceMatrix(RooFitResult* result, RooArgSet* params, 
					std::string filename);

      void SetFunctionsToPreprocess(std::vector<std::string> lines) { fPreprocessFunctions=lines; }

    protected:

       void AddConstraintTerms(RooWorkspace* proto, Measurement& measurement, std::string prefix, std::string interpName,
			      std::vector<OverallSys>& systList, 			 
			      std::vector<std::string>& likelihoodTermNames, 
			      std::vector<std::string>& totSystTermNames);

      std::string AddNormFactor(RooWorkspace* proto, std::string& channel, 
				std::string& sigmaEpsilon, Sample& sample, bool doRatio);

      void AddMultiVarGaussConstraint(RooWorkspace* proto, std::string prefix, 
				      int lowBin, int highBin, 
				      std::vector<std::string>& likelihoodTermNames);
    
      void AddPoissonTerms(RooWorkspace* proto, std::string prefix, std::string obsPrefix, 
			   std::string expPrefix, int lowBin, int highBin,
			   std::vector<std::string>& likelihoodTermNames);
    
      static void EditSyst(RooWorkspace* proto, const char* pdfNameChar, 
			   std::map<std::string,double> gammaSyst, 
			   std::map<std::string,double> uniformSyst, 
			   std::map<std::string,double> logNormSyst, 
			   std::map<std::string,double> noSyst);

      void LinInterpWithConstraint(RooWorkspace* proto, TH1* nominal, std::vector<HistoSys>,  
				   std::string prefix, std::string productPrefix, 
				   std::string systTerm, 
				   std::vector<std::string>& likelihoodTermNames);

      RooWorkspace* MakeSingleChannelWorkspace(Measurement& measurement, Channel& channel);

      void MakeTotalExpected(RooWorkspace* proto, std::string totName, 
			     std::vector<std::string>& syst_x_expectedPrefixNames,
			     std::vector<std::string>& normByNames);
    
      RooDataSet* MergeDataSets(RooWorkspace* combined,
				std::vector<RooWorkspace*> wspace_vec, 
				std::vector<std::string> channel_names, 
				std::string dataSetName,
				RooArgList obsList,
				RooCategory* channelCat);

      void ProcessExpectedHisto(TH1* hist, RooWorkspace* proto, std::string prefix, 
				std::string productPrefix, std::string systTerm );

      void SetObsToExpected(RooWorkspace* proto, std::string obsPrefix, std::string expPrefix, 
			    int lowBin, int highBin);

      TH1* MakeScaledUncertaintyHist(const std::string& Name, 
				     std::vector< std::pair<TH1*, TH1*> > HistVec );

      TH1* MakeAbsolUncertaintyHist( const std::string& Name, const TH1* Hist );

      RooArgList createStatConstraintTerms( RooWorkspace* proto, 
					    std::vector<std::string>& constraintTerms, 
					    ParamHistFunc& paramHist, TH1* uncertHist, 
					    Constraint::Type type, Double_t minSigma );

      void ConfigureHistFactoryDataset(RooDataSet* obsData, TH1* nominal, RooWorkspace* proto,
				       std::vector<std::string> obsNameVec);
    
      std::vector<std::string> fSystToFix;
      std::map<std::string, double> fParamValues;
      double fNomLumi;
      double fLumiError;
      int fLowBin; 
      int fHighBin;    

    private:
    
      void GuessObsNameVec(TH1* hist);
    
      std::vector<std::string> fObsNameVec;
      std::string fObsName;
      std::vector<std::string> fPreprocessFunctions;
    
      ClassDef(RooStats::HistFactory::HistoToWorkspaceFactoryFast,3)
    };
  
  }
}

#endif
 HistoToWorkspaceFactoryFast.h:1
 HistoToWorkspaceFactoryFast.h:2
 HistoToWorkspaceFactoryFast.h:3
 HistoToWorkspaceFactoryFast.h:4
 HistoToWorkspaceFactoryFast.h:5
 HistoToWorkspaceFactoryFast.h:6
 HistoToWorkspaceFactoryFast.h:7
 HistoToWorkspaceFactoryFast.h:8
 HistoToWorkspaceFactoryFast.h:9
 HistoToWorkspaceFactoryFast.h:10
 HistoToWorkspaceFactoryFast.h:11
 HistoToWorkspaceFactoryFast.h:12
 HistoToWorkspaceFactoryFast.h:13
 HistoToWorkspaceFactoryFast.h:14
 HistoToWorkspaceFactoryFast.h:15
 HistoToWorkspaceFactoryFast.h:16
 HistoToWorkspaceFactoryFast.h:17
 HistoToWorkspaceFactoryFast.h:18
 HistoToWorkspaceFactoryFast.h:19
 HistoToWorkspaceFactoryFast.h:20
 HistoToWorkspaceFactoryFast.h:21
 HistoToWorkspaceFactoryFast.h:22
 HistoToWorkspaceFactoryFast.h:23
 HistoToWorkspaceFactoryFast.h:24
 HistoToWorkspaceFactoryFast.h:25
 HistoToWorkspaceFactoryFast.h:26
 HistoToWorkspaceFactoryFast.h:27
 HistoToWorkspaceFactoryFast.h:28
 HistoToWorkspaceFactoryFast.h:29
 HistoToWorkspaceFactoryFast.h:30
 HistoToWorkspaceFactoryFast.h:31
 HistoToWorkspaceFactoryFast.h:32
 HistoToWorkspaceFactoryFast.h:33
 HistoToWorkspaceFactoryFast.h:34
 HistoToWorkspaceFactoryFast.h:35
 HistoToWorkspaceFactoryFast.h:36
 HistoToWorkspaceFactoryFast.h:37
 HistoToWorkspaceFactoryFast.h:38
 HistoToWorkspaceFactoryFast.h:39
 HistoToWorkspaceFactoryFast.h:40
 HistoToWorkspaceFactoryFast.h:41
 HistoToWorkspaceFactoryFast.h:42
 HistoToWorkspaceFactoryFast.h:43
 HistoToWorkspaceFactoryFast.h:44
 HistoToWorkspaceFactoryFast.h:45
 HistoToWorkspaceFactoryFast.h:46
 HistoToWorkspaceFactoryFast.h:47
 HistoToWorkspaceFactoryFast.h:48
 HistoToWorkspaceFactoryFast.h:49
 HistoToWorkspaceFactoryFast.h:50
 HistoToWorkspaceFactoryFast.h:51
 HistoToWorkspaceFactoryFast.h:52
 HistoToWorkspaceFactoryFast.h:53
 HistoToWorkspaceFactoryFast.h:54
 HistoToWorkspaceFactoryFast.h:55
 HistoToWorkspaceFactoryFast.h:56
 HistoToWorkspaceFactoryFast.h:57
 HistoToWorkspaceFactoryFast.h:58
 HistoToWorkspaceFactoryFast.h:59
 HistoToWorkspaceFactoryFast.h:60
 HistoToWorkspaceFactoryFast.h:61
 HistoToWorkspaceFactoryFast.h:62
 HistoToWorkspaceFactoryFast.h:63
 HistoToWorkspaceFactoryFast.h:64
 HistoToWorkspaceFactoryFast.h:65
 HistoToWorkspaceFactoryFast.h:66
 HistoToWorkspaceFactoryFast.h:67
 HistoToWorkspaceFactoryFast.h:68
 HistoToWorkspaceFactoryFast.h:69
 HistoToWorkspaceFactoryFast.h:70
 HistoToWorkspaceFactoryFast.h:71
 HistoToWorkspaceFactoryFast.h:72
 HistoToWorkspaceFactoryFast.h:73
 HistoToWorkspaceFactoryFast.h:74
 HistoToWorkspaceFactoryFast.h:75
 HistoToWorkspaceFactoryFast.h:76
 HistoToWorkspaceFactoryFast.h:77
 HistoToWorkspaceFactoryFast.h:78
 HistoToWorkspaceFactoryFast.h:79
 HistoToWorkspaceFactoryFast.h:80
 HistoToWorkspaceFactoryFast.h:81
 HistoToWorkspaceFactoryFast.h:82
 HistoToWorkspaceFactoryFast.h:83
 HistoToWorkspaceFactoryFast.h:84
 HistoToWorkspaceFactoryFast.h:85
 HistoToWorkspaceFactoryFast.h:86
 HistoToWorkspaceFactoryFast.h:87
 HistoToWorkspaceFactoryFast.h:88
 HistoToWorkspaceFactoryFast.h:89
 HistoToWorkspaceFactoryFast.h:90
 HistoToWorkspaceFactoryFast.h:91
 HistoToWorkspaceFactoryFast.h:92
 HistoToWorkspaceFactoryFast.h:93
 HistoToWorkspaceFactoryFast.h:94
 HistoToWorkspaceFactoryFast.h:95
 HistoToWorkspaceFactoryFast.h:96
 HistoToWorkspaceFactoryFast.h:97
 HistoToWorkspaceFactoryFast.h:98
 HistoToWorkspaceFactoryFast.h:99
 HistoToWorkspaceFactoryFast.h:100
 HistoToWorkspaceFactoryFast.h:101
 HistoToWorkspaceFactoryFast.h:102
 HistoToWorkspaceFactoryFast.h:103
 HistoToWorkspaceFactoryFast.h:104
 HistoToWorkspaceFactoryFast.h:105
 HistoToWorkspaceFactoryFast.h:106
 HistoToWorkspaceFactoryFast.h:107
 HistoToWorkspaceFactoryFast.h:108
 HistoToWorkspaceFactoryFast.h:109
 HistoToWorkspaceFactoryFast.h:110
 HistoToWorkspaceFactoryFast.h:111
 HistoToWorkspaceFactoryFast.h:112
 HistoToWorkspaceFactoryFast.h:113
 HistoToWorkspaceFactoryFast.h:114
 HistoToWorkspaceFactoryFast.h:115
 HistoToWorkspaceFactoryFast.h:116
 HistoToWorkspaceFactoryFast.h:117
 HistoToWorkspaceFactoryFast.h:118
 HistoToWorkspaceFactoryFast.h:119
 HistoToWorkspaceFactoryFast.h:120
 HistoToWorkspaceFactoryFast.h:121
 HistoToWorkspaceFactoryFast.h:122
 HistoToWorkspaceFactoryFast.h:123
 HistoToWorkspaceFactoryFast.h:124
 HistoToWorkspaceFactoryFast.h:125
 HistoToWorkspaceFactoryFast.h:126
 HistoToWorkspaceFactoryFast.h:127
 HistoToWorkspaceFactoryFast.h:128
 HistoToWorkspaceFactoryFast.h:129
 HistoToWorkspaceFactoryFast.h:130
 HistoToWorkspaceFactoryFast.h:131
 HistoToWorkspaceFactoryFast.h:132
 HistoToWorkspaceFactoryFast.h:133
 HistoToWorkspaceFactoryFast.h:134
 HistoToWorkspaceFactoryFast.h:135
 HistoToWorkspaceFactoryFast.h:136
 HistoToWorkspaceFactoryFast.h:137
 HistoToWorkspaceFactoryFast.h:138
 HistoToWorkspaceFactoryFast.h:139
 HistoToWorkspaceFactoryFast.h:140
 HistoToWorkspaceFactoryFast.h:141
 HistoToWorkspaceFactoryFast.h:142
 HistoToWorkspaceFactoryFast.h:143
 HistoToWorkspaceFactoryFast.h:144
 HistoToWorkspaceFactoryFast.h:145
 HistoToWorkspaceFactoryFast.h:146