// Roo1DTable implements a one-dimensional table. A table is the category
// equivalent of a plot. To create a table use the RooDataSet::table method.
// END_HTML
#include "RooFit.h"
#include "Riostream.h"
#include <iomanip>
#include "TString.h"
#include "TMath.h"
#include "Roo1DTable.h"
#include "RooMsgService.h"
#include "TClass.h"
using namespace std ;
ClassImp(Roo1DTable)
Roo1DTable::Roo1DTable(const char *name, const char *title, const RooAbsCategory& cat) : 
  RooTable(name,title), _total(0), _nOverflow(0)
{
  
  
  
  
  
  Int_t nbin=0 ;
  TIterator* tIter = cat.typeIterator() ;
  RooCatType* type ;
  while (((type = (RooCatType*)tIter->Next()))) {
    _types.Add(new RooCatType(*type)) ;
    nbin++ ;
  }
  delete tIter ;
  
  _count.resize(nbin) ;
  for (int i=0 ; i<nbin ; i++) _count[i] = 0 ;
}
Roo1DTable::Roo1DTable(const Roo1DTable& other) : 
  RooTable(other), _count(other._count), _total(other._total), _nOverflow(other._nOverflow)
{  
  
  
  int i;
  for (i=0 ; i<other._types.GetEntries() ; i++) {
    _types.Add(new RooCatType(*(RooCatType*)other._types.At(i))) ;
  }
}
Roo1DTable::~Roo1DTable()
{
  
  
  _types.Delete() ;
}
void Roo1DTable::fill(RooAbsCategory& cat, Double_t weight) 
{
  
  
  
  
  if (weight==0) return ;
  _total += weight ;
  
  for (int i=0 ; i<_types.GetEntries() ; i++) {
    RooCatType* entry = (RooCatType*) _types.At(i) ;
    if (cat.getIndex()==entry->getVal()) {
      _count[i] += weight ; ;
      
      return;
    }
  }  
  
  _nOverflow += weight ;
  
}
void Roo1DTable::printName(ostream& os) const 
{
  
  os << GetName() ;
}
void Roo1DTable::printTitle(ostream& os) const 
{
  
  os << GetTitle() ;
}
void Roo1DTable::printClassName(ostream& os) const 
{
  
  os << IsA()->GetName() ;
}
void Roo1DTable::printValue(ostream& os) const 
{
  
  os << "(" ;
  for (Int_t i=0 ; i<_types.GetEntries() ; i++) {
    RooCatType* entry = (RooCatType*) _types.At(i) ;
    if (_count[i]>0) {
      if (i>0) {
	os << "," ;
      }
      os << entry->GetName() << "=" << _count[i] ;
    }
  }
  os << ")" ;
}
void Roo1DTable::printMultiline(ostream& os, Int_t , Bool_t verbose, TString indent) const 
{
  
  
  os << indent << endl ;
  os << indent << "  Table " << GetName() << " : " << GetTitle() << endl ;
  
  Int_t labelWidth(0) ;
  Double_t maxCount(1) ;
  int i;
  for (i=0 ; i<_types.GetEntries() ; i++) {
    RooCatType* entry = (RooCatType*) _types.At(i) ;
    
    
    Int_t lwidth = strlen(entry->GetName());
    labelWidth = lwidth > labelWidth ? lwidth : labelWidth;
    maxCount=_count[i]>maxCount?_count[i]:maxCount ;
  }
  
  if (_nOverflow>0) {
    labelWidth=labelWidth>8?labelWidth:8 ;
    maxCount=maxCount>_nOverflow?maxCount:_nOverflow ;
  }
  
  Int_t countWidth=((Int_t)log10(maxCount))+1 ;
  os << indent << "  +-" << setw(labelWidth) << setfill('-') << "-" << "-+-" << setw(countWidth) << "-" << "-+" << endl ;
  os << setfill(' ') ;
  
  for (i=0 ; i<_types.GetEntries() ; i++) {
    RooCatType* entry = (RooCatType*) _types.At(i) ;
    if (_count[i]>0 || verbose) {
      os << "  | " << setw(labelWidth) << entry->GetName() << " | " << setw(countWidth) << _count[i] << " |" << endl ;
    }
  }
  
  if (_nOverflow) {
    os << indent << "  +-" << setw(labelWidth) << setfill('-') << "-" << "-+-" << setw(countWidth) << "-" << "-+" << endl ;
    os << indent << "  | " << "Overflow" << " | " << setw(countWidth) << _nOverflow << " |" << endl ;    
  }
  
  os << indent << "  +-" << setw(labelWidth) << setfill('-') << "-" << "-+-" << setw(countWidth) << "-" << "-+" << endl ;
  os << setfill(' ') ;
  os << indent << endl ;
}
Double_t Roo1DTable::get(const char* label, Bool_t silent) const 
{
  
  
  TObject* cat = _types.FindObject(label) ;
  if (!cat) {
    if (!silent) {
      coutE(InputArguments) << "Roo1DTable::get: ERROR: no such entry: " << label << endl ;
    }
    return 0 ;
  }
  return _count[_types.IndexOf(cat)] ;
}
Double_t Roo1DTable::getOverflow() const 
{
  
  return _nOverflow ;
}
Double_t Roo1DTable::getFrac(const char* label, Bool_t silent) const 
{
  
  
  
  if (_total) {
    return get(label,silent) / _total ;
  } else {
    if (!silent) coutW(Contents) << "Roo1DTable::getFrac: WARNING table empty, returning 0" << endl ;
    return 0. ;
  }
}
Bool_t Roo1DTable::isIdentical(const RooTable& other) 
{
  
  const Roo1DTable* other1d = &dynamic_cast<const Roo1DTable&>(other) ;
  if (!other1d) {
    return kFALSE ;
  }
  int i;
  for (i=0 ; i<_types.GetEntries() ; i++) {
    
    if (_count[i] != other1d->_count[i]) {
      return kFALSE ;
    }
  }
  return kTRUE ;
}
Last change: Wed Jun 25 08:31:23 2008
Last generated: 2008-06-25 08:31
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.