39 ClassImp(RooStats::HLFactory); ;
42 using namespace RooStats;
43 using namespace RooFit;
50 HLFactory::HLFactory(
const char *name,
58 fCombinationDone(false),
64 fWs =
new RooWorkspace(wsName,
true);
66 fSigBkgPdfNames.SetOwner();
67 fBkgPdfNames.SetOwner();
68 fDatasetsNames.SetOwner();
77 HLFactory::HLFactory(
const char* name,
78 RooWorkspace* externalWs,
85 fCombinationDone(false),
90 fSigBkgPdfNames.SetOwner();
91 fBkgPdfNames.SetOwner();
92 fDatasetsNames.SetOwner();
98 HLFactory::HLFactory():
99 TNamed(
"hlfactory",
"hlfactory"),
104 fCombinationDone(false),
108 fWs =
new RooWorkspace(
"hlfactory_ws",
true);
110 fSigBkgPdfNames.SetOwner();
111 fBkgPdfNames.SetOwner();
112 fDatasetsNames.SetOwner();
119 HLFactory::~HLFactory(){
120 if (fComboSigBkgPdf!=NULL)
121 delete fComboSigBkgPdf;
122 if (fComboBkgPdf!=NULL)
124 if (fComboDataset!=NULL)
125 delete fComboDataset;
141 int HLFactory::AddChannel(
const char* label,
142 const char* SigBkgPdfName,
143 const char* BkgPdfName,
144 const char* DatasetName){
145 if (fCombinationDone){
146 std::cerr <<
"Cannot add anymore channels. "
147 <<
"Combination already carried out.\n";
151 if (SigBkgPdfName!=0){
152 if (fWs->pdf(SigBkgPdfName)==NULL){
153 std::cerr <<
"Pdf " << SigBkgPdfName <<
" not found in workspace!\n";
156 TObjString* name =
new TObjString(SigBkgPdfName);
157 fSigBkgPdfNames.Add(name);
161 if (fWs->pdf(BkgPdfName)==NULL){
162 std::cerr <<
"Pdf " << BkgPdfName <<
" not found in workspace!\n";
165 TObjString* name =
new TObjString(BkgPdfName);
166 fBkgPdfNames.Add(name);
170 if (fWs->data(DatasetName)==NULL){
171 std::cerr <<
"Dataset " << DatasetName <<
" not found in workspace!\n";
174 TObjString* name =
new TObjString(DatasetName);
175 fDatasetsNames.Add(name);
179 TObjString* name =
new TObjString(label);
180 fLabelsNames.Add(name);
190 RooAbsPdf* HLFactory::GetTotSigBkgPdf(){
191 if (fSigBkgPdfNames.GetSize()==0)
194 if (fComboSigBkgPdf!=NULL)
195 return fComboSigBkgPdf;
197 if (!fNamesListsConsistent())
200 if (fSigBkgPdfNames.GetSize()==1){
201 TString name(((TObjString*)fSigBkgPdfNames.At(0))->String());
202 fComboSigBkgPdf=fWs->pdf(name);
203 return fComboSigBkgPdf;
206 if (!fCombinationDone)
209 RooArgList pdfs(
"pdfs");
211 TIterator* it=fSigBkgPdfNames.MakeIterator();
214 while ((obj = it->Next())){
215 ostring=(TObjString*) obj;
216 pdfs.add( *(fWs->pdf(ostring->String())) );
220 TString name(GetName());
223 TString title(GetName());
227 new RooSimultaneous(name,
232 return fComboSigBkgPdf;
241 RooAbsPdf* HLFactory::GetTotBkgPdf(){
242 if (fBkgPdfNames.GetSize()==0)
245 if (fComboBkgPdf!=NULL)
248 if (!fNamesListsConsistent())
251 if (fBkgPdfNames.GetSize()==1){
252 fComboBkgPdf=fWs->pdf(((TObjString*)fBkgPdfNames.First())->String());
256 if (!fCombinationDone)
259 RooArgList pdfs(
"pdfs");
261 TIterator* it = fBkgPdfNames.MakeIterator();
264 while ((obj = it->Next())){
265 ostring=(TObjString*) obj;
266 pdfs.add( *(fWs->pdf(ostring->String())) );
269 TString name(GetName());
272 TString title(GetName());
276 new RooSimultaneous(name,
290 RooDataSet* HLFactory::GetTotDataSet(){
291 if (fDatasetsNames.GetSize()==0)
294 if (fComboDataset!=NULL)
295 return fComboDataset;
297 if (!fNamesListsConsistent())
300 if (fDatasetsNames.GetSize()==1){
301 fComboDataset=(RooDataSet*)fWs->data(((TObjString*)fDatasetsNames.First())->String());
302 return fComboDataset;
305 if (!fCombinationDone)
309 TIterator* it = fDatasetsNames.MakeIterator();
311 TObject* obj = it->Next();
312 ostring = (TObjString*) obj;
313 fComboDataset = (RooDataSet*) fWs->data(ostring->String()) ;
314 if (!fComboDataset)
return NULL;
315 fComboDataset->Print();
316 TString dataname(GetName());
317 fComboDataset =
new RooDataSet(*fComboDataset,dataname+
"_TotData");
319 fComboCat->setIndex(catindex);
320 fComboDataset->addColumn(*fComboCat);
321 while ((obj = it->Next())){
322 ostring=(TObjString*) obj;
324 RooDataSet * data = (RooDataSet*)fWs->data(ostring->String());
325 if (!data)
return NULL;
326 RooDataSet* dummy =
new RooDataSet(*data,
"");
327 fComboCat->setIndex(catindex);
329 dummy->addColumn(*fComboCat);
330 fComboDataset->append(*dummy);
335 return fComboDataset;
343 RooCategory* HLFactory::GetTotCategory(){
347 if (!fNamesListsConsistent())
350 if (!fCombinationDone)
360 int HLFactory::ProcessCard(
const char* filename){
361 return fReadFile(filename,0);
391 int HLFactory::fReadFile(
const char*fileName,
bool is_included){
398 const int maxDeepness=50;
399 if (fInclusionLevel>maxDeepness){
400 TString warning(
"The inclusion stack is deeper than ");
401 warning+=maxDeepness;
402 warning+=
". Is this a recursive inclusion?";
403 Warning(
"fReadFile",
"%s", warning.Data());
408 std::ifstream ifile(fileName);
411 TString error(
"File ");
413 error+=
" could not be opened.";
414 Error(
"fReadFile",
"%s", error.Data());
418 TString ifileContent(
"");
419 ifileContent.ReadFile(ifile);
424 TString ifileContentStripped(
"");
426 TObjArray* lines_array = ifileContent.Tokenize(
"\n");
427 TIterator* lineIt=lines_array->MakeIterator();
429 bool in_comment=
false;
433 while((line_o=(*lineIt)())){
434 line = (
static_cast<TObjString*
>(line_o))->GetString();
438 if (line.EndsWith(
"*/")){
440 if (fVerbose) Info(
"fReadFile",
"Out of multiline comment ...");
447 if ((line.BeginsWith(
"/*") && line.EndsWith(
"*/")) ||
448 line.BeginsWith(
"//")){
449 if (fVerbose) Info(
"fReadFile",
"In single line comment ...");
454 if (line.BeginsWith(
"/*")){
456 if (fVerbose) Info(
"fReadFile",
"In multiline comment ...");
460 ifileContentStripped+=line+
"\n";
468 lines_array = ifileContentStripped.Tokenize(
";");
469 lineIt=lines_array->MakeIterator();
472 const int nNeutrals=2;
473 TString neutrals[nNeutrals]={
"\t",
" "};
475 while((line_o=(*lineIt)())){
477 line = (
static_cast<TObjString*
>(line_o))->GetString();
480 line.Strip(TString::kBoth,
' ');
483 line.ReplaceAll(
"\n",
"");
486 if (line.BeginsWith(
"echo")){
487 line = line(5,line.Length()-1);
489 std::cout <<
"Echoing line " << line.Data() << std::endl;
490 std::cout <<
"[" << GetName() <<
"] echo: "
491 << line.Data() << std::endl;
496 for (
int i=0;i<nNeutrals;++i)
497 line.ReplaceAll(neutrals[i],
"");
500 if (fVerbose) Info(
"fReadFile",
"Reading --> %s <--", line.Data());
504 if (fVerbose) Info(
"fReadFile",
"%s",
"Empty line: skipping ...");
510 if (line.BeginsWith(
"#include")){
511 line.ReplaceAll(
"#include",
"");
512 if (fVerbose) Info(
"fReadFile",
"Reading included file...");
513 fReadFile(line,
true);
518 if (fVerbose) Info(
"fReadFile",
"Parsing the line...");
534 void HLFactory::fCreateCategory(){
535 fCombinationDone=
true;
537 TString name(GetName());
540 TString title(GetName());
543 fComboCat=
new RooCategory(name,title);
545 TIterator* it=fLabelsNames.MakeIterator();
548 while ((obj = it->Next())){
549 ostring=(TObjString*) obj;
550 fComboCat->defineType(ostring->String());
559 bool HLFactory::fNamesListsConsistent(){
560 if ((fSigBkgPdfNames.GetEntries()==fBkgPdfNames.GetEntries() || fBkgPdfNames.GetEntries()==0) &&
561 (fSigBkgPdfNames.GetEntries()==fDatasetsNames.GetEntries() || fDatasetsNames.GetEntries()==0) &&
562 (fSigBkgPdfNames.GetEntries()==fLabelsNames.GetEntries() || fLabelsNames.GetEntries()==0))
565 std::cerr <<
"The number of datasets and models added as channels "
566 <<
" is not the same!\n";
574 int HLFactory::fParseLine(TString& line){
575 if (fVerbose) Info(
"fParseLine",
"Parsing line: %s", line.Data());
577 TString new_line(
"");
579 const int nequals = line.CountChar(
'=');
583 if (line.Contains(
"::") ||
585 (line.Contains(
"[") &&
586 line.Contains(
"]") &&
588 ! line.Contains(
"(") &&
589 ! line.Contains(
")"))) {
596 (nequals > 1 && line.Contains(
"SIMUL"))){
600 const int equal_index=line.First(
'=');
601 const int par_index=line.First(
'(');
602 TString o_name(line(0,equal_index));
603 TString o_class(line(equal_index+1,par_index-equal_index-1));
604 TString o_descr(line(par_index+1,line.Length()-par_index-2));
606 if (fVerbose) Info(
"fParseLine",
"o_name=%s o_class=%s o_descr=%s",
607 o_name.Data(), o_class.Data(), o_descr.Data());
611 if (o_class ==
"import"){
615 TObjArray* descr_array = o_descr.Tokenize(
",");
617 const int n_descr_parts=descr_array->GetEntries();
619 if (n_descr_parts<2 || n_descr_parts>3)
620 Error(
"fParseLine",
"Import wrong syntax: cannot process %s", o_descr.Data());
622 TString obj_name (static_cast<TObjString*>(descr_array->At(n_descr_parts-1))->GetString());
624 TString rootfile_name (static_cast<TObjString*>(descr_array->At(0))->GetString());
626 TFile* ifile=TFile::Open(rootfile_name);
630 if (n_descr_parts==3){
631 o_descr.ReplaceAll(
",",
":");
632 fWs->import(o_descr);
634 else if(n_descr_parts==2){
636 Info(
"fParseLine",
"Importing %s from %s under the name of %s",
637 obj_name.Data(), rootfile_name.Data(), o_name.Data());
638 TObject* the_obj=ifile->Get(obj_name);
639 fWs->import(*the_obj,o_name);
645 new_line=o_class+
"::"+o_name+
"("+o_descr+
")";
648 std::cout <<
"DEBUG: line: " << line.Data() << std::endl;
649 std::cout <<
"DEBUG: new_line: " << new_line.Data() << std::endl;
652 fWs->factory(new_line);