// @(#)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_HISTOTOWORKSPACEFACTORY
#define ROOSTATS_HISTOTOWORKSPACEFACTORY

#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>


namespace RooStats{
namespace HistFactory{

  struct EstimateSummary;
   
  class HistoToWorkspaceFactory: public TObject {

    public:

     HistoToWorkspaceFactory(  std::string, std::string , std::vector<std::string> , double =200, double =20, int =0, int =6, TFile * =0);
      HistoToWorkspaceFactory();
      virtual ~HistoToWorkspaceFactory();

      void AddEfficiencyTerms(RooWorkspace* proto, std::string prefix, std::string interpName,
            std::map<std::string,std::pair<double,double> > systMap,
            std::vector<std::string>& likelihoodTermNames, std::vector<std::string>& totSystTermNames);

      std::string AddNormFactor(RooWorkspace *, std::string & , std::string & , EstimateSummary & , bool );

      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);

      //void Combine_old();

      RooWorkspace *  MakeCombinedModel(std::vector<std::string>, std::vector<RooWorkspace*>);

      //void Combine_ratio(std::vector<std::string> , std::vector<RooWorkspace*>);

      void Customize(RooWorkspace* proto, const char* pdfNameChar, std::map<std::string,std::string> renameMap);

      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);

      void FormatFrameForLikelihood(RooPlot* frame, std::string XTitle=std::string("#sigma / #sigma_{SM}"), std::string YTitle=std::string("-log likelihood"));


      void LinInterpWithConstraint(RooWorkspace* proto, TH1* nominal, std::vector<TH1*> lowHist, std::vector<TH1*> highHist,
                 std::vector<std::string> sourceName, std::string prefix, std::string productPrefix, std::string systTerm,
                 int lowBin, int highBin, std::vector<std::string>& likelihoodTermNames);

      TDirectory* Makedirs( TDirectory* file, std::vector<std::string> names );

      RooWorkspace* MakeSingleChannelModel(std::vector<RooStats::HistFactory::EstimateSummary> summary, std::vector<std::string> systToFix, bool doRatio=false);

      void  MakeTotalExpected(RooWorkspace* proto, std::string totName, std::string /**/, std::string /**/,
            int lowBin, int highBin, std::vector<std::string>& syst_x_expectedPrefixNames,
            std::vector<std::string>& normByNames);

      TDirectory* Mkdir( TDirectory * file, std::string name );

      void PrintCovarianceMatrix(RooFitResult* result, RooArgSet* params, std::string filename);
      void ProcessExpectedHisto(TH1* hist,RooWorkspace* proto, std::string prefix, std::string productPrefix, std::string systTerm, double low, double high, int lowBin, int highBin);
      void SetObsToExpected(RooWorkspace* proto, std::string obsPrefix, std::string expPrefix, int lowBin, int highBin);
      void FitModel(RooWorkspace *, std::string, std::string, std::string, bool=false  );
      std::string FilePrefixStr(std::string);


      std::string fFileNamePrefix;
      std::string fRowTitle;
      std::vector<std::string> fSystToFix;
      double fNomLumi, fLumiError;
      int  fLowBin, fHighBin;    
      std::stringstream fResultsPrefixStr;
      TFile * fOut_f;
      FILE * pFile;

      ClassDef(RooStats::HistFactory::HistoToWorkspaceFactory,1)
  };

}
}

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