#include "RooFit.h"
#include "Riostream.h"
#include "Riostream.h"
#include <stdlib.h>
#include <stdio.h>
#include "TString.h"
#include "RooMappedCategory.h"
#include "RooStreamParser.h"
#include "RooMapCatEntry.h"
ClassImp(RooMappedCategory)
RooMappedCategory::RooMappedCategory(const char *name, const char *title, RooAbsCategory& inputCat, const char* defOut, Int_t defOutIdx) :
  RooAbsCategory(name, title), _inputCat("inputCat","Input category",this,inputCat)
{
  
  
  if (defOutIdx==NoCatIdx) {
    _defCat = (RooCatType*) defineType(defOut) ;
  } else {
    _defCat = (RooCatType*) defineType(defOut,defOutIdx) ;
  }
}
RooMappedCategory::RooMappedCategory(const RooMappedCategory& other, const char *name) :
  RooAbsCategory(other,name), _inputCat("inputCat",this,other._inputCat)
{
  _defCat = (RooCatType*) lookupType(other._defCat->GetName()) ;
  
  int i ;
  for (i=0 ; i<other._mapArray.GetEntries() ; i++) {
    _mapArray.Add(new RooMapCatEntry(*(RooMapCatEntry*)other._mapArray.At(i))) ;
  }
}
RooMappedCategory::~RooMappedCategory() 
{
  
  _mapArray.Delete() ;
}
Bool_t RooMappedCategory::map(const char* inKeyRegExp, const char* outKey, Int_t outIdx)
{
  
  
  
  
  
  if (!inKeyRegExp || !outKey) return kTRUE ;  
  
  if (_mapArray.FindObject(inKeyRegExp)) {
    cout << "RooMappedCategory::map(" << GetName() << "): ERROR expression " 
	 << inKeyRegExp << " already mapped" << endl ;
    return kTRUE ;
  }
  
  const RooCatType* outType = lookupType(outKey) ;
  if (!outType) {
    if (outIdx==NoCatIdx) {
      outType = defineType(outKey) ;
    } else {
      outType = defineType(outKey,outIdx) ;
    }
  }
  if (!outType) {
    cout << "RooMappedCategory::map(" << GetName() 
	 << "): ERROR, unable to output type " << outKey << endl ;
    return kTRUE ;    
  }
  
  RooMapCatEntry *newMap = new RooMapCatEntry(inKeyRegExp,outType) ;
  if (!newMap->ok()) {
    cout << "RooMappedCategory::map(" << GetName() 
	 << "): ERROR, expression " << inKeyRegExp << " didn't compile" << endl ;
    delete newMap ;
    return kTRUE ;    
  }
  _mapArray.Add(newMap) ;  
  return kFALSE ;
}
RooCatType
RooMappedCategory::evaluate() const
{
  
  const char* inKey = _inputCat ;
  
  for (int i=0 ; i<_mapArray.GetEntries() ; i++) {
    RooMapCatEntry* map = (RooMapCatEntry*)_mapArray.At(i) ;
    if (map->match(inKey)) {
      return map->outCat() ;
    }
  }
  
  return *_defCat ;
}
void RooMappedCategory::printToStream(ostream& os, PrintOption opt, TString indent) const
{
  
  
  
  
  
  
   RooAbsCategory::printToStream(os,opt,indent);
   if (opt > Standard) {
     os << indent << "--- RooMappedCategory ---" << endl
	<< indent << "  Maps from " ;
     _inputCat.arg().printToStream(os,Standard);
     
     os << indent << "  Default value is ";
     _defCat->printToStream(os,OneLine);
     os << indent << "  Mapping rules:" << endl;
     Int_t n= _mapArray.GetEntries();
     for(Int_t i= 0 ; i< n; i++) {
       RooMapCatEntry* map = (RooMapCatEntry*)_mapArray.At(i) ;
       os << indent << "  " << map->GetName() << " -> " << map->outCat().GetName() << endl ;
     }
   }
}
Bool_t RooMappedCategory::readFromStream(istream& is, Bool_t compact, Bool_t ) 
{
  
   if (compact) {
     cout << "RooMappedCategory::readFromSteam(" << GetName() << "): can't read in compact mode" << endl ;
     return kTRUE ;    
   } else {
     
     TString defCatName(_defCat->GetName()) ;
     _mapArray.Delete() ;
     clearTypes() ;
     _defCat = (RooCatType*) defineType(defCatName) ;
     TString token,errorPrefix("RooMappedCategory::readFromStream(") ;
     errorPrefix.Append(GetName()) ;
     errorPrefix.Append(")") ;
     RooStreamParser parser(is,errorPrefix) ;
     parser.setPunctuation(":,") ;
  
     TString destKey,srcKey ;
     Bool_t readToken(kTRUE) ;
    
     while(1) {      
       if (readToken) token=parser.readToken() ;
       if (token.IsNull()) break ;
       readToken=kTRUE ;
       destKey = token ;
       if (parser.expectToken(":",kTRUE)) return kTRUE ;
       
       while(1) { 
	 srcKey = parser.readToken() ;	
	 token = parser.readToken() ;
	 
	 if (map(srcKey,destKey)) return kTRUE ;
       
	 
	 
	 if (token.CompareTo(",")) {	  	  
	   readToken = kFALSE ;
	   break ;
	 } 	
       }
     }
     return kFALSE ;
   }
   
}
void RooMappedCategory::writeToStream(ostream& os, Bool_t compact) const
{
  
  if (compact) {
    
    os << getLabel() ;
  } else {
    
    
    RooCatType prevOutCat ;
    Bool_t first(kTRUE) ;
    for (int i=0 ; i<_mapArray.GetEntries() ; i++) {
      RooMapCatEntry* map = (RooMapCatEntry*)_mapArray.At(i) ;
      if (map->outCat().getVal()!=prevOutCat.getVal()) {
	if (!first) { os << " " ; }
	first=kFALSE ;
	os << map->outCat().GetName() << ":" << map->GetName() ;
	prevOutCat=map->outCat() ;
      } else {
	os << "," << map->GetName() ;
      }
    }
    
    if (!first) { os << " " ; }
    os << _defCat->GetName() << ":*" ;  
  }
}
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.