// RooDataSet is a container class to hold unbinned data. Each data point
// in N-dimensional space is represented by a RooArgSet of RooRealVar, RooCategory
// or RooStringVar objects
// END_HTML
#include "RooFit.h"
#include "Riostream.h"
#include "Riostream.h"
#include <fstream>
#include "TTree.h"
#include "TH2.h"
#include "TDirectory.h"
#include "RooDataSet.h"
#include "RooPlot.h"
#include "RooAbsReal.h"
#include "Roo1DTable.h"
#include "RooCategory.h"
#include "RooFormulaVar.h"
#include "RooArgList.h"
#include "RooAbsRealLValue.h"
#include "RooRealVar.h"
#include "RooDataHist.h"
#include "RooMsgService.h"
#include "RooCmdConfig.h"
#include "RooHist.h"
#include "TROOT.h"
#include "TFile.h"
#include "RooTreeDataStore.h"
#include "RooVectorDataStore.h"
#include "RooCompositeDataStore.h"
#include "RooTreeData.h"
#include "RooSentinel.h"
#if (__GNUC__==3&&__GNUC_MINOR__==2&&__GNUC_PATCHLEVEL__==3)
char* operator+( streampos&, char* );
#endif
ClassImp(RooDataSet)
;
char* RooDataSet::_poolBegin = 0 ;
char* RooDataSet::_poolCur = 0 ;
char* RooDataSet::_poolEnd = 0 ;
#define POOLSIZE 1048576
struct POOLDATA
{
void* _base ;
} ;
static std::list<POOLDATA> _memPoolList ;
void RooDataSet::cleanup()
{
std::list<POOLDATA>::iterator iter = _memPoolList.begin() ;
while(iter!=_memPoolList.end()) {
free(iter->_base) ;
iter->_base=0 ;
iter++ ;
}
_memPoolList.clear() ;
}
#ifdef USEMEMPOOL
void* RooDataSet::operator new (size_t bytes)
{
if (!_poolBegin || _poolCur+(sizeof(RooDataSet)) >= _poolEnd) {
if (_poolBegin!=0) {
oocxcoutD((TObject*)0,Caching) << "RooDataSet::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) << "RooDataSet::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 RooDataSet::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
RooDataSet::RooDataSet() : _wgtVar(0)
{
}
RooDataSet::RooDataSet(const char* name, const char* title, const RooArgSet& vars, const RooCmdArg& arg1, const RooCmdArg& arg2, const RooCmdArg& arg3,
const RooCmdArg& arg4,const RooCmdArg& arg5,const RooCmdArg& arg6,const RooCmdArg& arg7,const RooCmdArg& arg8) :
RooAbsData(name,title,RooArgSet(vars,(RooAbsArg*)RooCmdConfig::decodeObjOnTheFly("RooDataSet::RooDataSet", "IndexCat",0,0,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8)))
{
RooCmdConfig pc(Form("RooDataSet::ctor(%s)",GetName())) ;
pc.defineInt("ownLinked","OwnLinked",0) ;
pc.defineObject("impTree","ImportTree",0) ;
pc.defineObject("impData","ImportData",0) ;
pc.defineObject("indexCat","IndexCat",0) ;
pc.defineObject("impSliceData","ImportDataSlice",0,0,kTRUE) ;
pc.defineString("impSliceState","ImportDataSlice",0,"",kTRUE) ;
pc.defineObject("lnkSliceData","LinkDataSlice",0,0,kTRUE) ;
pc.defineString("lnkSliceState","LinkDataSlice",0,"",kTRUE) ;
pc.defineString("cutSpec","CutSpec",0,"") ;
pc.defineObject("cutVar","CutVar",0) ;
pc.defineString("cutRange","CutRange",0,"") ;
pc.defineString("wgtVarName","WeightVarName",0,"") ;
pc.defineString("fname","ImportFromFile",0,"") ;
pc.defineString("tname","ImportFromFile",1,"") ;
pc.defineObject("wgtVar","WeightVar",0) ;
pc.defineObject("dummy1","ImportDataSliceMany",0) ;
pc.defineObject("dummy2","LinkDataSliceMany",0) ;
pc.defineSet("errorSet","StoreError",0) ;
pc.defineSet("asymErrSet","StoreAsymError",0) ;
pc.defineMutex("ImportTree","ImportData","ImportDataSlice","LinkDataSlice","ImportFromFile") ;
pc.defineMutex("CutSpec","CutVar") ;
pc.defineMutex("WeightVarName","WeightVar") ;
pc.defineDependency("ImportDataSlice","IndexCat") ;
pc.defineDependency("LinkDataSlice","IndexCat") ;
pc.defineDependency("OwnLinked","LinkDataSlice") ;
RooLinkedList l ;
l.Add((TObject*)&arg1) ; l.Add((TObject*)&arg2) ;
l.Add((TObject*)&arg3) ; l.Add((TObject*)&arg4) ;
l.Add((TObject*)&arg5) ; l.Add((TObject*)&arg6) ;
l.Add((TObject*)&arg7) ; l.Add((TObject*)&arg8) ;
pc.process(l) ;
if (!pc.ok(kTRUE)) {
assert(0) ;
return ;
}
TTree* impTree = static_cast<TTree*>(pc.getObject("impTree")) ;
RooDataSet* impData = static_cast<RooDataSet*>(pc.getObject("impData")) ;
RooFormulaVar* cutVar = static_cast<RooFormulaVar*>(pc.getObject("cutVar")) ;
const char* cutSpec = pc.getString("cutSpec","",kTRUE) ;
const char* cutRange = pc.getString("cutRange","",kTRUE) ;
const char* wgtVarName = pc.getString("wgtVarName","",kTRUE) ;
RooRealVar* wgtVar = static_cast<RooRealVar*>(pc.getObject("wgtVar")) ;
const char* impSliceNames = pc.getString("impSliceState","",kTRUE) ;
const RooLinkedList& impSliceData = pc.getObjectList("impSliceData") ;
const char* lnkSliceNames = pc.getString("lnkSliceState","",kTRUE) ;
const RooLinkedList& lnkSliceData = pc.getObjectList("lnkSliceData") ;
RooCategory* indexCat = static_cast<RooCategory*>(pc.getObject("indexCat")) ;
RooArgSet* errorSet = pc.getSet("errorSet") ;
RooArgSet* asymErrorSet = pc.getSet("asymErrSet") ;
const char* fname = pc.getString("fname") ;
const char* tname = pc.getString("tname") ;
Int_t ownLinked = pc.getInt("ownLinked") ;
if (lnkSliceNames) {
map<string,RooAbsData*> hmap ;
if (indexCat) {
char tmp[10240] ;
strlcpy(tmp,lnkSliceNames,10240) ;
char* token = strtok(tmp,",") ;
TIterator* hiter = lnkSliceData.MakeIterator() ;
while(token) {
hmap[token] = (RooAbsData*) hiter->Next() ;
token = strtok(0,",") ;
}
delete hiter ;
}
if (wgtVar) {
wgtVarName = wgtVar->GetName() ;
}
appendToDir(this,kTRUE) ;
initialize(0) ;
map<string,RooAbsDataStore*> storeMap ;
RooCategory* icat = (RooCategory*) (indexCat ? _vars.find(indexCat->GetName()) : 0 ) ;
if (!icat) {
throw std::string("RooDataSet::RooDataSet() ERROR in constructor, cannot find index category") ;
}
for (map<string,RooAbsData*>::iterator hiter = hmap.begin() ; hiter!=hmap.end() ; ++hiter) {
if (indexCat && !indexCat->lookupType(hiter->first.c_str())) {
indexCat->defineType(hiter->first.c_str()) ;
coutI(InputArguments) << "RooDataSet::ctor(" << GetName() << ") defining state \"" << hiter->first << "\" in index category " << indexCat->GetName() << endl ;
}
if (icat && !icat->lookupType(hiter->first.c_str())) {
icat->defineType(hiter->first.c_str()) ;
}
icat->setLabel(hiter->first.c_str()) ;
storeMap[icat->getLabel()]=hiter->second->store() ;
if (ownLinked) {
addOwnedComponent(hiter->first.c_str(),*hiter->second) ;
}
}
_dstore = new RooCompositeDataStore(name,title,_vars,*icat,storeMap) ;
} else {
if (wgtVar) {
wgtVarName = wgtVar->GetName() ;
}
RooTreeDataStore* tstore(0) ;
RooVectorDataStore* vstore(0) ;
if (defaultStorageType==Tree) {
tstore = new RooTreeDataStore(name,title,_vars,wgtVarName) ;
_dstore = tstore ;
} else if (defaultStorageType==Vector) {
vstore = new RooVectorDataStore(name,title,_vars,wgtVarName) ;
_dstore = vstore ;
} else {
_dstore = 0 ;
}
map<string,RooDataSet*> hmap ;
if (indexCat) {
char tmp[1024] ;
strlcpy(tmp,impSliceNames,1024) ;
char* token = strtok(tmp,",") ;
TIterator* hiter = impSliceData.MakeIterator() ;
while(token) {
hmap[token] = (RooDataSet*) hiter->Next() ;
token = strtok(0,",") ;
}
delete hiter ;
}
if (errorSet) {
RooArgSet* intErrorSet = (RooArgSet*) _vars.selectCommon(*errorSet) ;
intErrorSet->setAttribAll("StoreError") ;
TIterator* iter = intErrorSet->createIterator() ;
RooAbsArg* arg ;
while((arg=(RooAbsArg*)iter->Next())) {
arg->attachToStore(*_dstore) ;
}
delete iter ;
delete intErrorSet ;
}
if (asymErrorSet) {
RooArgSet* intAsymErrorSet = (RooArgSet*) _vars.selectCommon(*asymErrorSet) ;
intAsymErrorSet->setAttribAll("StoreAsymError") ;
TIterator* iter = intAsymErrorSet->createIterator() ;
RooAbsArg* arg ;
while((arg=(RooAbsArg*)iter->Next())) {
arg->attachToStore(*_dstore) ;
}
delete iter ;
delete intAsymErrorSet ;
}
if (wgtVar) {
wgtVarName = wgtVar->GetName() ;
}
appendToDir(this,kTRUE) ;
if (wgtVarName && *wgtVarName) {
initialize(wgtVarName) ;
} else {
if (impData && impData->_wgtVar && vars.find(impData->_wgtVar->GetName())) {
initialize(impData->_wgtVar->GetName()) ;
} else if (indexCat) {
RooDataSet* firstDS = hmap.begin()->second ;
if (firstDS->_wgtVar && vars.find(firstDS->_wgtVar->GetName())) {
initialize(firstDS->_wgtVar->GetName()) ;
} else {
initialize(0) ;
}
} else {
initialize(0) ;
}
}
if (cutSpec && *cutSpec) {
if (indexCat) {
RooCategory* icat = (RooCategory*) _vars.find(indexCat->GetName()) ;
for (map<string,RooDataSet*>::iterator hiter = hmap.begin() ; hiter!=hmap.end() ; ++hiter) {
if (!indexCat->lookupType(hiter->first.c_str())) {
indexCat->defineType(hiter->first.c_str()) ;
coutI(InputArguments) << "RooDataSet::ctor(" << GetName() << ") defining state \"" << hiter->first << "\" in index category " << indexCat->GetName() << endl ;
}
if (!icat->lookupType(hiter->first.c_str())) {
icat->defineType(hiter->first.c_str()) ;
}
icat->setLabel(hiter->first.c_str()) ;
RooFormulaVar cutVarTmp(cutSpec,cutSpec,hiter->second->_vars) ;
_dstore->loadValues(hiter->second->store(),&cutVarTmp,cutRange) ;
}
} else if (impData) {
RooFormulaVar cutVarTmp(cutSpec,cutSpec,impData->_vars) ;
_dstore->loadValues(impData->store(),&cutVarTmp,cutRange);
} else if (impTree) {
RooFormulaVar cutVarTmp(cutSpec,cutSpec,_vars) ;
if (tstore) {
tstore->loadValues(impTree,&cutVarTmp,cutRange);
} else {
RooTreeDataStore tmpstore(name,title,_vars,wgtVarName) ;
tmpstore.loadValues(impTree,&cutVarTmp,cutRange) ;
_dstore->append(tmpstore) ;
}
} else if (fname && strlen(fname)) {
TFile *f = TFile::Open(fname) ;
if (!f) {
coutE(InputArguments) << "RooDataSet::ctor(" << GetName() << ") ERROR file '" << fname << "' cannot be opened or does not exist" << endl ;
throw string(Form("RooDataSet::ctor(%s) ERROR file %s cannot be opened or does not exist",GetName(),fname)) ;
}
TTree* t = dynamic_cast<TTree*>(f->Get(tname)) ;
if (!t) {
coutE(InputArguments) << "RooDataSet::ctor(" << GetName() << ") ERROR file '" << fname << "' does not contain a TTree named '" << tname << "'" << endl ;
throw string(Form("RooDataSet::ctor(%s) ERROR file %s does not contain a TTree named %s",GetName(),fname,tname)) ;
}
RooFormulaVar cutVarTmp(cutSpec,cutSpec,_vars) ;
if (tstore) {
tstore->loadValues(t,&cutVarTmp,cutRange);
} else {
RooTreeDataStore tmpstore(name,title,_vars,wgtVarName) ;
tmpstore.loadValues(impTree,&cutVarTmp,cutRange) ;
_dstore->append(tmpstore) ;
}
f->Close() ;
}
} else if (cutVar) {
if (indexCat) {
RooCategory* icat = (RooCategory*) _vars.find(indexCat->GetName()) ;
for (map<string,RooDataSet*>::iterator hiter = hmap.begin() ; hiter!=hmap.end() ; ++hiter) {
if (!indexCat->lookupType(hiter->first.c_str())) {
indexCat->defineType(hiter->first.c_str()) ;
coutI(InputArguments) << "RooDataSet::ctor(" << GetName() << ") defining state \"" << hiter->first << "\" in index category " << indexCat->GetName() << endl ;
}
if (!icat->lookupType(hiter->first.c_str())) {
icat->defineType(hiter->first.c_str()) ;
}
icat->setLabel(hiter->first.c_str()) ;
_dstore->loadValues(hiter->second->store(),cutVar,cutRange) ;
}
} else if (impData) {
_dstore->loadValues(impData->store(),cutVar,cutRange);
} else if (impTree) {
if (tstore) {
tstore->loadValues(impTree,cutVar,cutRange);
} else {
RooTreeDataStore tmpstore(name,title,_vars,wgtVarName) ;
tmpstore.loadValues(impTree,cutVar,cutRange) ;
_dstore->append(tmpstore) ;
}
} else if (fname && strlen(fname)) {
TFile *f = TFile::Open(fname) ;
if (!f) {
coutE(InputArguments) << "RooDataSet::ctor(" << GetName() << ") ERROR file '" << fname << "' cannot be opened or does not exist" << endl ;
throw string(Form("RooDataSet::ctor(%s) ERROR file %s cannot be opened or does not exist",GetName(),fname)) ;
}
TTree* t = dynamic_cast<TTree*>(f->Get(tname)) ;
if (!t) {
coutE(InputArguments) << "RooDataSet::ctor(" << GetName() << ") ERROR file '" << fname << "' does not contain a TTree named '" << tname << "'" << endl ;
throw string(Form("RooDataSet::ctor(%s) ERROR file %s does not contain a TTree named %s",GetName(),fname,tname)) ;
}
if (tstore) {
tstore->loadValues(t,cutVar,cutRange);
} else {
RooTreeDataStore tmpstore(name,title,_vars,wgtVarName) ;
tmpstore.loadValues(impTree,cutVar,cutRange) ;
_dstore->append(tmpstore) ;
}
f->Close() ;
}
} else {
if (indexCat) {
RooCategory* icat = (RooCategory*) _vars.find(indexCat->GetName()) ;
for (map<string,RooDataSet*>::iterator hiter = hmap.begin() ; hiter!=hmap.end() ; ++hiter) {
if (!indexCat->lookupType(hiter->first.c_str())) {
indexCat->defineType(hiter->first.c_str()) ;
coutI(InputArguments) << "RooDataSet::ctor(" << GetName() << ") defining state \"" << hiter->first << "\" in index category " << indexCat->GetName() << endl ;
}
if (!icat->lookupType(hiter->first.c_str())) {
icat->defineType(hiter->first.c_str()) ;
}
icat->setLabel(hiter->first.c_str()) ;
_dstore->loadValues(hiter->second->store(),0,cutRange) ;
}
} else if (impData) {
_dstore->loadValues(impData->store(),0,cutRange);
} else if (impTree) {
if (tstore) {
tstore->loadValues(impTree,0,cutRange);
} else {
RooTreeDataStore tmpstore(name,title,_vars,wgtVarName) ;
tmpstore.loadValues(impTree,0,cutRange) ;
_dstore->append(tmpstore) ;
}
} else if (fname && strlen(fname)) {
TFile *f = TFile::Open(fname) ;
if (!f) {
coutE(InputArguments) << "RooDataSet::ctor(" << GetName() << ") ERROR file '" << fname << "' cannot be opened or does not exist" << endl ;
throw string(Form("RooDataSet::ctor(%s) ERROR file %s cannot be opened or does not exist",GetName(),fname)) ;
}
TTree* t = dynamic_cast<TTree*>(f->Get(tname)) ;
if (!t) {
coutE(InputArguments) << "RooDataSet::ctor(" << GetName() << ") ERROR file '" << fname << "' does not contain a TTree named '" << tname << "'" << endl ;
throw string(Form("RooDataSet::ctor(%s) ERROR file %s does not contain a TTree named %s",GetName(),fname,tname)) ;
}
if (tstore) {
tstore->loadValues(t,0,cutRange);
} else {
RooTreeDataStore tmpstore(name,title,_vars,wgtVarName) ;
tmpstore.loadValues(impTree,0,cutRange) ;
_dstore->append(tmpstore) ;
}
f->Close() ;
}
}
}
}
RooDataSet::RooDataSet(const char *name, const char *title, const RooArgSet& vars, const char* wgtVarName) :
RooAbsData(name,title,vars)
{
_dstore = (defaultStorageType==Tree) ? ((RooAbsDataStore*) new RooTreeDataStore(name,title,_vars,wgtVarName)) :
((RooAbsDataStore*) new RooVectorDataStore(name,title,_vars,wgtVarName)) ;
appendToDir(this,kTRUE) ;
initialize(wgtVarName) ;
}
RooDataSet::RooDataSet(const char *name, const char *title, RooDataSet *dset,
const RooArgSet& vars, const char *cuts, const char* wgtVarName) :
RooAbsData(name,title,vars)
{
_dstore = new RooTreeDataStore(name,title,_vars,*dset->_dstore,cuts,wgtVarName) ;
appendToDir(this,kTRUE) ;
if (wgtVarName) {
initialize(wgtVarName) ;
} else {
if (dset->_wgtVar && vars.find(dset->_wgtVar->GetName())) {
initialize(dset->_wgtVar->GetName()) ;
} else {
initialize(0) ;
}
}
}
RooDataSet::RooDataSet(const char *name, const char *title, RooDataSet *dset,
const RooArgSet& vars, const RooFormulaVar& cutVar, const char* wgtVarName) :
RooAbsData(name,title,vars)
{
_dstore = new RooTreeDataStore(name,title,_vars,*dset->_dstore,cutVar,wgtVarName) ;
appendToDir(this,kTRUE) ;
if (wgtVarName) {
initialize(wgtVarName) ;
} else {
if (dset->_wgtVar && vars.find(dset->_wgtVar->GetName())) {
initialize(dset->_wgtVar->GetName()) ;
} else {
initialize(0) ;
}
}
}
RooDataSet::RooDataSet(const char *name, const char *title, TTree *intree,
const RooArgSet& vars, const RooFormulaVar& cutVar, const char* wgtVarName) :
RooAbsData(name,title,vars)
{
_dstore = new RooTreeDataStore(name,title,_vars,*intree,cutVar,wgtVarName) ;
appendToDir(this,kTRUE) ;
initialize(wgtVarName) ;
}
RooDataSet::RooDataSet(const char *name, const char *title, TTree *intree,
const RooArgSet& vars, const char *selExpr, const char* wgtVarName) :
RooAbsData(name,title,vars)
{
_dstore = new RooTreeDataStore(name,title,_vars,*intree,selExpr,wgtVarName) ;
appendToDir(this,kTRUE) ;
initialize(wgtVarName) ;
}
RooDataSet::RooDataSet(RooDataSet const & other, const char* newname) :
RooAbsData(other,newname), RooDirItem()
{
appendToDir(this,kTRUE) ;
initialize(other._wgtVar?other._wgtVar->GetName():0) ;
}
RooDataSet::RooDataSet(const char *name, const char *title, RooDataSet *dset,
const RooArgSet& vars, const RooFormulaVar* cutVar, const char* cutRange,
Int_t nStart, Int_t nStop, Bool_t copyCache, const char* wgtVarName) :
RooAbsData(name,title,vars)
{
_dstore = (defaultStorageType==Tree) ?
((RooAbsDataStore*) new RooTreeDataStore(name,title,*dset->_dstore,_vars,cutVar,cutRange,nStart,nStop,copyCache,wgtVarName)) :
((RooAbsDataStore*) new RooVectorDataStore(name,title,*dset->_dstore,_vars,cutVar,cutRange,nStart,nStop,copyCache,wgtVarName)) ;
_cachedVars.add(_dstore->cachedVars()) ;
appendToDir(this,kTRUE) ;
initialize(dset->_wgtVar?dset->_wgtVar->GetName():0) ;
}
RooArgSet RooDataSet::addWgtVar(const RooArgSet& origVars, const RooAbsArg* wgtVar)
{
RooArgSet tmp(origVars) ;
if (wgtVar) tmp.add(*wgtVar) ;
return tmp ;
}
RooAbsData* RooDataSet::cacheClone(const RooAbsArg* newCacheOwner, const RooArgSet* newCacheVars, const char* newName)
{
RooDataSet* dset = new RooDataSet(newName?newName:GetName(),GetTitle(),this,_vars,(RooFormulaVar*)0,0,0,2000000000,kTRUE,_wgtVar?_wgtVar->GetName():0) ;
RooArgSet* selCacheVars = (RooArgSet*) newCacheVars->selectCommon(dset->_cachedVars) ;
dset->attachCache(newCacheOwner, *selCacheVars) ;
delete selCacheVars ;
return dset ;
}
RooAbsData* RooDataSet::emptyClone(const char* newName, const char* newTitle, const RooArgSet* vars) const
{
RooArgSet vars2 ;
if (vars) {
vars2.add(*vars) ;
if (_wgtVar && !vars2.find(_wgtVar->GetName())) {
vars2.add(*_wgtVar) ;
}
} else {
vars2.add(_vars) ;
}
RooDataSet* dset = new RooDataSet(newName?newName:GetName(),newTitle?newTitle:GetTitle(),vars2,_wgtVar?_wgtVar->GetName():0) ;
return dset ;
}
void RooDataSet::initialize(const char* wgtVarName)
{
_varsNoWgt.removeAll() ;
_varsNoWgt.add(_vars) ;
_wgtVar = 0 ;
if (wgtVarName) {
RooAbsArg* wgt = _varsNoWgt.find(wgtVarName) ;
if (!wgt) {
coutW(DataHandling) << "RooDataSet::RooDataSet(" << GetName() << ") WARNING: designated weight variable "
<< wgtVarName << " not found in set of variables, no weighting will be assigned" << endl ;
} else if (!dynamic_cast<RooRealVar*>(wgt)) {
coutW(DataHandling) << "RooDataSet::RooDataSet(" << GetName() << ") WARNING: designated weight variable "
<< wgtVarName << " is not of type RooRealVar, no weighting will be assigned" << endl ;
} else {
_varsNoWgt.remove(*wgt) ;
_wgtVar = (RooRealVar*) wgt ;
}
}
}
RooAbsData* RooDataSet::reduceEng(const RooArgSet& varSubset, const RooFormulaVar* cutVar, const char* cutRange,
Int_t nStart, Int_t nStop, Bool_t copyCache)
{
checkInit() ;
RooArgSet tmp(varSubset) ;
if (_wgtVar) {
tmp.add(*_wgtVar) ;
}
RooDataSet* ret = new RooDataSet(GetName(), GetTitle(), this, tmp, cutVar, cutRange, nStart, nStop, copyCache,_wgtVar?_wgtVar->GetName():0) ;
return ret ;
}
RooDataSet::~RooDataSet()
{
removeFromDir(this) ;
}
RooDataHist* RooDataSet::binnedClone(const char* newName, const char* newTitle) const
{
TString title, name ;
if (newName) {
name = newName ;
} else {
name = Form("%s_binned",GetName()) ;
}
if (newTitle) {
title = newTitle ;
} else {
title = Form("%s_binned",GetTitle()) ;
}
return new RooDataHist(name,title,*get(),*this) ;
}
Double_t RooDataSet::weight() const
{
return store()->weight() ;
}
void RooDataSet::weightError(Double_t& lo, Double_t& hi, ErrorType etype) const
{
store()->weightError(lo,hi,etype) ;
}
Double_t RooDataSet::weightError(ErrorType etype) const
{
return store()->weightError(etype) ;
}
const RooArgSet* RooDataSet::get(Int_t index) const
{
const RooArgSet* ret = RooAbsData::get(index) ;
return ret ? &_varsNoWgt : 0 ;
}
Double_t RooDataSet::sumEntries() const
{
return store()->sumEntries() ;
if (!isWeighted()) {
return numEntries() ;
}
Double_t sumw(0) ;
Int_t i ;
for (i=0 ; i<numEntries() ; i++) {
get(i) ;
sumw += weight() ;
}
return sumw ;
}
Double_t RooDataSet::sumEntries(const char* cutSpec, const char* cutRange) const
{
RooFormula* select = 0 ;
if (cutSpec) {
select = new RooFormula("select",cutSpec,*get()) ;
}
if (!select && !cutRange && !isWeighted()) {
return numEntries() ;
}
Double_t sumw(0) ;
Int_t i ;
for (i=0 ; i<numEntries() ; i++) {
get(i) ;
if (select && select->eval()==0.) continue ;
if (cutRange && !_vars.allInRange(cutRange)) continue ;
sumw += weight() ;
}
if (select) delete select ;
return sumw ;
}
Bool_t RooDataSet::isWeighted() const
{
return store()->isWeighted() ;
}
Bool_t RooDataSet::isNonPoissonWeighted() const
{
if (!_wgtVar) return kFALSE ;
for (int i=0 ; i<numEntries() ; i++) {
get(i) ;
if (fabs(weight()-Int_t(weight()))>1e-10) return kTRUE ;
}
if (sumEntries()<numEntries()) return kTRUE ;
return kFALSE ;
}
const RooArgSet* RooDataSet::get() const
{
return &_varsNoWgt ;
}
void RooDataSet::add(const RooArgSet& data, Double_t wgt, Double_t wgtError)
{
checkInit() ;
_varsNoWgt = data;
if (_wgtVar) {
_wgtVar->setVal(wgt) ;
if (wgtError!=0.) {
_wgtVar->setError(wgtError) ;
}
}
fill();
}
void RooDataSet::add(const RooArgSet& indata, Double_t inweight, Double_t weightErrorLo, Double_t weightErrorHi)
{
checkInit() ;
_varsNoWgt = indata;
if (_wgtVar) {
_wgtVar->setVal(inweight) ;
_wgtVar->setAsymError(weightErrorLo,weightErrorHi) ;
}
fill();
}
void RooDataSet::addFast(const RooArgSet& data, Double_t wgt, Double_t wgtError)
{
checkInit() ;
_varsNoWgt.assignFast(data,_dstore->dirtyProp());
if (_wgtVar) {
_wgtVar->setVal(wgt) ;
if (wgtError!=0.) {
_wgtVar->setError(wgtError) ;
}
}
fill();
}
Bool_t RooDataSet::merge(RooDataSet* data1, RooDataSet* data2, RooDataSet* data3,
RooDataSet* data4, RooDataSet* data5, RooDataSet* data6)
{
checkInit() ;
list<RooDataSet*> dsetList ;
if (data1) dsetList.push_back(data1) ;
if (data2) dsetList.push_back(data2) ;
if (data3) dsetList.push_back(data3) ;
if (data4) dsetList.push_back(data4) ;
if (data5) dsetList.push_back(data5) ;
if (data6) dsetList.push_back(data6) ;
return merge(dsetList) ;
}
Bool_t RooDataSet::merge(list<RooDataSet*>dsetList)
{
checkInit() ;
for (list<RooDataSet*>::iterator iter = dsetList.begin() ; iter != dsetList.end() ; iter++) {
if (numEntries()!=(*iter)->numEntries()) {
coutE(InputArguments) << "RooDataSet::merge(" << GetName() << ") ERROR: datasets have different size" << endl ;
return kTRUE ;
}
}
list<RooAbsDataStore*> dstoreList ;
for (list<RooDataSet*>::iterator iter = dsetList.begin() ; iter != dsetList.end() ; iter++) {
_vars.addClone((*iter)->_vars,kTRUE) ;
dstoreList.push_back((*iter)->store()) ;
}
RooAbsDataStore* mergedStore = _dstore->merge(_vars,dstoreList) ;
mergedStore->SetName(_dstore->GetName()) ;
mergedStore->SetTitle(_dstore->GetTitle()) ;
delete _dstore ;
_dstore = mergedStore ;
initialize(_wgtVar?_wgtVar->GetName():0) ;
return kFALSE ;
}
void RooDataSet::append(RooDataSet& data)
{
checkInit() ;
_dstore->append(*data._dstore) ;
}
RooAbsArg* RooDataSet::addColumn(RooAbsArg& var, Bool_t adjustRange)
{
checkInit() ;
RooAbsArg* ret = _dstore->addColumn(var,adjustRange) ;
_vars.addOwned(*ret) ;
initialize(_wgtVar?_wgtVar->GetName():0) ;
return ret ;
}
RooArgSet* RooDataSet::addColumns(const RooArgList& varList)
{
checkInit() ;
RooArgSet* ret = _dstore->addColumns(varList) ;
_vars.addOwned(*ret) ;
initialize(_wgtVar?_wgtVar->GetName():0) ;
return ret ;
}
TH2F* RooDataSet::createHistogram(const RooAbsRealLValue& var1, const RooAbsRealLValue& var2, const char* cuts, const char *name) const
{
checkInit() ;
return createHistogram(var1, var2, var1.getBins(), var2.getBins(), cuts, name);
}
TH2F* RooDataSet::createHistogram(const RooAbsRealLValue& var1, const RooAbsRealLValue& var2,
Int_t nx, Int_t ny, const char* cuts, const char *name) const
{
checkInit() ;
static Int_t counter(0) ;
Bool_t ownPlotVarX(kFALSE) ;
RooAbsReal* plotVarX= (RooAbsReal*)_vars.find(var1.GetName());
if(0 == plotVarX) {
if (!var1.dependsOn(_vars)) {
coutE(InputArguments) << GetName() << "::createHistogram: Argument " << var1.GetName()
<< " is not in dataset and is also not dependent on data set" << endl ;
return 0 ;
}
plotVarX = (RooAbsReal*) var1.Clone() ;
ownPlotVarX = kTRUE ;
plotVarX->redirectServers(const_cast<RooArgSet&>(_vars)) ;
}
Bool_t ownPlotVarY(kFALSE) ;
RooAbsReal* plotVarY= (RooAbsReal*)_vars.find(var2.GetName());
if(0 == plotVarY) {
if (!var2.dependsOn(_vars)) {
coutE(InputArguments) << GetName() << "::createHistogram: Argument " << var2.GetName()
<< " is not in dataset and is also not dependent on data set" << endl ;
return 0 ;
}
plotVarY = (RooAbsReal*) var2.Clone() ;
ownPlotVarY = kTRUE ;
plotVarY->redirectServers(const_cast<RooArgSet&>(_vars)) ;
}
RooFormula* select = 0;
if(0 != cuts && strlen(cuts)) {
select=new RooFormula(cuts,cuts,_vars);
if (!select || !select->ok()) {
delete select;
return 0 ;
}
}
TString histName(name);
histName.Prepend("_");
histName.Prepend(fName);
histName.Append("_") ;
histName.Append(Form("%08x",counter++)) ;
TH2F* histogram=new TH2F(histName.Data(), "Events", nx, var1.getMin(), var1.getMax(),
ny, var2.getMin(), var2.getMax());
if(!histogram) {
coutE(DataHandling) << fName << "::createHistogram: unable to create a new histogram" << endl;
return 0;
}
Int_t nevent= numEntries() ;
for(Int_t i=0; i < nevent; ++i)
{
get(i);
if (select && select->eval()==0) continue ;
histogram->Fill(plotVarX->getVal(), plotVarY->getVal()) ;
}
if (ownPlotVarX) delete plotVarX ;
if (ownPlotVarY) delete plotVarY ;
if (select) delete select ;
return histogram ;
}
RooPlot* RooDataSet::plotOnXY(RooPlot* frame, const RooCmdArg& arg1, const RooCmdArg& arg2,
const RooCmdArg& arg3, const RooCmdArg& arg4,
const RooCmdArg& arg5, const RooCmdArg& arg6,
const RooCmdArg& arg7, const RooCmdArg& arg8) const
{
checkInit() ;
RooLinkedList argList ;
argList.Add((TObject*)&arg1) ; argList.Add((TObject*)&arg2) ;
argList.Add((TObject*)&arg3) ; argList.Add((TObject*)&arg4) ;
argList.Add((TObject*)&arg5) ; argList.Add((TObject*)&arg6) ;
argList.Add((TObject*)&arg7) ; argList.Add((TObject*)&arg8) ;
RooCmdConfig pc(Form("RooDataSet::plotOnXY(%s)",GetName())) ;
pc.defineString("drawOption","DrawOption",0,"P") ;
pc.defineString("histName","Name",0,"") ;
pc.defineInt("lineColor","LineColor",0,-999) ;
pc.defineInt("lineStyle","LineStyle",0,-999) ;
pc.defineInt("lineWidth","LineWidth",0,-999) ;
pc.defineInt("markerColor","MarkerColor",0,-999) ;
pc.defineInt("markerStyle","MarkerStyle",0,8) ;
pc.defineDouble("markerSize","MarkerSize",0,-999) ;
pc.defineInt("fillColor","FillColor",0,-999) ;
pc.defineInt("fillStyle","FillStyle",0,-999) ;
pc.defineInt("histInvisible","Invisible",0,0) ;
pc.defineDouble("scaleFactor","Rescale",0,1.) ;
pc.defineObject("xvar","XVar",0,0) ;
pc.defineObject("yvar","YVar",0,0) ;
pc.process(argList) ;
if (!pc.ok(kTRUE)) {
return frame ;
}
const char* drawOptions = pc.getString("drawOption") ;
Int_t histInvisible = pc.getInt("histInvisible") ;
const char* histName = pc.getString("histName",0,kTRUE) ;
Double_t scaleFactor = pc.getDouble("scaleFactor") ;
RooRealVar* xvar = (RooRealVar*) _vars.find(frame->getPlotVar()->GetName()) ;
RooRealVar* yvar = (RooRealVar*)(pc.getObject("yvar")) ;
if (!_wgtVar && !yvar) {
coutE(InputArguments) << "RooDataSet::plotOnXY(" << GetName() << ") ERROR: no YVar() argument specified and dataset is not weighted" << endl ;
return 0 ;
}
RooRealVar* dataY = yvar ? (RooRealVar*) _vars.find(yvar->GetName()) : 0 ;
if (yvar && !dataY) {
coutE(InputArguments) << "RooDataSet::plotOnXY(" << GetName() << ") ERROR on YVar() argument, dataset does not contain a variable named " << yvar->GetName() << endl ;
return 0 ;
}
RooHist* graph = new RooHist ;
if (histName) {
graph->SetName(histName) ;
} else {
graph->SetName(Form("hxy_%s",GetName())) ;
}
for (int i=0 ; i<numEntries() ; i++) {
get(i) ;
Double_t x = xvar->getVal() ;
Double_t exlo = xvar->getErrorLo() ;
Double_t exhi = xvar->getErrorHi() ;
Double_t y,eylo,eyhi ;
if (!dataY) {
y = weight() ;
weightError(eylo,eyhi) ;
} else {
y = dataY->getVal() ;
eylo = dataY->getErrorLo() ;
eyhi = dataY->getErrorHi() ;
}
graph->addBinWithXYError(x,y,-1*exlo,exhi,-1*eylo,eyhi,scaleFactor) ;
}
Int_t lineColor = pc.getInt("lineColor") ;
Int_t lineStyle = pc.getInt("lineStyle") ;
Int_t lineWidth = pc.getInt("lineWidth") ;
Int_t markerColor = pc.getInt("markerColor") ;
Int_t markerStyle = pc.getInt("markerStyle") ;
Size_t markerSize = pc.getDouble("markerSize") ;
Int_t fillColor = pc.getInt("fillColor") ;
Int_t fillStyle = pc.getInt("fillStyle") ;
if (lineColor!=-999) graph->SetLineColor(lineColor) ;
if (lineStyle!=-999) graph->SetLineStyle(lineStyle) ;
if (lineWidth!=-999) graph->SetLineWidth(lineWidth) ;
if (markerColor!=-999) graph->SetMarkerColor(markerColor) ;
if (markerStyle!=-999) graph->SetMarkerStyle(markerStyle) ;
if (markerSize!=-999) graph->SetMarkerSize(markerSize) ;
if (fillColor!=-999) graph->SetFillColor(fillColor) ;
if (fillStyle!=-999) graph->SetFillStyle(fillStyle) ;
frame->addPlotable(graph,drawOptions,histInvisible) ;
return frame ;
}
RooDataSet *RooDataSet::read(const char *fileList, const RooArgList &varList,
const char *verbOpt, const char* commonPath,
const char* indexCatName) {
RooArgList variables(varList) ;
Bool_t ownIsBlind(kTRUE) ;
RooAbsArg* blindState = variables.find("blindState") ;
if (!blindState) {
blindState = new RooCategory("blindState","Blinding State") ;
variables.add(*blindState) ;
} else {
ownIsBlind = kFALSE ;
if (blindState->IsA()!=RooCategory::Class()) {
oocoutE((TObject*)0,DataHandling) << "RooDataSet::read: ERROR: variable list already contains"
<< "a non-RooCategory blindState member" << endl ;
return 0 ;
}
oocoutW((TObject*)0,DataHandling) << "RooDataSet::read: WARNING: recycling existing "
<< "blindState category in variable list" << endl ;
}
RooCategory* blindCat = (RooCategory*) blindState ;
blindCat->setAttribute("Dynamic") ;
blindCat->defineType("Normal",0) ;
blindCat->defineType("Blind",1) ;
TString opts= verbOpt;
opts.ToLower();
Bool_t verbose= !opts.Contains("q");
Bool_t debug= opts.Contains("d");
RooDataSet *data= new RooDataSet("dataset", fileList, variables);
if (ownIsBlind) { variables.remove(*blindState) ; delete blindState ; }
if(!data) {
oocoutE((TObject*)0,DataHandling) << "RooDataSet::read: unable to create a new dataset"
<< endl;
return 0;
}
blindCat = (RooCategory*) data->_vars.find("blindState") ;
RooCategory *indexCat = 0;
if (indexCatName) {
RooAbsArg* tmp = 0;
tmp = data->_vars.find(indexCatName) ;
if (!tmp) {
oocoutE((TObject*)0,DataHandling) << "RooDataSet::read: no index category named "
<< indexCatName << " in supplied variable list" << endl ;
return 0 ;
}
if (tmp->IsA()!=RooCategory::Class()) {
oocoutE((TObject*)0,DataHandling) << "RooDataSet::read: variable " << indexCatName
<< " is not a RooCategory" << endl ;
return 0 ;
}
indexCat = (RooCategory*)tmp ;
indexCat->setAttribute("Dynamic") ;
}
Int_t outOfRange(0) ;
char fileList2[10240] ;
strlcpy(fileList2,fileList,10240) ;
char *filename = strtok(fileList2,", ") ;
Int_t fileSeqNum(0) ;
while (filename) {
if (indexCat) {
char *catname = strchr(filename,':') ;
if (catname) {
*catname=0 ;
catname++ ;
const RooCatType* type = indexCat->lookupType(catname,kFALSE) ;
if (type) {
indexCat->setIndex(type->getVal()) ;
} else {
indexCat->defineType(catname,fileSeqNum) ;
indexCat->setIndex(fileSeqNum) ;
}
} else {
char newLabel[128] ;
snprintf(newLabel,128,"file%03d",fileSeqNum) ;
if (indexCat->defineType(newLabel,fileSeqNum)) {
oocoutE((TObject*)0,DataHandling) << "RooDataSet::read: Error, cannot register automatic type name " << newLabel
<< " in index category " << indexCat->GetName() << endl ;
return 0 ;
}
indexCat->setIndex(fileSeqNum) ;
}
}
oocoutI((TObject*)0,DataHandling) << "RooDataSet::read: reading file " << filename << endl ;
TString fullName(commonPath) ;
fullName.Append(filename) ;
ifstream file(fullName) ;
if(!file.good()) {
oocoutW((TObject*)0,DataHandling) << "RooDataSet::read: unable to open '"
<< filename << "', skipping" << endl;
}
Int_t line(0) ;
Bool_t haveBlindString(false) ;
while(file.good() && !file.eof()) {
line++;
if(debug) oocxcoutD((TObject*)0,DataHandling) << "reading line " << line << endl;
if (file.peek() == '#')
{
if(debug) oocxcoutD((TObject*)0,DataHandling) << "skipping comment on line " << line << endl;
}
else {
Bool_t readError = variables.readFromStream(file,kTRUE,verbose) ;
data->_vars = variables ;
if(file.eof()) break ;
if(!file.good()) {
oocoutE((TObject*)0,DataHandling) << "RooDataSet::read(static): read error at line " << line << endl ;
break;
}
if (readError) {
outOfRange++ ;
continue ;
}
blindCat->setIndex(haveBlindString) ;
data->fill();
}
}
file.close();
filename = strtok(0," ,") ;
fileSeqNum++ ;
}
if (indexCat) {
RooCategory* origIndexCat = (RooCategory*) variables.find(indexCatName) ;
TIterator* tIter = indexCat->typeIterator() ;
RooCatType* type = 0;
while ((type=(RooCatType*)tIter->Next())) {
origIndexCat->defineType(type->GetName(),type->getVal()) ;
}
}
oocoutI((TObject*)0,DataHandling) << "RooDataSet::read: read " << data->numEntries()
<< " events (ignored " << outOfRange << " out of range events)" << endl;
return data;
}
Bool_t RooDataSet::write(const char* filename)
{
checkInit() ;
ofstream ofs(filename) ;
if (ofs.fail()) {
coutE(DataHandling) << "RooDataSet::write(" << GetName() << ") cannot create file " << filename << endl ;
return kTRUE ;
}
coutI(DataHandling) << "RooDataSet::write(" << GetName() << ") writing ASCII file " << filename << endl ;
Int_t i ;
for (i=0 ; i<numEntries() ; i++) {
RooArgList list(*get(i),"line") ;
list.writeToStream(ofs,kTRUE) ;
}
if (ofs.fail()) {
coutW(DataHandling) << "RooDataSet::write(" << GetName() << "): WARNING error(s) have occured in writing" << endl ;
}
return ofs.fail() ;
}
void RooDataSet::printMultiline(ostream& os, Int_t contents, Bool_t verbose, TString indent) const
{
checkInit() ;
RooAbsData::printMultiline(os,contents,verbose,indent) ;
if (_wgtVar) {
os << indent << " Dataset variable \"" << _wgtVar->GetName() << "\" is interpreted as the event weight" << endl ;
}
}
void RooDataSet::printValue(ostream& os) const
{
os << numEntries() << " entries" ;
if (isWeighted()) {
os << " (" << sumEntries() << " weighted)" ;
}
}
void RooDataSet::printArgs(ostream& os) const
{
os << "[" ;
TIterator* iter = _varsNoWgt.createIterator() ;
RooAbsArg* arg ;
Bool_t first(kTRUE) ;
while((arg=(RooAbsArg*)iter->Next())) {
if (first) {
first=kFALSE ;
} else {
os << "," ;
}
os << arg->GetName() ;
}
if (_wgtVar) {
os << ",weight:" << _wgtVar->GetName() ;
}
os << "]" ;
delete iter ;
}
void RooDataSet::SetName(const char *name)
{
if (_dir) _dir->GetList()->Remove(this);
TNamed::SetName(name) ;
if (_dir) _dir->GetList()->Add(this);
}
void RooDataSet::SetNameTitle(const char *name, const char* title)
{
if (_dir) _dir->GetList()->Remove(this);
TNamed::SetNameTitle(name,title) ;
if (_dir) _dir->GetList()->Add(this);
}
void RooDataSet::Streamer(TBuffer &R__b)
{
if (R__b.IsReading()) {
UInt_t R__s, R__c;
Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
if (R__v>1) {
R__b.ReadClassBuffer(RooDataSet::Class(),this,R__v,R__s,R__c);
} else {
UInt_t R__s1, R__c1;
Version_t R__v1 = R__b.ReadVersion(&R__s1, &R__c1); if (R__v1) { }
RooAbsData::Streamer(R__b);
TTree* X_tree(0) ; R__b >> X_tree;
RooArgSet X_truth ; X_truth.Streamer(R__b);
TString X_blindString ; X_blindString.Streamer(R__b);
R__b.CheckByteCount(R__s1, R__c1, RooTreeData::Class());
_dstore = new RooTreeDataStore(X_tree,_vars) ;
_dstore->SetName(GetName()) ;
_dstore->SetTitle(GetTitle()) ;
_dstore->checkInit() ;
RooDirItem::Streamer(R__b);
_varsNoWgt.Streamer(R__b);
R__b >> _wgtVar;
R__b.CheckByteCount(R__s, R__c, RooDataSet::IsA());
}
} else {
R__b.WriteClassBuffer(RooDataSet::Class(),this);
}
}