rarDatasets.cc

Go to the documentation of this file.
00001 /*****************************************************************************
00002 * Project: BaBar detector at the SLAC PEP-II B-factory
00003 * Package: RooRarFit
00004  *    File: $Id: rarDatasets.cc,v 1.26 2011/07/31 06:06:39 yury Exp $
00005  * Authors: Lei Zhang
00006  * History:
00007  * 
00008  * Copyright (C) 2005-2012, University of California, Riverside
00009  *****************************************************************************/
00010 
00011 // -- CLASS DESCRIPTION [RooRarFit] --
00012 // This class provides dataset class for RooRarFit
00014 //
00015 // BEGIN_HTML
00016 // This class provides dataset class for RooRarFit
00017 // END_HTML
00018 //
00019 
00020 #include "RooRarFit/rarVersion.hh"
00021 
00022 #include <stdlib.h>
00023 #include <iostream>
00024 #include <fstream>
00025 #include "Riostream.h"
00026 #include <sstream>
00027 
00028 #include "TMD5.h"
00029 
00030 #include "RooFitCore/Roo1DTable.hh"
00031 #include "RooFitCore/RooArgList.hh"
00032 #include "RooFitCore/RooDataSet.hh"
00033 #include "RooFitCore/RooRealVar.hh"
00034 #include "RooFitCore/RooStringVar.hh"
00035 
00036 #include "RooRarFit/rarDatasets.hh"
00037 
00038 ClassImp(rarDatasets)
00039   ;
00040 
00044 rarDatasets::rarDatasets()
00045   : rarConfig(),
00046     _dsd(0), _fullFObs(0)
00047 {
00048   init();
00049 }
00050 
00061 rarDatasets::rarDatasets(const char *configFile, const char *configSec,
00062                          const char *actionSec)
00063   : rarConfig(configFile, configSec, "null", "Datasets", "Datasets"),
00064     _actionSec(actionSec), _dsd(0), _fullFObs(0)
00065 {
00066   init();
00067 }
00068 
00069 rarDatasets::~rarDatasets()
00070 {
00071   // _dataSets.Delete();
00072 }
00073 
00083 void rarDatasets::init()
00084 {
00085   cout<<"init of rarDatasets:"<<endl;
00086   // first set #_createFundamental and #_fullNameSchema
00087   _createFundamental=kTRUE;
00088   _fullNameSchema="self";
00089 
00090   // first create dsd object
00091   TString dataDefSec=readConfStr("dsdSec", "Dataset Definition");
00092   _dsd=new rarDatasetDef(_configFile, dataDefSec);
00093   _fullObs=_dsd->getFullObs();
00094   // create full fundamental obs
00095   _fullFObs=new RooArgSet("theFullFObs");
00096   RooArgList fullObs(*_fullObs);
00097   for (Int_t i=0; i<fullObs.getSize(); i++) {
00098     RooAbsArg *theVar=fullObs.at(i);
00099     _fullFObs->add(*((RooAbsArg*)_rarVars.FindObject(theVar->GetName())));
00100   }
00101   
00102   // get number of datasets, first check action section
00103   TString datasetsStr=readConfStr("Datasets", "notSet", _actionSec);
00104   // if not found, go to dsi section
00105   if ("notSet"==datasetsStr) datasetsStr=readConfStr("Datasets", "");
00106   rarStrParser datasetsStrParser=datasetsStr;
00107   Int_t nDataset=datasetsStrParser.nArgs();
00108   if (nDataset<=0) {
00109     cout<<"no datasets defined in RooStringVar \"Datasets\" in section \""
00110         <<_configSec<<"\""<<endl;
00111     exit(-1);
00112   }
00113   // read in datasets info
00114   RooArgSet datasetList("Dataset List");
00115   for (Int_t i=0; i<nDataset; i++) {
00116     RooStringVar *dataset=new RooStringVar
00117       (datasetsStrParser[i], datasetsStrParser[i], "notSet", 8192);
00118     datasetList.addOwned(*dataset);
00119   }
00120   datasetList.readFromFile(_configFile, 0, _configSec);
00121   //datasetList.Print("v");
00122   // list configed datasets
00123   cout<<endl
00124       <<"The datasets defined in config file:"<<endl;
00125   for (Int_t i=0; i<nDataset; i++) {
00126     TString datasetStr=(RooStringVar&)datasetList[datasetsStrParser[i]];
00127     cout<<Form(" dataset%02d ",i)<<datasetsStrParser[i]<<" "<<datasetStr<<endl;
00128   }
00129   cout<<endl;
00130   // now read in the datasets
00131   for (Int_t i=0; i<nDataset; i++) {
00132     Bool_t isUB=kFALSE;
00133     TString datasetStr=(RooStringVar&)datasetList[datasetsStrParser[i]];
00134     // get name of weight variable
00135     TString wgtVarName = getWeightVarName(getDSName(datasetsStrParser[i]));
00136 
00137     RooDataSet *data=createDataSet(datasetsStrParser[i]+" "+datasetStr, isUB, wgtVarName);
00138     data->SetName(getDSName(datasetsStrParser[i]));
00139     _dataSets.Add(data);
00140     if (isUB) ubStr(getDSName(datasetsStrParser[i]), "Unblinded");
00141     //data->Print("v");
00142     //data->get()->Print("v");
00143   }
00144 
00145   cout<<endl<<"Datasets read in:"<<endl;
00146   _dataSets.Print();
00147   cout<<endl;
00148   
00149   // do we need to tabulate it?
00150   tabulateDatasets();
00151 }
00152 
00156 TString rarDatasets::getWeightVarName(TString datasetName)
00157 {
00158   TString swvStr = readConfStr("setWeightVar", "no");
00159   if (swvStr.BeginsWith("no")) return(""); // no weight variable
00160   
00161   // do we have a default weight variable for all datasets?
00162   rarStrParser swvStrParser = swvStr;
00163   TString swvName = swvStrParser[0]; // either dataset name or default weight variable
00164   // check variable exists
00165   if (_fullFObs->find(swvName)) {
00166     cout << "Will use " << swvName << " as weight for dataset " << datasetName << endl;
00167   } else {
00168     cout << "Can not find " << swvName << " as a variable to use as a weight." << endl;
00169     swvName="";
00170   }
00171   return(swvName);
00172 }
00173 
00178 void rarDatasets::tabulateDatasets(const char *dsName)
00179 {
00180   TString tdsStr=readConfStr("tabulateDatasets", "no");
00181   if (tdsStr.BeginsWith("no")) return;
00182   
00183   cout << "Tabulate Datsets by Category\n" << endl;
00184   RooArgList catList(*getCats());
00185   for (Int_t dIdx=0; dIdx<_dataSets.GetSize(); dIdx++) {
00186     RooDataSet *data=(RooDataSet*) _dataSets.At(dIdx);
00187     if (dsName&&TString(dsName)!=data->GetName()) continue;
00188     for (Int_t i=0; i<catList.getSize(); i++) {
00189       RooAbsCategory *theCat=(RooAbsCategory *)&(catList[i]);
00190       Roo1DTable *theTable(0);
00191       if (theCat&&(theTable=data->table(*theCat))) {
00192         theTable->Print("v");
00193         // get frac table
00194         TIterator* catTypeIter = theCat->typeIterator();
00195         RooCatType *theType(0);
00196         while(theType=(RooCatType*)catTypeIter->Next()) {
00197           TString typeName=theType->GetName();
00198           cout<<"    "<<typeName<<"\t"<<theTable->getFrac(typeName)<<endl;
00199         }
00200         cout<<endl;
00201         delete catTypeIter;
00202       }
00203     }
00204   }
00205 }
00206 
00210 TString rarDatasets::getDSName(TString name)
00211 {
00212   // first remove "
00213   name.ReplaceAll("\"", "");
00214   name.ReplaceAll("  ", " ");
00215   name.ReplaceAll(">=", ".GE.");
00216   name.ReplaceAll(">", ".GT.");
00217   name.ReplaceAll("<=", ".LE.");
00218   name.ReplaceAll("<", ".LT.");
00219   name.ReplaceAll("==", ".EQ.");
00220   name.ReplaceAll("!=", ".NEQ.");
00221   name.ReplaceAll("=", ".EQ.");
00222   name.ReplaceAll("&&", ".AND.");
00223   name.ReplaceAll("||", ".OR.");
00224   for (Int_t i=0; i<name.Length(); i++)
00225     if (!isalnum(name[i])) name[i]='_';
00226   
00227   return name;
00228 }
00229 
00241 RooDataSet *rarDatasets::getData(const char *name)
00242 {
00243   RooDataSet *theData=(RooDataSet*)_dataSets.FindObject(getDSName(name));
00244   if (theData) return theData;
00245   // can not find the data set parser the name
00246   rarStrParser nameParser=name;
00247   if (nameParser.nArgs()<=0) return theData;
00248   theData=(RooDataSet*)_dataSets.FindObject(getDSName(nameParser[0]));
00249   if (!theData) return theData; //can not find any
00250   // reduce the dataset
00251   theData=(RooDataSet*)theData->reduce(nameParser[1]);
00252   theData->SetName(getDSName(name));
00253   _dataSets.Add(theData);
00254   // check if need to set ub bit
00255   if (!isBlind(getDSName(nameParser[0]))) {
00256     ubStr(getDSName(name), "Unblinded");
00257   }
00258   cout<<" New dataset created"<<endl;
00259   if (theData) {
00260     //theData->Print("v");
00261     theData->Print();
00262     tabulateDatasets(theData->GetName());
00263   }
00264   return theData;
00265 }
00266 
00271 TString rarDatasets::ubStr(TString dsName, const char *ubStrVal)
00272 {
00273   TString ubStrName="ub_"+dsName;
00274   // have we calculated it?
00275   RooStringVar *theStr=(RooStringVar*)_UBs.find(ubStrName);
00276   if (theStr&&("notSet"!=TString(theStr->getVal()))) {
00277     if (ubStrVal) theStr->setVal(ubStrVal);
00278     return theStr->getVal();
00279   }
00280   if (!theStr) {
00281     theStr=new RooStringVar(ubStrName, ubStrName, "notSet", 8192);
00282     _UBs.add(*theStr);
00283   }
00284   if (ubStrVal) {
00285     theStr->setVal(ubStrVal);
00286     return theStr->getVal();
00287   }
00288   // compute chksum
00289   TMD5 chksum;
00290   stringstream o;
00291   // read in all the data entries
00292   RooDataSet *theData=getData(dsName);
00293   if (!theData) {
00294     cout<<" W A R N I N G !"<<endl
00295         <<" Can not find dataset named "<<dsName<<" for ub calculation!"<<endl;
00296     return theStr->getVal();
00297   }
00298   Int_t nEvt=theData->numEntries();
00299   Int_t nStep=nEvt/10000;
00300   nStep++;
00301   Int_t i=0;
00302   while (i<nEvt) {
00303     RooArgList evt(*theData->get(i));
00304     evt.writeToStream((ostream&)o, kTRUE);
00305     i+=nStep;
00306   }
00307   string chkStr=o.str();
00308   chksum.Update((UChar_t*)chkStr.c_str(), chkStr.length());
00309   chksum.Final();
00310   theStr->setVal(chksum.AsString());
00311   
00312   return theStr->getVal();
00313 }
00314 
00318 Bool_t rarDatasets::isBlind(TString dsName)
00319 {
00320   // magic override
00321   if (getenv("RARFITUNBLIND")) return kFALSE;
00322 
00323   // rest of the routine
00324   Bool_t retVal(kTRUE);
00325   TString ubStrVal=ubStr(dsName);
00326   if ("notSet"==ubStrVal) {
00327     cout<<" W A R N I N G !"<<endl
00328         <<" Somehow the ub status for "<<dsName<<" is "<<ubStrVal<<endl
00329         <<" So "<<dsName<<" remains blind. Please double check"<<endl;
00330     return retVal;
00331   }
00332   if ("Unblinded"==ubStrVal) return kFALSE;
00333   rarStrParser ubIDParser=ubStrVal;
00334   rarStrParser dsiUBStrParser=readConfStr("ub_"+dsName, "");
00335   while(dsiUBStrParser.nArgs()>0) {
00336     TString dsiUBStr=dsiUBStrParser[0];
00337     dsiUBStrParser.Remove();
00338     if (ubIDParser.Have(dsiUBStr)) return kFALSE;
00339   }
00340   
00341   return retVal;
00342 }

Generated on 30 Oct 2013 for RooRarFit by  doxygen 1.4.7