/*****************************************************************************
 * 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
// Class RooBinningCategory provides a real-to-category mapping defined
// by a series of thresholds.
// END_HTML
//


#include "RooFit.h"

#include "Riostream.h"
#include "Riostream.h"
#include <stdlib.h>
#include <stdio.h>
#include "TString.h"
#include "RooBinningCategory.h"
#include "RooStreamParser.h"
#include "RooThreshEntry.h"
#include "RooMsgService.h"

using namespace std;

ClassImp(RooBinningCategory)



//_____________________________________________________________________________
RooBinningCategory::RooBinningCategory(const char *name, const char *title, RooAbsRealLValue& inputVar, 
					   const char* binningName, const char* catTypeName) :
  RooAbsCategory(name, title), _inputVar("inputVar","Input category",this,inputVar), _bname(binningName)
{
  // Constructor with input function to be mapped and name and index of default
  // output state of unmapped values

  initialize(catTypeName) ;

}



//_____________________________________________________________________________
RooBinningCategory::RooBinningCategory(const RooBinningCategory& other, const char *name) :
  RooAbsCategory(other,name), _inputVar("inputVar",this,other._inputVar), _bname(other._bname)
{
  // Copy constructor
}



//_____________________________________________________________________________
RooBinningCategory::~RooBinningCategory() 
{
  // Destructor
}




//_____________________________________________________________________________
void RooBinningCategory::initialize(const char* catTypeName)
{
  // Iterator over all bins in input variable and define corresponding state labels

  Int_t nbins = ((RooAbsRealLValue&)_inputVar.arg()).getBinning(_bname.Length()>0?_bname.Data():0).numBins() ;
  for (Int_t i=0 ; i<nbins ; i++) {
    string name = catTypeName!=0 ? Form("%s%d",catTypeName,i)
            : (_bname.Length()>0 ? Form("%s_%s_bin%d",_inputVar.arg().GetName(),_bname.Data(),i) 
            : Form("%s_bin%d",_inputVar.arg().GetName(),i)) ;
    defineType(name.c_str(),i) ;
  }
}




//_____________________________________________________________________________
RooCatType RooBinningCategory::evaluate() const
{
  // Calculate and return the value of the mapping function
  Int_t ibin = ((RooAbsRealLValue&)_inputVar.arg()).getBin(_bname.Length()>0?_bname.Data():0) ;
  const RooCatType* cat = lookupType(ibin) ;
  if (!cat) {

    string name = (_bname.Length()>0) ? Form("%s_%s_bin%d",_inputVar.arg().GetName(),_bname.Data(),ibin) 
	                              : Form("%s_bin%d",_inputVar.arg().GetName(),ibin) ;
    cat = const_cast<RooBinningCategory*>(this)->defineType(name.c_str(),ibin) ;     
  }

  return *cat ;
}




//_____________________________________________________________________________
void RooBinningCategory::printMultiline(ostream& os, Int_t content, Bool_t verbose, TString indent) const
{
  // Print info about this threshold category to the specified stream. In addition to the info
  // from RooAbsCategory::printStream() we add:
  //
  //  Standard : input category
  //     Shape : default value
  //   Verbose : list of thresholds

   RooAbsCategory::printMultiline(os,content,verbose,indent);

   if (verbose) {
     os << indent << "--- RooBinningCategory ---" << endl
	<< indent << "  Maps from " ;
     _inputVar.arg().printStream(os,kName|kValue,kSingleLine);
   }
}


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