#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"
#include "RooMsgService.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)) {
coutE(InputArguments) << "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) {
coutE(InputArguments) << "RooMappedCategory::map(" << GetName()
<< "): ERROR, unable to output type " << outKey << endl ;
return kTRUE ;
}
RooMapCatEntry *newMap = new RooMapCatEntry(inKeyRegExp,outType) ;
if (!newMap->ok()) {
coutE(InputArguments) << "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.label() ;
for (int i=0 ; i<_mapArray.GetEntries() ; i++) {
RooMapCatEntry* theMap = (RooMapCatEntry*)_mapArray.At(i) ;
if (theMap->match(inKey)) {
return theMap->outCat() ;
}
}
return *_defCat ;
}
void RooMappedCategory::printMultiline(ostream& os, Int_t content, Bool_t verbose, TString indent) const
{
RooAbsCategory::printMultiline(os,content,verbose,indent);
if (verbose) {
os << indent << "--- RooMappedCategory ---" << endl
<< indent << " Maps from " ;
_inputCat.arg().printStream(os,0,kStandard);
os << indent << " Default value is ";
_defCat->printStream(os,kName|kValue,kSingleLine);
os << indent << " Mapping rules:" << endl;
Int_t n= _mapArray.GetEntries();
for(Int_t i= 0 ; i< n; i++) {
RooMapCatEntry* theMap = (RooMapCatEntry*)_mapArray.At(i) ;
os << indent << " " << theMap->GetName() << " -> " << theMap->outCat().GetName() << endl ;
}
}
}
Bool_t RooMappedCategory::readFromStream(istream& is, Bool_t compact, Bool_t )
{
if (compact) {
coutE(InputArguments) << "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* theMap = (RooMapCatEntry*)_mapArray.At(i) ;
if (theMap->outCat().getVal()!=prevOutCat.getVal()) {
if (!first) { os << " " ; }
first=kFALSE ;
os << theMap->outCat().GetName() << ":" << theMap->GetName() ;
prevOutCat=theMap->outCat() ;
} else {
os << "," << theMap->GetName() ;
}
}
if (!first) { os << " " ; }
os << _defCat->GetName() << ":*" ;
}
}
Last change: Wed Jun 25 08:33:26 2008
Last generated: 2008-06-25 08:33
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.