// @(#)root/roostats:$Id$
// Author: Kyle Cranmer, George Lewis 
/*************************************************************************
 * 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.             *
 *************************************************************************/

//_________________________________________________
/*
BEGIN_HTML
<p>
</p>
END_HTML
*/
//

#include "RooRealVar.h"
#include "RooStats/HistFactory/HistFactoryException.h"

#include "RooStats/HistFactory/Asimov.h"

void RooStats::HistFactory::Asimov::ConfigureWorkspace(RooWorkspace* wspace) {

  // Here is where we set the values, and constantness
  // of all parameters in the workspace before creating
  // an asimov dataset

  /*
  // Okay, y'all, first we're going to create a snapshot
  // of the current state of the variables in the workspace
  
  std::string ListOfVariableNames = "";
  for( std::map< std::string, double >::iterator itr = fParamValsToSet.begin(); 
       itr != fParamValsToSet.end(); ++itr) {
    // Extend the Variable Name list
    ListOfVariableNames += "," + itr->first;
  }  
  for( std::map< std::string, bool >::iterator itr = fParamsToFix.begin(); 
       itr != fParamsToFix.end(); ++itr) {
    // Extend the Variable Name list
    ListOfVariableNames += "," + itr->first;
  }  
  
  // Save a snapshot
  std::string SnapShotName = "NominalParamValues";
  wspace->saveSnapshot(SnapShotName.c_str(), ListOfVariableNames.c_str());
  */

  //
  // First we set all parameters to their given values
  //


  for( std::map< std::string, double >::iterator itr = fParamValsToSet.begin(); 
       itr != fParamValsToSet.end(); ++itr) {

    std::string param = itr->first;
    double val  = itr->second;

    // Try to get the variable in the workspace
    RooRealVar* var = wspace->var(param.c_str());
    if( !var ) {
      std::cout << "Error: Trying to set variable: " << var
		<< " to a specific value in creation of asimov dataset: " << fName
		<< " but this variable doesn't appear to exist in the workspace"
		<< std::endl;
      throw hf_exc();
    }

    // Check that the desired value is in the range of the variable
    double inRange = var->inRange(val, NULL);
    if( !inRange ) {
      std::cout << "Error: Attempting to set variable: " << var
		<< " to value: " << val << ", however it appears"
		<< " that this is not withn the variable's range: " 
		<< "[" << var->getMin() << ", " << var->getMax() << "]"
		<< std::endl;
      throw hf_exc();
    }

    // Set its value
    std::cout << "Configuring Asimov Dataset: Setting " << param
	      << " = " << val << std::endl;
    var->setVal( val );
  }


  //
  // Then, we set any variables to constant
  //
  
  for( std::map< std::string, bool >::iterator itr = fParamsToFix.begin(); 
       itr != fParamsToFix.end(); ++itr) {

    std::string param = itr->first;
    bool isConstant  = itr->second;

    // Try to get the variable in the workspace
    RooRealVar* var = wspace->var(param.c_str());
    if( !var ) {
      std::cout << "Error: Trying to set variable: " << var
		<< " constant in creation of asimov dataset: " << fName
		<< " but this variable doesn't appear to exist in the workspace"
		<< std::endl;
      throw hf_exc();
    }

    std::cout << "Configuring Asimov Dataset: Setting " << param
	      << " to constant " << std::endl;
    var->setConstant( isConstant );
    
  }
  
  return;

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