/*****************************************************************************
 * Project: RooFit                                                           *
 * Package: RooFitCore                                                       *
 * @(#)root/roofitcore:$Id$
 * Authors:                                                                  *
 *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
 *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
 *                                                                           *
 * Copyright (c) 2000-2005, Regents of the University of California          *
 *                          and Stanford University. All rights reserved.    *
 *                                                                           *
 * Redistribution and use in source and binary forms,                        *
 * with or without modification, are permitted according to the terms        *
 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
 *****************************************************************************/

//////////////////////////////////////////////////////////////////////////////
// 
// BEGIN_HTML
// RooErrorVar is an auxilary class that represents the error
// of a RooRealVar as a seperate object. The main reason of
// existence of this class is to facilitate the reuse of existing
// techniques to perform calculations that involve a RooRealVars
// error, such as calculating the pull value.
// END_HTML
//
//

#include "RooFit.h"
#include "Riostream.h"

#include "RooErrorVar.h"
#include "RooErrorVar.h"
#include "RooAbsBinning.h"
#include "RooStreamParser.h"
#include "RooRangeBinning.h"
#include "RooMsgService.h"



using namespace std;

ClassImp(RooErrorVar)
;



//_____________________________________________________________________________
RooErrorVar::RooErrorVar(const char *name, const char *title, const RooRealVar& input) :
  RooAbsRealLValue(name,title),
  _realVar("realVar","RooRealVar with error",this,(RooAbsReal&)input)
{
  // Construct an lvalue variable representing the error of RooRealVar input

  _binning = new RooUniformBinning(-1,1,100) ;
}



//_____________________________________________________________________________
RooErrorVar::RooErrorVar(const RooErrorVar& other, const char* name) :
  RooAbsRealLValue(other,name),
  _realVar("realVar",this,other._realVar)
{
  _binning = other._binning->clone() ;

  // Copy constructor

  TIterator* iter = other._altBinning.MakeIterator() ;
  RooAbsBinning* binning ;
  while((binning=(RooAbsBinning*)iter->Next())) {
    _altBinning.Add(binning->clone()) ;
  }
  delete iter ;
}



//_____________________________________________________________________________
RooErrorVar::~RooErrorVar()
{
  // Destructor 

  delete _binning ;
}



//_____________________________________________________________________________
Double_t RooErrorVar::getValV(const RooArgSet*) const 
{ 
  // Return value, i.e. error on input variable

  return evaluate();
}



//_____________________________________________________________________________
Bool_t RooErrorVar::hasBinning(const char* name) const
{
  // Return true if we have binning with given name
  
  return _altBinning.FindObject(name) ? kTRUE : kFALSE ;
}



//_____________________________________________________________________________
const RooAbsBinning& RooErrorVar::getBinning(const char* name, Bool_t verbose, Bool_t createOnTheFly) const 
{
  // Return binning with given name. If no binning exists with such a name, clone the default
  // binning on the fly if so requested

  return const_cast<RooErrorVar*>(this)->getBinning(name,verbose,createOnTheFly) ;
}



//_____________________________________________________________________________
RooAbsBinning& RooErrorVar::getBinning(const char* name, Bool_t /*verbose*/, Bool_t createOnTheFly) 
{
  // Return binning with given name. If no binning exists with such a name, clone the default
  // binning on the fly if so requested
  
  // Return default (normalization) binning and range if no name is specified
  if (name==0) {
    return *_binning ;
  }
  
  // Check if binning with this name has been created already
  RooAbsBinning* binning = (RooAbsBinning*) _altBinning.FindObject(name) ;
  if (binning) {
    return *binning ;
  }

  // Return default binning if binning is not found and no creation is requested
  if (!createOnTheFly) {
    return *_binning ;
  }

  // Create a new RooRangeBinning with this name with default range
  binning = new RooRangeBinning(getMin(),getMax(),name) ;
  coutI(Contents) << "RooErrorVar::getBinning(" << GetName() << ") new range named '" 
		  << name << "' created with default bounds" << endl ;

  _altBinning.Add(binning) ;

  return *binning ;
}

//_____________________________________________________________________________
std::list<std::string> RooErrorVar::getBinningNames() const
{
  // Get a list of all binning names. An empty name implies the default binning.
  // A 0 pointer should be passed to getBinning in this case.
  std::list<std::string> binningNames(1, "");

  RooFIter iter = _altBinning.fwdIterator();
  const RooAbsArg* binning = 0;
  while((binning = iter.next())) {
    const char* name = binning->GetName();
    binningNames.push_back(name);
  }
  return binningNames;
}

