#include "RooFit.h"
#include "Riostream.h"
#include <iomanip>
#include <fstream>
#include <list>
#include "TClass.h"
#include "RooArgSet.h"
#include "RooStreamParser.h"
#include "RooFormula.h"
#include "RooAbsRealLValue.h"
#include "RooAbsCategoryLValue.h"
#include "RooStringVar.h"
#include "RooTrace.h"
#include "RooArgList.h"
#include "RooSentinel.h"
#include "RooMsgService.h"
using namespace std ;
#if (__GNUC__==3&&__GNUC_MINOR__==2&&__GNUC_PATCHLEVEL__==3)
char* operator+( streampos&, char* );
#endif
ClassImp(RooArgSet)
;
char* RooArgSet::_poolBegin = 0 ;
char* RooArgSet::_poolCur = 0 ;
char* RooArgSet::_poolEnd = 0 ;
#define POOLSIZE 1048576
struct POOLDATA
{
void* _base ;
} ;
static std::list<POOLDATA> _memPoolList ;
void RooArgSet::cleanup()
{
std::list<POOLDATA>::iterator iter = _memPoolList.begin() ;
while(iter!=_memPoolList.end()) {
free(iter->_base) ;
iter->_base=0 ;
iter++ ;
}
_memPoolList.clear() ;
}
#ifdef USEMEMPOOL
void* RooArgSet::operator new (size_t bytes)
{
if (!_poolBegin || _poolCur+(sizeof(RooArgSet)) >= _poolEnd) {
if (_poolBegin!=0) {
oocxcoutD((TObject*)0,Caching) << "RooArgSet::operator new(), starting new 1MB memory pool" << endl ;
}
if (_memPoolList.size()>3) {
void* toFree(0) ;
for (std::list<POOLDATA>::iterator poolIter = _memPoolList.begin() ; poolIter!=_memPoolList.end() ; ++poolIter) {
if ((*(Int_t*)(poolIter->_base))==0) {
oocxcoutD((TObject*)0,Caching) << "RooArgSet::operator new(), pruning empty memory pool " << (void*)(poolIter->_base) << endl ;
toFree = poolIter->_base ;
_memPoolList.erase(poolIter) ;
break ;
}
}
free(toFree) ;
}
void* mem = malloc(POOLSIZE) ;
_poolBegin = (char*)mem ;
_poolCur = _poolBegin+sizeof(Int_t) ;
_poolEnd = _poolBegin+(POOLSIZE) ;
*((Int_t*)_poolBegin)=0 ;
POOLDATA p ;
p._base=mem ;
_memPoolList.push_back(p) ;
RooSentinel::activate() ;
}
char* ptr = _poolCur ;
_poolCur += bytes ;
(*((Int_t*)_poolBegin))++ ;
return ptr ;
}
void RooArgSet::operator delete (void* ptr)
{
for (std::list<POOLDATA>::iterator poolIter = _memPoolList.begin() ; poolIter!=_memPoolList.end() ; ++poolIter) {
if ((char*)ptr > (char*)poolIter->_base && (char*)ptr < (char*)poolIter->_base + POOLSIZE) {
(*(Int_t*)(poolIter->_base))-- ;
break ;
}
}
}
#endif
RooArgSet::RooArgSet() :
RooAbsCollection()
{
}
RooArgSet::RooArgSet(const RooArgList& list) :
RooAbsCollection(list.GetName())
{
add(list,kTRUE) ;
}
RooArgSet::RooArgSet(const RooArgList& list, const RooAbsArg* var1) :
RooAbsCollection(list.GetName())
{
if (var1) {
add(*var1,kTRUE) ;
}
add(list,kTRUE) ;
}
RooArgSet::RooArgSet(const char *name) :
RooAbsCollection(name)
{
}
RooArgSet::RooArgSet(const RooArgSet& set1, const RooArgSet& set2, const char *name) : RooAbsCollection(name)
{
add(set1) ;
add(set2) ;
}
RooArgSet::RooArgSet(const RooAbsArg& var1,
const char *name) :
RooAbsCollection(name)
{
add(var1);
}
RooArgSet::RooArgSet(const RooAbsArg& var1, const RooAbsArg& var2,
const char *name) :
RooAbsCollection(name)
{
add(var1); add(var2);
}
RooArgSet::RooArgSet(const RooAbsArg& var1, const RooAbsArg& var2,
const RooAbsArg& var3,
const char *name) :
RooAbsCollection(name)
{
add(var1); add(var2); add(var3);
}
RooArgSet::RooArgSet(const RooAbsArg& var1, const RooAbsArg& var2,
const RooAbsArg& var3, const RooAbsArg& var4,
const char *name) :
RooAbsCollection(name)
{
add(var1); add(var2); add(var3); add(var4);
}
RooArgSet::RooArgSet(const RooAbsArg& var1,
const RooAbsArg& var2, const RooAbsArg& var3,
const RooAbsArg& var4, const RooAbsArg& var5,
const char *name) :
RooAbsCollection(name)
{
add(var1); add(var2); add(var3); add(var4); add(var5);
}
RooArgSet::RooArgSet(const RooAbsArg& var1, const RooAbsArg& var2,
const RooAbsArg& var3, const RooAbsArg& var4,
const RooAbsArg& var5, const RooAbsArg& var6,
const char *name) :
RooAbsCollection(name)
{
add(var1); add(var2); add(var3); add(var4); add(var5); add(var6);
}
RooArgSet::RooArgSet(const RooAbsArg& var1, const RooAbsArg& var2,
const RooAbsArg& var3, const RooAbsArg& var4,
const RooAbsArg& var5, const RooAbsArg& var6,
const RooAbsArg& var7,
const char *name) :
RooAbsCollection(name)
{
add(var1); add(var2); add(var3); add(var4); add(var5); add(var6); add(var7) ;
}
RooArgSet::RooArgSet(const RooAbsArg& var1, const RooAbsArg& var2,
const RooAbsArg& var3, const RooAbsArg& var4,
const RooAbsArg& var5, const RooAbsArg& var6,
const RooAbsArg& var7, const RooAbsArg& var8,
const char *name) :
RooAbsCollection(name)
{
add(var1); add(var2); add(var3); add(var4); add(var5); add(var6); add(var7) ;add(var8) ;
}
RooArgSet::RooArgSet(const RooAbsArg& var1, const RooAbsArg& var2,
const RooAbsArg& var3, const RooAbsArg& var4,
const RooAbsArg& var5, const RooAbsArg& var6,
const RooAbsArg& var7, const RooAbsArg& var8,
const RooAbsArg& var9, const char *name) :
RooAbsCollection(name)
{
add(var1); add(var2); add(var3); add(var4); add(var5); add(var6); add(var7); add(var8); add(var9);
}
RooArgSet::RooArgSet(const TCollection& tcoll, const char* name) :
RooAbsCollection(name)
{
TIterator* iter = tcoll.MakeIterator() ;
TObject* obj ;
while((obj=iter->Next())) {
if (!dynamic_cast<RooAbsArg*>(obj)) {
coutW(InputArguments) << "RooArgSet::RooArgSet(TCollection) element " << obj->GetName()
<< " is not a RooAbsArg, ignored" << endl ;
continue ;
}
add(*(RooAbsArg*)obj) ;
}
delete iter ;
}
RooArgSet::RooArgSet(const RooArgSet& other, const char *name)
: RooAbsCollection(other,name)
{
}
RooArgSet::~RooArgSet()
{
}
Bool_t RooArgSet::add(const RooAbsArg& var, Bool_t silent)
{
return checkForDup(var,silent)? kFALSE : RooAbsCollection::add(var,silent) ;
}
Bool_t RooArgSet::addOwned(RooAbsArg& var, Bool_t silent)
{
return checkForDup(var,silent)? kFALSE : RooAbsCollection::addOwned(var,silent) ;
}
RooAbsArg* RooArgSet::addClone(const RooAbsArg& var, Bool_t silent)
{
return checkForDup(var,silent)? 0 : RooAbsCollection::addClone(var,silent) ;
}
RooAbsArg& RooArgSet::operator[](const char* name) const
{
RooAbsArg* arg = find(name) ;
if (!arg) {
coutE(InputArguments) << "RooArgSet::operator[](" << GetName() << ") ERROR: no element named " << name << " in set" << endl ;
RooErrorHandler::softAbort() ;
}
return *arg ;
}
Bool_t RooArgSet::checkForDup(const RooAbsArg& var, Bool_t silent) const
{
RooAbsArg *other = 0;
if((other= find(var.GetName()))) {
if(other != &var) {
if (!silent)
coutE(InputArguments) << "RooArgSet::checkForDup: ERROR argument with name " << var.GetName() << " is already in this set" << endl;
}
return kTRUE;
}
return kFALSE ;
}
Double_t RooArgSet::getRealValue(const char* name, Double_t defVal, Bool_t verbose) const
{
RooAbsArg* raa = find(name) ;
if (!raa) {
if (verbose) coutE(InputArguments) << "RooArgSet::getRealValue(" << GetName() << ") ERROR no object with name '" << name << "' found" << endl ;
return defVal ;
}
RooAbsReal* rar = dynamic_cast<RooAbsReal*>(raa) ;
if (!rar) {
if (verbose) coutE(InputArguments) << "RooArgSet::getRealValue(" << GetName() << ") ERROR object '" << name << "' is not of type RooAbsReal" << endl ;
return defVal ;
}
return rar->getVal() ;
}
Bool_t RooArgSet::setRealValue(const char* name, Double_t newVal, Bool_t verbose)
{
RooAbsArg* raa = find(name) ;
if (!raa) {
if (verbose) coutE(InputArguments) << "RooArgSet::setRealValue(" << GetName() << ") ERROR no object with name '" << name << "' found" << endl ;
return kTRUE ;
}
RooAbsRealLValue* rar = dynamic_cast<RooAbsRealLValue*>(raa) ;
if (!rar) {
if (verbose) coutE(InputArguments) << "RooArgSet::setRealValue(" << GetName() << ") ERROR object '" << name << "' is not of type RooAbsRealLValue" << endl ;
return kTRUE;
}
rar->setVal(newVal) ;
return kFALSE ;
}
const char* RooArgSet::getCatLabel(const char* name, const char* defVal, Bool_t verbose) const
{
RooAbsArg* raa = find(name) ;
if (!raa) {
if (verbose) coutE(InputArguments) << "RooArgSet::getCatLabel(" << GetName() << ") ERROR no object with name '" << name << "' found" << endl ;
return defVal ;
}
RooAbsCategory* rac = dynamic_cast<RooAbsCategory*>(raa) ;
if (!rac) {
if (verbose) coutE(InputArguments) << "RooArgSet::getCatLabel(" << GetName() << ") ERROR object '" << name << "' is not of type RooAbsCategory" << endl ;
return defVal ;
}
return rac->getLabel() ;
}
Bool_t RooArgSet::setCatLabel(const char* name, const char* newVal, Bool_t verbose)
{
RooAbsArg* raa = find(name) ;
if (!raa) {
if (verbose) coutE(InputArguments) << "RooArgSet::setCatLabel(" << GetName() << ") ERROR no object with name '" << name << "' found" << endl ;
return kTRUE ;
}
RooAbsCategoryLValue* rac = dynamic_cast<RooAbsCategoryLValue*>(raa) ;
if (!rac) {
if (verbose) coutE(InputArguments) << "RooArgSet::setCatLabel(" << GetName() << ") ERROR object '" << name << "' is not of type RooAbsCategory" << endl ;
return kTRUE ;
}
rac->setLabel(newVal) ;
return kFALSE ;
}
Int_t RooArgSet::getCatIndex(const char* name, Int_t defVal, Bool_t verbose) const
{
RooAbsArg* raa = find(name) ;
if (!raa) {
if (verbose) coutE(InputArguments) << "RooArgSet::getCatLabel(" << GetName() << ") ERROR no object with name '" << name << "' found" << endl ;
return defVal ;
}
RooAbsCategory* rac = dynamic_cast<RooAbsCategory*>(raa) ;
if (!rac) {
if (verbose) coutE(InputArguments) << "RooArgSet::getCatLabel(" << GetName() << ") ERROR object '" << name << "' is not of type RooAbsCategory" << endl ;
return defVal ;
}
return rac->getIndex() ;
}
Bool_t RooArgSet::setCatIndex(const char* name, Int_t newVal, Bool_t verbose)
{
RooAbsArg* raa = find(name) ;
if (!raa) {
if (verbose) coutE(InputArguments) << "RooArgSet::setCatLabel(" << GetName() << ") ERROR no object with name '" << name << "' found" << endl ;
return kTRUE ;
}
RooAbsCategoryLValue* rac = dynamic_cast<RooAbsCategoryLValue*>(raa) ;
if (!rac) {
if (verbose) coutE(InputArguments) << "RooArgSet::setCatLabel(" << GetName() << ") ERROR object '" << name << "' is not of type RooAbsCategory" << endl ;
return kTRUE ;
}
rac->setIndex(newVal) ;
return kFALSE ;
}
const char* RooArgSet::getStringValue(const char* name, const char* defVal, Bool_t verbose) const
{
RooAbsArg* raa = find(name) ;
if (!raa) {
if (verbose) coutE(InputArguments) << "RooArgSet::getStringValue(" << GetName() << ") ERROR no object with name '" << name << "' found" << endl ;
return defVal ;
}
RooAbsString* ras = dynamic_cast<RooAbsString*>(raa) ;
if (!ras) {
if (verbose) coutE(InputArguments) << "RooArgSet::getStringValue(" << GetName() << ") ERROR object '" << name << "' is not of type RooAbsString" << endl ;
return defVal ;
}
return ras->getVal() ;
}
Bool_t RooArgSet::setStringValue(const char* name, const char* newVal, Bool_t verbose)
{
RooAbsArg* raa = find(name) ;
if (!raa) {
if (verbose) coutE(InputArguments) << "RooArgSet::setStringValue(" << GetName() << ") ERROR no object with name '" << name << "' found" << endl ;
return kTRUE ;
}
RooStringVar* ras = dynamic_cast<RooStringVar*>(raa) ;
if (!ras) {
if (verbose) coutE(InputArguments) << "RooArgSet::setStringValue(" << GetName() << ") ERROR object '" << name << "' is not of type RooAbsString" << endl ;
return kTRUE ;
}
ras->setVal(newVal) ;
return kFALSE ;
}
void RooArgSet::writeToFile(const char* fileName) const
{
ofstream ofs(fileName) ;
if (ofs.fail()) {
coutE(InputArguments) << "RooArgSet::writeToFile(" << GetName() << ") error opening file " << fileName << endl ;
return ;
}
writeToStream(ofs,kFALSE) ;
}
Bool_t RooArgSet::readFromFile(const char* fileName, const char* flagReadAtt, const char* section, Bool_t verbose)
{
ifstream ifs(fileName) ;
if (ifs.fail()) {
coutE(InputArguments) << "RooArgSet::readFromFile(" << GetName() << ") error opening file " << fileName << endl ;
return kTRUE ;
}
return readFromStream(ifs,kFALSE,flagReadAtt,section,verbose) ;
}
void RooArgSet::writeToStream(ostream& os, Bool_t compact, const char* ) const
{
if (compact) {
coutE(InputArguments) << "RooArgSet::writeToStream(" << GetName() << ") compact mode not supported" << endl ;
return ;
}
TIterator *iterator= createIterator();
RooAbsArg *next = 0;
while((0 != (next= (RooAbsArg*)iterator->Next()))) {
os << next->GetName() << " = " ;
next->writeToStream(os,kFALSE) ;
os << endl ;
}
delete iterator;
}
Bool_t RooArgSet::readFromStream(istream& is, Bool_t compact, const char* flagReadAtt, const char* section, Bool_t verbose)
{
if (compact) {
coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << ") compact mode not supported" << endl ;
return kTRUE ;
}
RooStreamParser parser(is) ;
parser.setPunctuation("=") ;
TString token ;
Bool_t retVal(kFALSE) ;
Bool_t anyCondTrue[100] ;
Bool_t condStack[100] ;
Bool_t lastLineWasElse=kFALSE ;
Int_t condStackLevel=0 ;
condStack[0]=kTRUE ;
TString sectionHdr("[") ;
if (section) sectionHdr.Append(section) ;
sectionHdr.Append("]") ;
Bool_t inSection(section?kFALSE:kTRUE) ;
Bool_t reprocessToken = kFALSE ;
while (1) {
if (is.eof() || is.fail() || parser.atEOF()) {
break ;
}
if (!reprocessToken) {
token = parser.readToken() ;
}
reprocessToken = kFALSE ;
if (token.IsNull()) {
continue ;
}
if (!token.CompareTo("include")) {
if (parser.atEOL()) {
coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName()
<< "): no filename found after include statement" << endl ;
return kTRUE ;
}
TString filename = parser.readLine() ;
ifstream incfs(filename) ;
if (!incfs.good()) {
coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): cannot open include file " << filename << endl ;
return kTRUE ;
}
coutI(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): processing include file "
<< filename << endl ;
if (readFromStream(incfs,compact,flagReadAtt,inSection?0:section,verbose)) return kTRUE ;
continue ;
}
if (*token.Data()=='[') {
TString hdr(token) ;
const char* last = token.Data() + token.Length() -1 ;
if (*last != ']') {
hdr.Append(" ") ;
hdr.Append(parser.readLine()) ;
}
if (section) {
inSection = !sectionHdr.CompareTo(hdr) ;
}
continue ;
}
if (!inSection) {
parser.zapToEnd(kTRUE) ;
continue ;
}
if (!token.CompareTo("if")) {
TString expr = parser.readLine() ;
RooFormula form(expr,expr,*this) ;
if (!form.ok()) return kTRUE ;
Bool_t status = form.eval()?kTRUE:kFALSE ;
if (lastLineWasElse) {
anyCondTrue[condStackLevel] |= status ;
lastLineWasElse=kFALSE ;
} else {
condStackLevel++ ;
anyCondTrue[condStackLevel] = status ;
}
condStack[condStackLevel] = status ;
if (verbose) cxcoutD(Eval) << "RooArgSet::readFromStream(" << GetName()
<< "): conditional expression " << expr << " = "
<< (condStack[condStackLevel]?"true":"false") << endl ;
continue ;
}
if (!token.CompareTo("else")) {
if (condStackLevel==0) {
coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): unmatched 'else'" << endl ;
}
if (parser.atEOL()) {
condStack[condStackLevel] = !anyCondTrue[condStackLevel] ;
parser.zapToEnd(kFALSE) ;
continue ;
} else {
token = parser.readToken() ;
if (token.CompareTo("if")) {
coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): syntax error: 'else " << token << "'" << endl ;
return kTRUE ;
} else {
if (anyCondTrue[condStackLevel]) {
condStack[condStackLevel] = kFALSE ;
parser.zapToEnd(kFALSE) ;
continue ;
} else {
reprocessToken = kTRUE ;
lastLineWasElse=kTRUE ;
continue ;
}
}
}
}
if (!token.CompareTo("endif")) {
if (condStackLevel==0) {
coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): unmatched 'endif'" << endl ;
return kTRUE ;
}
condStackLevel-- ;
continue ;
}
if (condStack[condStackLevel]) {
if (!token.CompareTo("echo")) {
TString message = parser.readLine() ;
coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): >> " << message << endl ;
continue ;
}
if (!token.CompareTo("abort")) {
TString message = parser.readLine() ;
coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): USER ABORT" << endl ;
return kTRUE ;
}
RooAbsArg *arg ;
if ((arg = find(token)) && !arg->getAttribute("Dynamic")) {
if (parser.expectToken("=",kTRUE)) {
parser.zapToEnd(kTRUE) ;
retVal=kTRUE ;
coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName()
<< "): missing '=' sign: " << arg << endl ;
continue ;
}
Bool_t argRet = arg->readFromStream(is,kFALSE,verbose) ;
if (!argRet && flagReadAtt) arg->setAttribute(flagReadAtt,kTRUE) ;
retVal |= argRet ;
} else {
if (verbose) {
coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): argument "
<< token << " not in list, ignored" << endl ;
}
parser.zapToEnd(kTRUE) ;
}
} else {
parser.readLine() ;
}
}
if (condStackLevel!=0) {
coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): missing 'endif'" << endl ;
return kTRUE ;
}
return retVal ;
}