00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00014
00015
00016
00017
00018
00019
00020 #include "RooRarFit/rarVersion.hh"
00021
00022 #include "Riostream.h"
00023
00024 #include "RooFitCore/Roo1DTable.hh"
00025 #include "RooFitCore/RooAbsPdf.hh"
00026 #include "RooFitCore/RooArgList.hh"
00027 #include "RooFitCore/RooDataSet.hh"
00028 #include "RooFitCore/RooFitResult.hh"
00029 #include "RooFitCore/RooMCStudy.hh"
00030 #include "RooFitCore/RooProdPdf.hh"
00031 #include "RooFitCore/RooRealVar.hh"
00032 #include "RooFitCore/RooStringVar.hh"
00033
00034 #include "RooFitCore/RooAddPdf.hh"
00035
00036 #include "RooRarFit/rarMLFitter.hh"
00037 #include "RooRarFit/rarMLPdf.hh"
00038
00039 ClassImp(rarMLPdf)
00040 ;
00041
00045 rarMLPdf::rarMLPdf()
00046 : rarAdd(),
00047 _specialStr("")
00048 {
00049 init();
00050 }
00051
00064 rarMLPdf::rarMLPdf(const char *configFile, const char *configSec,
00065 const char *configStr,
00066 rarDatasets *theDatasets, RooDataSet *theData,
00067 const char *name, const char *title)
00068 : rarAdd(configFile, configSec, configStr,
00069 theDatasets, theData, name, title, kFALSE, kFALSE),
00070 _specialStr("")
00071 {
00072 init();
00073 }
00074
00075 rarMLPdf::~rarMLPdf()
00076 {
00077 }
00078
00104 void rarMLPdf::init()
00105 {
00106
00107 if (_nCoeff!=_nComp) {
00108 cout<<"Pdf model should be extended pdf"<<endl;
00109 exit(-1);
00110 }
00111
00112 for (Int_t i=0; i<=_nComp; i++) _compCat.defineType(Form("%02d",i));
00113
00114 _sCoeffs.add(_coeffs);
00115
00116 TString masterSec=getMasterSec();
00117 Bool_t simFit=getControlBit("SimFit")&&
00118 ("simple"!=readConfStr("simultaneousFit", "yes",masterSec));
00119
00120 if (simFit) {
00121 createAbsVars("splitSpecials", &_specialSet);
00122
00123 TString myVarSec=getVarSec();
00124 setVarSec(masterSec);
00125 createAbsVars("splitSpecials", &_specialSet);
00126 setVarSec(myVarSec);
00127 }
00128 TString yieldSplitMethod=readConfStr("yieldSplitMethod", "auto", masterSec);
00129 if (simFit && ("manual"!=yieldSplitMethod)) {
00130
00131 TString myVarSec=getVarSec();
00132 setVarSec(masterSec);
00133
00134 RooArgSet coeffParamSet("Coeff Parameter Set");
00135 for (Int_t i=0; i<_nCoeff; i++) {
00136 coeffParamSet.add(*(_coeffs[i].getParameters(*_fullObs)));
00137 }
00138 RooArgList coeffParamList(coeffParamSet);
00139
00140
00141 TString pdfSplitStr=readConfStr(Form("the_%s", GetName()), "", masterSec);
00142 if ("semi"==yieldSplitMethod) {
00143 for (Int_t i=0; i<_coeffs.getSize(); i++) {
00144 if (pdfSplitStr.Contains(_coeffs[i].GetName())) {
00145 cout<<"Please do not include "<<_coeffs[i].GetName()
00146 <<" or its dependents in your simPdfBuilder configs"<<endl
00147 <<"in section \""<<masterSec<<"\":"<<endl
00148 <<pdfSplitStr<<endl
00149 <<"We will split it for you."<<endl;
00150 exit(-1);
00151 }
00152 }
00153 for (Int_t i=0; i<coeffParamList.getSize(); i++) {
00154 if (pdfSplitStr.Contains(coeffParamList[i].GetName())) {
00155 cout<<"Please do not include "<<coeffParamList[i].GetName()
00156 <<" or its dependents in your simPdfBuilder configs"<<endl
00157 <<"in section \""<<masterSec<<"\":"<<endl
00158 <<pdfSplitStr<<endl
00159 <<"We will split it for you."<<endl;
00160 exit(-1);
00161 }
00162 }
00163 }
00164
00165 TString physCat=getFitter()->getPhysCat();
00166 TString splitCats=getFitter()->getSplitCats();
00167
00168 _coeffs.removeAll();
00169 for (Int_t i=0; i<_nCoeff; i++) {
00170
00171 TString coeffArgsStr=_sCoeffs[i].GetName();
00172 TString formulaStr="@0";
00173
00174 TString fracRuleStr=readConfStr("fracRule", splitCats, masterSec);
00175 fracRuleStr=readConfStr("fracRule_"+coeffArgsStr,fracRuleStr, masterSec);
00176 rarStrParser catsStrParser=fracRuleStr;
00177 Int_t nSCat=catsStrParser.nArgs();
00178
00179 RooArgSet splitCatSet(getFitter()->getSplitCatSet());
00180 for (Int_t j=0; j<nSCat; j++) {
00181 TString catName=catsStrParser[j];
00182 RooAbsCategory *theCat=getFitter()->getSplitCat(splitCatSet, catName);
00183 if (!theCat) {
00184 cout<<"Can not find cat "<<catName<<endl
00185 <<"Please make sure the cat you are using exists"<<endl;
00186 exit(-1);
00187 }
00188
00189 catName=theCat->GetName();
00190
00191 TString catCoeffName=Form("Frac_%s_%s",
00192 _sCoeffs[i].GetName(), catName.Data());
00193 TString catCoeffStr=readConfStr(catCoeffName, "notSet", masterSec);
00194 if ("notSet"==catCoeffStr)
00195 catCoeffStr=Form("%s %s RooRealVar \"cat coeff\" C 1. 0. 1.",
00196 catCoeffName.Data(), catCoeffName.Data());
00197 else
00198 catCoeffStr=catCoeffName+" "+catCoeffStr;
00199 RooRealVar *catCoeff=(RooRealVar*)createAbsVar(catCoeffStr);
00200 catCoeffName=catCoeff->GetName();
00201
00202 saveFracName(catCoeffName);
00203
00204 coeffArgsStr+=Form(" %s ", catCoeff->GetName());
00205 formulaStr+=Form("*@%d",j+1);
00206
00207 if ("RooRealVar"!=TString(catCoeff->ClassName())) {
00208 cout<<catCoeffName<<" is created in config file as "
00209 <<catCoeff->ClassName()<<","<<endl
00210 <<"but it should be RooRealVar"<<endl;
00211 exit(-1);
00212 }
00213
00214
00215 TString catTypesStr="";
00216 TIterator* catTypeIter = theCat->typeIterator();
00217 RooCatType *theType(0);
00218 while(theType=(RooCatType*)catTypeIter->Next()) {
00219 catTypesStr+=Form(" %s ", theType->GetName());
00220 }
00221 delete catTypeIter;
00222
00223 rarStrParser catTypesStrParser=catTypesStr;
00224 if (catTypesStrParser.nArgs()<1) continue;
00225 TString cat1FormulaStr="1.";
00226 TString cat1ArgSet="";
00227 Roo1DTable* theTable(0);
00228 if (_theData) theTable = _theData->table(*theCat);
00229
00230 Bool_t useFracSrc=kFALSE;
00231 TString fracSrcStr=
00232 readConfStrCnA(Form("fracSrc_%s_%s", _sCoeffs[i].GetName(),
00233 catName.Data()),"notSet");
00234 if ("notSet"==fracSrcStr)
00235 fracSrcStr=
00236 readConfStrCnA(Form("fracSrc_%s",_sCoeffs[i].GetName()), "notSet");
00237 if ("notSet"==fracSrcStr)
00238 fracSrcStr=readConfStrCnA("fracSrc", "notSet");
00239 if ("notSet"!=fracSrcStr) {
00240 RooDataSet *theData=getDatasets()->getData(fracSrcStr);
00241 if (theData) {
00242 theTable = theData->table(*theCat);
00243 useFracSrc=kTRUE;
00244 cout<<" Using table from "<<theData->GetName()
00245 <<" for "<<_sCoeffs[i].GetName()<<" "<<catName<<endl;
00246 theTable->Print();
00247 }
00248 }
00249
00250 Int_t nonIdx=0;
00251
00252 if ("auto"==yieldSplitMethod) {
00253 Bool_t notFound=(catTypesStrParser.nArgs()>1);
00254 for (Int_t k=0; k<catTypesStrParser.nArgs(); k++) {
00255 TString myNonFreeName=catCoeffName+"["+catTypesStrParser[k]+"]";
00256 Int_t strIdx=pdfSplitStr.Index(myNonFreeName);
00257 if (strIdx<0) continue;
00258
00259 TString subpdfSplitStr=pdfSplitStr(0,strIdx);
00260 Int_t colonIdx=subpdfSplitStr.Last(':');
00261 if (colonIdx<=0) break;
00262 rarStrParser pdfSplitStrParser=TString(subpdfSplitStr(0,colonIdx));
00263
00264
00265 TString splitCatName=
00266 pdfSplitStrParser[pdfSplitStrParser.nArgs()-1];
00267 splitCatName.ReplaceAll(",", "_");
00268
00269 if (splitCatName==catName) {
00270 nonIdx=k;
00271 notFound=kFALSE;
00272 }
00273 break;
00274 }
00275 if (notFound) {
00276 cout<<" Can not find splitting specif. for"<<endl
00277 <<" "<<catCoeffName<<" wrt types:"<<endl
00278 <<" "<<catTypesStr<<endl
00279 <<" in the_"<<GetName()<<endl<<pdfSplitStr<<endl
00280 <<" from section ["<<getVarSec()<<"]"<<endl;
00281 exit(-1);
00282 }
00283 } else {
00284 TString nonFreeStr=readConfStr("nonFreeCatTypes", "", getVarSec());
00285 for (Int_t k=0; k<catTypesStrParser.nArgs(); k++) {
00286 if (nonFreeStr.
00287 Contains(catCoeffName+"["+catTypesStrParser[k]+"]")) {
00288 nonIdx=k;
00289 break;
00290 }
00291 }
00292 }
00293 for (Int_t k=0; k<catTypesStrParser.nArgs(); k++) {
00294
00295 const RooCatType *theType=theCat->lookupType(catTypesStrParser[k]);
00296 if (!theType) {
00297 cout <<"The cat type "<<catTypesStrParser[k]
00298 <<" does not exist in cat "<<theCat->GetName()<<endl;
00299 exit(-1);
00300 }
00301 if (k==nonIdx) continue;
00302 TString catCoeffTypeName=catCoeffName+"_"+catTypesStrParser[k]+"";
00303 Double_t typeFrac=1./catTypesStrParser.nArgs();
00304 if (theTable) typeFrac=theTable->getFrac(catTypesStrParser[k]);
00305 RooAbsReal *catCoeffType=(RooAbsReal*)
00306 createAbsVar(Form("%s %s RooRealVar \"cat type coeff\" C %f 0 1",
00307 catCoeffTypeName.Data(), catCoeffTypeName.Data(),
00308 typeFrac));
00309
00310 saveFracName(catCoeffTypeName);
00311 if (useFracSrc)
00312 addToConfStr("Ignored", catCoeffType->GetName(), myVarSec);
00313 _specialSet.add(*catCoeffType);
00314 if (2==catTypesStrParser.nArgs()) _asymSet.add(*catCoeffType);
00315 Int_t fvIdx = k<nonIdx ? k : k-1;
00316 cat1FormulaStr+=Form("-@%d", fvIdx);
00317 cat1ArgSet+=Form(" %s ", catCoeffType->GetName());
00318 }
00319
00320 TString catCoeffTypeName=catCoeffName+"_"+catTypesStrParser[nonIdx]+"";
00321
00322 RooAbsReal *catCoeffType=(RooAbsReal*)
00323 createAbsVar(Form("%s %s RooFormulaVar %s %s",
00324 catCoeffTypeName.Data(), catCoeffTypeName.Data(),
00325 cat1FormulaStr.Data(), cat1ArgSet.Data()));
00326
00327 if ("RooFormulaVar"!=TString(catCoeffType->ClassName())) {
00328 cout<<catCoeffTypeName<<" is created in config file as "
00329 <<catCoeffType->ClassName()<<","<<endl
00330 <<"but it should be RooFormulaVar"<<endl;
00331 exit(-1);
00332 }
00333 _specialSet.add(*catCoeffType);
00334 if (catTypesStrParser.nArgs()>1) _cat1Set.add(*catCoeffType);
00335 if ("semi"==yieldSplitMethod) {
00336 _specialStr+=Form(" %s : %s ", catName.Data(), catCoeffName.Data());
00337 }
00338 }
00339
00340 if (splitCatSet.getSize()>0) {
00341 cout<<" No splitting rules for "<<_sCoeffs[i].GetName()<<" wrt:"<<endl;
00342 splitCatSet.Print();
00343 cout<<" Please check config fracRule or fracRule_"
00344 <<_sCoeffs[i].GetName()<<" in section \""<<masterSec<<"\":"<<endl;
00345 exit(-1);
00346 }
00347 TString coeffName=Form("theSim_%s", _sCoeffs[i].GetName());
00348
00349
00350
00351 RooAbsReal *coeff=(RooAbsReal*)
00352 createAbsVar(Form("%s %s RooFormulaVar %s %s",
00353 coeffName.Data(), coeffName.Data(),
00354 formulaStr.Data(), coeffArgsStr.Data()));
00355
00356 _coeffs.add(*coeff);
00357 }
00358
00359
00360 setVarSec(myVarSec);
00361 }
00362
00363 _thePdf=new RooAddPdf(Form("the_%s", GetName()), _pdfType+" "+GetTitle(),
00364 _subPdfs, _coeffs);
00365
00366 cout<<"done init of rarMLPdf for "<<GetName()<<endl<<endl;
00367 }
00368
00375 RooArgSet rarMLPdf::getSpecialSet(TString setName)
00376 {
00377 if ("cat1Set"==setName) return _cat1Set;
00378 if ("asymSet"==setName) return _asymSet;
00379 return _specialSet;
00380 }
00381
00386 RooAbsPdf *rarMLPdf::getProtGen()
00387 {
00388 if (_theProtGen) return _theProtGen;
00389
00390 rarCompBase::getProtGen();
00391
00392 RooArgList compList;
00393 for (Int_t i=0; i<_nComp; i++) {
00394 if (_protGenPdfs.getSize()>0) {
00395 RooArgList pdfList(_protGenPdfs);
00396 pdfList.add(_subProtGenPdfs[i]);
00397 TString compName=
00398 Form("prodProtGen_%s_%s",GetName(),_subPdfs[i].GetName());
00399 TString compTitle=
00400 Form("Prod Prot Gen %s %s",GetName(),_subPdfs[i].GetName());
00401 RooAbsPdf *thePdf=new RooProdPdf (compName, compTitle, pdfList);
00402 compList.add(*thePdf);
00403 } else compList.add(_subProtGenPdfs[i]);
00404 }
00405 _theProtGen=new RooAddPdf
00406 (Form("gen_%s", GetName()), Form("Gen Add for %s", GetName()),
00407 compList, _coeffs);
00408
00409 cout<<"getProtGen: Expected number of events "
00410 <<_theProtGen->expectedEvents(0)<<endl;
00411
00412 cout<<"protGen created for "<<GetName()<<endl;
00413 _theProtGen->Print();
00414
00415 return _theProtGen;
00416 }
00417
00428 RooPlot *rarMLPdf::doPdfPlot(TList &plotList, TString pdfList)
00429 {
00430 return rarCompBase::doPdfPlot(plotList, pdfList);
00431 }