//_____________________________________________________________________________
void RooErrorVar::setBinning(const RooAbsBinning& binning, const char* name) 
{
  // Store given binning with this variable under the given name

  if (!name) {
    if (_binning) delete _binning ;
    _binning = binning.clone() ;
  } else {

    // Remove any old binning with this name
    RooAbsBinning* oldBinning = (RooAbsBinning*) _altBinning.FindObject(name) ;
    if (oldBinning) {
      _altBinning.Remove(oldBinning) ;
      delete oldBinning ;
    }

    // Insert new binning in list of alternative binnings
    RooAbsBinning* newBinning = binning.clone() ;
    newBinning->SetName(name) ;
    newBinning->SetTitle(name) ;
    _altBinning.Add(newBinning) ;

  }
  

}



//_____________________________________________________________________________
void RooErrorVar::setMin(const char* name, Double_t value) 
{
  // Set the lower bound of the range with the given name to the given value
  // If name is a null pointer, set the lower bound of the default range

  // Set new minimum of fit range 
  RooAbsBinning& binning = getBinning(name) ;

  // Check if new limit is consistent
  if (value >= getMax()) {
    coutW(InputArguments) << "RooErrorVar::setMin(" << GetName() 
			  << "): Proposed new fit min. larger than max., setting min. to max." << endl ;
    binning.setMin(getMax()) ;
  } else {
    binning.setMin(value) ;
  }

  // Clip current value in window if it fell out
  if (!name) {
    Double_t clipValue ;
    if (!inRange(_value,0,&clipValue)) {
      setVal(clipValue) ;
    }
  }
    
  setShapeDirty() ;
}


//_____________________________________________________________________________
void RooErrorVar::setMax(const char* name, Double_t value)
{
  // Set the upper bound of the range with the given name to the given value
  // If name is a null pointer, set the upper bound of the default range

  // Set new maximum of fit range 
  RooAbsBinning& binning = getBinning(name) ;

  // Check if new limit is consistent
  if (value < getMin()) {
    coutW(InputArguments) << "RooErrorVar::setMax(" << GetName() 
			  << "): Proposed new fit max. smaller than min., setting max. to min." << endl ;
    binning.setMax(getMin()) ;
  } else {
    binning.setMax(value) ;
  }

  // Clip current value in window if it fell out
  if (!name) {
    Double_t clipValue ;
    if (!inRange(_value,0,&clipValue)) {
      setVal(clipValue) ;
    }
  }

  setShapeDirty() ;
}



//_____________________________________________________________________________
void RooErrorVar::setRange( const char* name, Double_t min, Double_t max) 
{
  // Set the upper and lower lower bound of the range with the given name to the given values
  // If name is a null pointer, set the upper and lower bounds of the default range

  Bool_t exists = name ? (_altBinning.FindObject(name)?kTRUE:kFALSE) : kTRUE ;

  // Set new fit range 
  RooAbsBinning& binning = getBinning(name,kFALSE) ;

  // Check if new limit is consistent
  if (min>max) {
    coutW(InputArguments) << "RooErrorVar::setRange(" << GetName() 
			  << "): Proposed new fit max. smaller than min., setting max. to min." << endl ;
    binning.setRange(min,min) ;
  } else {
    binning.setRange(min,max) ;
  }

  if (!exists) {
    coutI(InputArguments) << "RooErrorVar::setRange(" << GetName() 
			  << ") new range named '" << name << "' created with bounds [" 
			  << min << "," << max << "]" << endl ;
  }

  setShapeDirty() ;  
}



//_____________________________________________________________________________
Bool_t RooErrorVar::readFromStream(istream& is, Bool_t /*compact*/, Bool_t verbose) 
{
  // Read object contents from given stream

  TString token,errorPrefix("RooErrorVar::readFromStream(") ;
  errorPrefix.Append(GetName()) ;
  errorPrefix.Append(")") ;
  RooStreamParser parser(is,errorPrefix) ;
  Double_t value(0) ;

    // Compact mode: Read single token
  if (parser.readDouble(value,verbose)) return kTRUE ;
  if (isValidReal(value,verbose)) {
    setVal(value) ;
    return kFALSE ;
  } else {
    return kTRUE ;
  }
}



