// RooGenCategory provides the most flexibe mapping of a series of input categories
// on a output category via a global function provided in the constructor
// <p>
// The mapping function must have the form 'const char* mapFunc(const RooArgSet* catList)'
// and return the name of the output state for the list of categories supplied in the argument.
// The global function can be a ROOT interpreted function.
// <p>
// RooGenCategory builds a numerical index-to-index map from the user function
// to achieve a high performance mapping.
// END_HTML
#include "RooFit.h"
#include "Riostream.h"
#include "TMethodCall.h"
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include "TString.h"
#include "TInterpreter.h"
#include "RooGenCategory.h"
#include "RooStreamParser.h"
#include "RooMapCatEntry.h"
#include "RooErrorHandler.h"
#include "RooMsgService.h"
#if ROOT_VERSION_CODE < ROOT_VERSION(5,20,00)
#include "Api.h"
#endif
ClassImp(RooGenCategory)
RooGenCategory::RooGenCategory(const char *name, const char *title, void *userFunc, RooArgSet& catList) :
RooAbsCategory(name, title),
_superCat("superCat","Super Category",catList),
_superCatProxy("superCatProxy","Super Category Proxy",this,_superCat),
_map(0)
{
#if ROOT_VERSION_CODE >= ROOT_VERSION(5,20,00)
_userFuncName = gCint->Getp2f2funcname(userFunc);
#else
_userFuncName = G__p2f2funcname(userFunc);
#endif
if(_userFuncName.IsNull()) {
coutE(InputArguments) << GetName() << ": cannot find dictionary info for (void*)"
<< (void*)userFunc << endl;
return;
}
initialize() ;
}
RooGenCategory::RooGenCategory(const RooGenCategory& other, const char *name) :
RooAbsCategory(other,name), _superCat(other._superCat),
_superCatProxy("superCatProxy","Super Category Proxy",this,_superCat),
_map(0), _userFuncName(other._userFuncName)
{
removeServer((RooAbsArg&)other._superCat) ;
initialize() ;
}
void RooGenCategory::initialize()
{
addServer(_superCat,kTRUE,kTRUE) ;
_userFunc= new TMethodCall();
_userFunc->InitWithPrototype(_userFuncName.Data(),"RooArgSet*");
updateIndexList() ;
}
RooGenCategory::~RooGenCategory()
{
if (_serverList.FindObject(&_superCat)) {
removeServer(_superCat) ;
}
if (_map) delete[] _map ;
}
TString RooGenCategory::evalUserFunc(RooArgSet *vars)
{
assert(0 != _userFunc);
Long_t result;
_userArgs[0]= (Long_t)vars ;
_userFunc->SetParamPtrs(_userArgs);
_userFunc->Execute(result);
const char *text= (const char *)result;
return TString(text);
}
void RooGenCategory::updateIndexList()
{
if (_map) delete[] _map ;
_map = new Int_t[_superCatProxy.arg().numTypes()] ;
clearTypes() ;
RooArgSet* tmp=(RooArgSet*) RooArgSet(_superCatProxy.arg()).snapshot(kTRUE) ;
if (!tmp) {
coutE(ObjectHandling) << "RooGenCategory::updateIndexList(" << GetName() << ") Couldn't deep-clone super category, abort," << endl ;
throw std::string("RooGenCategory: Cannot deep clone super category") ;
}
RooSuperCategory* superClone = (RooSuperCategory*) tmp->find(_superCatProxy.arg().GetName()) ;
TIterator* sIter = superClone->typeIterator() ;
RooArgSet *catList = superClone->getParameters((const RooArgSet*)0) ;
RooCatType* type ;
while ((type=(RooCatType*)sIter->Next())) {
superClone->setIndex(type->getVal()) ;
TString typeName = evalUserFunc(catList) ;
const RooCatType* theType = lookupType(typeName,kFALSE) ;
if (!theType) theType = defineType(typeName) ;
_map[superClone->getIndex()] = theType->getVal() ;
}
delete tmp ;
delete catList ;
}
RooCatType
RooGenCategory::evaluate() const
{
if (isShapeDirty()) {
const_cast<RooGenCategory*>(this)->updateIndexList() ;
}
const RooCatType* ret = lookupType(_map[(Int_t)_superCatProxy]) ;
if (!ret) {
cout << "RooGenCategory::evaluate(" << GetName() << ") ERROR: cannot lookup super index " << (Int_t) _superCatProxy << endl ;
assert(0) ;
}
return *ret ;
}
void RooGenCategory::printMultiline(ostream& os, Int_t content, Bool_t verbose, TString indent) const
{
RooAbsCategory::printMultiline(os,content,verbose,indent);
if (verbose) {
os << indent << "--- RooGenCategory ---" << endl;
os << indent << " Input category list:" << endl ;
TString moreIndent(indent) ;
indent.Append(" ") ;
((RooSuperCategory&)_superCatProxy.arg()).inputCatList().printStream(os,kName|kClassName|kArgs,kSingleLine) ;
os << indent << " User mapping function is 'const char* " << _userFuncName << "(RooArgSet*)'" << endl ;
}
}
Bool_t RooGenCategory::readFromStream(istream& , Bool_t compact, Bool_t )
{
if (compact) {
coutE(InputArguments) << "RooGenCategory::readFromSteam(" << GetName() << "): can't read in compact mode" << endl ;
return kTRUE ;
} else {
return kFALSE ;
}
}
void RooGenCategory::writeToStream(ostream& os, Bool_t compact) const
{
if (compact) {
os << getLabel() ;
} else {
}
}