//_____________________________________________________________________________
void RooErrorVar::writeToStream(ostream& os, Bool_t /*compact*/) const
{
  // Write value to stream

  os << getVal() ;
}


//_____________________________________________________________________________
void RooErrorVar::syncCache(const RooArgSet*) 
{ 
  // Force the internal value cache to be up to date

  _value = evaluate() ; 
}



 RooErrorVar.cxx:1
 RooErrorVar.cxx:2
 RooErrorVar.cxx:3
 RooErrorVar.cxx:4
 RooErrorVar.cxx:5
 RooErrorVar.cxx:6
 RooErrorVar.cxx:7
 RooErrorVar.cxx:8
 RooErrorVar.cxx:9
 RooErrorVar.cxx:10
 RooErrorVar.cxx:11
 RooErrorVar.cxx:12
 RooErrorVar.cxx:13
 RooErrorVar.cxx:14
 RooErrorVar.cxx:15
 RooErrorVar.cxx:16
 RooErrorVar.cxx:17
 RooErrorVar.cxx:18
 RooErrorVar.cxx:19
 RooErrorVar.cxx:20
 RooErrorVar.cxx:21
 RooErrorVar.cxx:22
 RooErrorVar.cxx:23
 RooErrorVar.cxx:24
 RooErrorVar.cxx:25
 RooErrorVar.cxx:26
 RooErrorVar.cxx:27
 RooErrorVar.cxx:28
 RooErrorVar.cxx:29
 RooErrorVar.cxx:30
 RooErrorVar.cxx:31
 RooErrorVar.cxx:32
 RooErrorVar.cxx:33
 RooErrorVar.cxx:34
 RooErrorVar.cxx:35
 RooErrorVar.cxx:36
 RooErrorVar.cxx:37
 RooErrorVar.cxx:38
 RooErrorVar.cxx:39
 RooErrorVar.cxx:40
 RooErrorVar.cxx:41
 RooErrorVar.cxx:42
 RooErrorVar.cxx:43
 RooErrorVar.cxx:44
 RooErrorVar.cxx:45
 RooErrorVar.cxx:46
 RooErrorVar.cxx:47
 RooErrorVar.cxx:48
 RooErrorVar.cxx:49
 RooErrorVar.cxx:50
 RooErrorVar.cxx:51
 RooErrorVar.cxx:52
 RooErrorVar.cxx:53
 RooErrorVar.cxx:54
 RooErrorVar.cxx:55
 RooErrorVar.cxx:56
 RooErrorVar.cxx:57
 RooErrorVar.cxx:58
 RooErrorVar.cxx:59
 RooErrorVar.cxx:60
 RooErrorVar.cxx:61
 RooErrorVar.cxx:62
 RooErrorVar.cxx:63
 RooErrorVar.cxx:64
 RooErrorVar.cxx:65
 RooErrorVar.cxx:66
 RooErrorVar.cxx:67
 RooErrorVar.cxx:68
 RooErrorVar.cxx:69
 RooErrorVar.cxx:70
 RooErrorVar.cxx:71
 RooErrorVar.cxx:72
 RooErrorVar.cxx:73
 RooErrorVar.cxx:74
 RooErrorVar.cxx:75
 RooErrorVar.cxx:76
 RooErrorVar.cxx:77
 RooErrorVar.cxx:78
 RooErrorVar.cxx:79
 RooErrorVar.cxx:80
 RooErrorVar.cxx:81
 RooErrorVar.cxx:82
 RooErrorVar.cxx:83
 RooErrorVar.cxx:84
 RooErrorVar.cxx:85
 RooErrorVar.cxx:86
 RooErrorVar.cxx:87
 RooErrorVar.cxx:88
 RooErrorVar.cxx:89
 RooErrorVar.cxx:90
 RooErrorVar.cxx:91
 RooErrorVar.cxx:92
 RooErrorVar.cxx:93
 RooErrorVar.cxx:94
 RooErrorVar.cxx:95
 RooErrorVar.cxx:96
 RooErrorVar.cxx:97
 RooErrorVar.cxx:98
 RooErrorVar.cxx:99
 RooErrorVar.cxx:100
 RooErrorVar.cxx:101
 RooErrorVar.cxx:102
 RooErrorVar.cxx:103
 RooErrorVar.cxx:104
 RooErrorVar.cxx:105
 RooErrorVar.cxx:106
 RooErrorVar.cxx:107
 RooErrorVar.cxx:108
 RooErrorVar.cxx:109
 RooErrorVar.cxx:110
 RooErrorVar.cxx:111
 RooErrorVar.cxx:112
 RooErrorVar.cxx:113
 RooErrorVar.cxx:114
 RooErrorVar.cxx:115
 RooErrorVar.cxx:116
 RooErrorVar.cxx:117
 RooErrorVar.cxx:118
 RooErrorVar.cxx:119
 RooErrorVar.cxx:120
 RooErrorVar.cxx:121
 RooErrorVar.cxx:122
 RooErrorVar.cxx:123
 RooErrorVar.cxx:124
 RooErrorVar.cxx:125
 RooErrorVar.cxx:126
 RooErrorVar.cxx:127
 RooErrorVar.cxx:128
 RooErrorVar.cxx:129
 RooErrorVar.cxx:130
 RooErrorVar.cxx:131
 RooErrorVar.cxx:132
 RooErrorVar.cxx:133
 RooErrorVar.cxx:134
 RooErrorVar.cxx:135
 RooErrorVar.cxx:136
 RooErrorVar.cxx:137
 RooErrorVar.cxx:138
 RooErrorVar.cxx:139
 RooErrorVar.cxx:140
 RooErrorVar.cxx:141
 RooErrorVar.cxx:142
 RooErrorVar.cxx:143
 RooErrorVar.cxx:144
 RooErrorVar.cxx:145
 RooErrorVar.cxx:146
 RooErrorVar.cxx:147
 RooErrorVar.cxx:148
 RooErrorVar.cxx:149
 RooErrorVar.cxx:150
 RooErrorVar.cxx:151
 RooErrorVar.cxx:152
 RooErrorVar.cxx:153
 RooErrorVar.cxx:154
 RooErrorVar.cxx:155
 RooErrorVar.cxx:156
 RooErrorVar.cxx:157
 RooErrorVar.cxx:158
 RooErrorVar.cxx:159
 RooErrorVar.cxx:160
 RooErrorVar.cxx:161
 RooErrorVar.cxx:162
 RooErrorVar.cxx:163
 RooErrorVar.cxx:164
 RooErrorVar.cxx:165
 RooErrorVar.cxx:166
 RooErrorVar.cxx:167
 RooErrorVar.cxx:168
 RooErrorVar.cxx:169
 RooErrorVar.cxx:170
 RooErrorVar.cxx:171
 RooErrorVar.cxx:172
 RooErrorVar.cxx:173
 RooErrorVar.cxx:174
 RooErrorVar.cxx:175
 RooErrorVar.cxx:176
 RooErrorVar.cxx:177
 RooErrorVar.cxx:178
 RooErrorVar.cxx:179
 RooErrorVar.cxx:180
 RooErrorVar.cxx:181
 RooErrorVar.cxx:182
 RooErrorVar.cxx:183
 RooErrorVar.cxx:184
 RooErrorVar.cxx:185
 RooErrorVar.cxx:186
 RooErrorVar.cxx:187
 RooErrorVar.cxx:188
 RooErrorVar.cxx:189
 RooErrorVar.cxx:190
 RooErrorVar.cxx:191
 RooErrorVar.cxx:192
 RooErrorVar.cxx:193
 RooErrorVar.cxx:194
 RooErrorVar.cxx:195
 RooErrorVar.cxx:196
 RooErrorVar.cxx:197
 RooErrorVar.cxx:198
 RooErrorVar.cxx:199
 RooErrorVar.cxx:200
 RooErrorVar.cxx:201
 RooErrorVar.cxx:202
 RooErrorVar.cxx:203
 RooErrorVar.cxx:204
 RooErrorVar.cxx:205
 RooErrorVar.cxx:206
 RooErrorVar.cxx:207
 RooErrorVar.cxx:208
 RooErrorVar.cxx:209
 RooErrorVar.cxx:210
 RooErrorVar.cxx:211
 RooErrorVar.cxx:212
 RooErrorVar.cxx:213
 RooErrorVar.cxx:214
 RooErrorVar.cxx:215
 RooErrorVar.cxx:216
 RooErrorVar.cxx:217
 RooErrorVar.cxx:218
 RooErrorVar.cxx:219
 RooErrorVar.cxx:220
 RooErrorVar.cxx:221
 RooErrorVar.cxx:222
 RooErrorVar.cxx:223
 RooErrorVar.cxx:224
 RooErrorVar.cxx:225
 RooErrorVar.cxx:226
 RooErrorVar.cxx:227
 RooErrorVar.cxx:228
 RooErrorVar.cxx:229
 RooErrorVar.cxx:230
 RooErrorVar.cxx:231
 RooErrorVar.cxx:232
 RooErrorVar.cxx:233
 RooErrorVar.cxx:234
 RooErrorVar.cxx:235
 RooErrorVar.cxx:236
 RooErrorVar.cxx:237
 RooErrorVar.cxx:238
 RooErrorVar.cxx:239
 RooErrorVar.cxx:240
 RooErrorVar.cxx:241
 RooErrorVar.cxx:242
 RooErrorVar.cxx:243
 RooErrorVar.cxx:244
 RooErrorVar.cxx:245
 RooErrorVar.cxx:246
 RooErrorVar.cxx:247
 RooErrorVar.cxx:248
 RooErrorVar.cxx:249
 RooErrorVar.cxx:250
 RooErrorVar.cxx:251
 RooErrorVar.cxx:252
 RooErrorVar.cxx:253
 RooErrorVar.cxx:254
 RooErrorVar.cxx:255
 RooErrorVar.cxx:256
 RooErrorVar.cxx:257
 RooErrorVar.cxx:258
 RooErrorVar.cxx:259
 RooErrorVar.cxx:260
 RooErrorVar.cxx:261
 RooErrorVar.cxx:262
 RooErrorVar.cxx:263
 RooErrorVar.cxx:264
 RooErrorVar.cxx:265
 RooErrorVar.cxx:266
 RooErrorVar.cxx:267
 RooErrorVar.cxx:268
 RooErrorVar.cxx:269
 RooErrorVar.cxx:270
 RooErrorVar.cxx:271
 RooErrorVar.cxx:272
 RooErrorVar.cxx:273
 RooErrorVar.cxx:274
 RooErrorVar.cxx:275
 RooErrorVar.cxx:276
 RooErrorVar.cxx:277
 RooErrorVar.cxx:278
 RooErrorVar.cxx:279
 RooErrorVar.cxx:280
 RooErrorVar.cxx:281
 RooErrorVar.cxx:282
 RooErrorVar.cxx:283
 RooErrorVar.cxx:284
 RooErrorVar.cxx:285
 RooErrorVar.cxx:286
 RooErrorVar.cxx:287
 RooErrorVar.cxx:288
 RooErrorVar.cxx:289
 RooErrorVar.cxx:290
 RooErrorVar.cxx:291
 RooErrorVar.cxx:292
 RooErrorVar.cxx:293
 RooErrorVar.cxx:294
 RooErrorVar.cxx:295
 RooErrorVar.cxx:296
 RooErrorVar.cxx:297
 RooErrorVar.cxx:298
 RooErrorVar.cxx:299
 RooErrorVar.cxx:300
 RooErrorVar.cxx:301
 RooErrorVar.cxx:302
 RooErrorVar.cxx:303
 RooErrorVar.cxx:304
 RooErrorVar.cxx:305
 RooErrorVar.cxx:306
 RooErrorVar.cxx:307
 RooErrorVar.cxx:308
 RooErrorVar.cxx:309
 RooErrorVar.cxx:310
 RooErrorVar.cxx:311
 RooErrorVar.cxx:312
 RooErrorVar.cxx:313
 RooErrorVar.cxx:314
 RooErrorVar.cxx:315
 RooErrorVar.cxx:316
 RooErrorVar.cxx:317
 RooErrorVar.cxx:318
 RooErrorVar.cxx:319
 RooErrorVar.cxx:320
 RooErrorVar.cxx:321
 RooErrorVar.cxx:322
 RooErrorVar.cxx:323
 RooErrorVar.cxx:324
 RooErrorVar.cxx:325
 RooErrorVar.cxx:326
 RooErrorVar.cxx:327
 RooErrorVar.cxx:328
 RooErrorVar.cxx:329
 RooErrorVar.cxx:330
 RooErrorVar.cxx:331