25 namespace HistFactory{
28 std::string channelNameFromPdf( RooAbsPdf* channelPdf ) {
29 std::string channelPdfName = channelPdf->GetName();
30 std::string ChannelName = channelPdfName.substr(6, channelPdfName.size() );
34 RooAbsPdf* getSumPdfFromChannel( RooAbsPdf* sim_channel ) {
38 if(verbose) std::cout <<
"Getting the RooRealSumPdf for the channel: "
39 << sim_channel->GetName() << std::endl;
41 std::string channelPdfName = sim_channel->GetName();
42 std::string ChannelName = channelPdfName.substr(6, channelPdfName.size() );
46 std::string realSumPdfName = ChannelName +
"_model";
48 RooAbsPdf* sum_pdf = NULL;
49 TIterator* iter_sum_pdf = sim_channel->getComponents()->createIterator();
50 bool FoundSumPdf=
false;
51 RooAbsArg* sum_pdf_arg=NULL;
52 while((sum_pdf_arg=(RooAbsArg*)iter_sum_pdf->Next())) {
53 std::string NodeClassName = sum_pdf_arg->ClassName();
54 if( NodeClassName == std::string(
"RooRealSumPdf") ) {
56 sum_pdf = (RooAbsPdf*) sum_pdf_arg;
62 std::cout <<
"Failed to find RooRealSumPdf for channel: " << sim_channel->GetName() << std::endl;
63 sim_channel->getComponents()->Print(
"V");
69 if(verbose) std::cout <<
"Found RooRealSumPdf: " << sum_pdf->GetName() << std::endl;
79 void FactorizeHistFactoryPdf(
const RooArgSet &observables, RooAbsPdf &pdf, RooArgList &obsTerms, RooArgList &constraints) {
82 const std::type_info &
id =
typeid(pdf);
83 if (
id ==
typeid(RooProdPdf)) {
84 RooProdPdf *prod =
dynamic_cast<RooProdPdf *
>(&pdf);
85 RooArgList list(prod->pdfList());
86 for (
int i = 0, n = list.getSize(); i < n; ++i) {
87 RooAbsPdf *pdfi = (RooAbsPdf *) list.at(i);
88 FactorizeHistFactoryPdf(observables, *pdfi, obsTerms, constraints);
90 }
else if (
id ==
typeid(RooSimultaneous) ||
id ==
typeid(HistFactorySimultaneous) ) {
91 RooSimultaneous *sim =
dynamic_cast<RooSimultaneous *
>(&pdf);
92 RooAbsCategoryLValue *cat = (RooAbsCategoryLValue *) sim->indexCat().Clone();
93 for (
int ic = 0, nc = cat->numBins((
const char *)0); ic < nc; ++ic) {
95 FactorizeHistFactoryPdf(observables, *sim->getPdf(cat->getLabel()), obsTerms, constraints);
98 }
else if (pdf.dependsOn(observables)) {
99 if (!obsTerms.contains(pdf)) obsTerms.add(pdf);
101 if (!constraints.contains(pdf)) constraints.add(pdf);
183 bool getStatUncertaintyFromChannel( RooAbsPdf* channel, ParamHistFunc*& paramfunc, RooArgList* gammaList ) {
189 TIterator* iter = channel->getComponents()->createIterator();
190 bool FoundParamHistFunc=
false;
191 RooAbsArg* paramfunc_arg = NULL;
192 while(( paramfunc_arg = (RooAbsArg*) iter->Next() )) {
193 std::string NodeName = paramfunc_arg->GetName();
194 std::string NodeClassName = paramfunc_arg->ClassName();
195 if( NodeClassName != std::string(
"ParamHistFunc") )
continue;
196 if( NodeName.find(
"mc_stat_") != std::string::npos ) {
197 FoundParamHistFunc=
true;
198 paramfunc = (ParamHistFunc*) paramfunc_arg;
202 if( ! FoundParamHistFunc || !paramfunc ) {
203 if(verbose) std::cout <<
"Failed to find ParamHistFunc for channel: " << channel->GetName() << std::endl;
211 gammaList = (RooArgList*) &( paramfunc->paramList());
212 if(verbose) gammaList->Print(
"V");
219 void getDataValuesForObservables( std::map< std::string, std::vector<double> >& ChannelBinDataMap,
220 RooAbsData* data, RooAbsPdf* pdf ) {
226 RooSimultaneous* simPdf = (RooSimultaneous*) pdf;
229 RooArgSet* allobs = (RooArgSet*) data->get();
230 TIterator* obsIter = allobs->createIterator();
231 RooCategory* cat = NULL;
232 RooAbsArg* temp = NULL;
233 while( (temp=(RooAbsArg*) obsIter->Next())) {
235 if( strcmp(temp->ClassName(),
"RooCategory")==0){
236 cat = (RooCategory*) temp;
241 if(!cat) std::cout <<
"didn't find category"<< std::endl;
242 else std::cout <<
"found category"<< std::endl;
247 TList* dataByCategory = data->split(*cat);
248 if(verbose) dataByCategory->Print();
253 RooCategory* channelCat = (RooCategory*) (&simPdf->indexCat());
254 TIterator* iter = channelCat->typeIterator() ;
255 RooCatType* tt = NULL;
256 while((tt=(RooCatType*) iter->Next())) {
259 RooAbsPdf* pdftmp = simPdf->getPdf(tt->GetName()) ;
261 std::string ChannelName = pdftmp->GetName();
262 if(verbose) std::cout <<
"Getting data for channel: " << ChannelName << std::endl;
263 ChannelBinDataMap[ ChannelName ] = std::vector<double>();
265 RooAbsData* dataForChan = (RooAbsData*) dataByCategory->FindObject(tt->GetName());
266 if(verbose) dataForChan->Print();
269 RooArgSet* obstmp = pdftmp->getObservables(*dataForChan->get()) ;
270 RooRealVar* obs = ((RooRealVar*)obstmp->first());
271 if(verbose) obs->Print();
285 TH1* histForN = dataForChan->createHistogram(
"HhstForN",*obs);
286 for(
int i=1; i<=histForN->GetNbinsX(); ++i){
287 double n = histForN->GetBinContent(i);
288 if(verbose) std::cout <<
"n" << i <<
" = " << n << std::endl;
289 ChannelBinDataMap[ ChannelName ].push_back( n );
301 int getStatUncertaintyConstraintTerm( RooArgList* constraints, RooRealVar* gamma_stat,
302 RooAbsReal*& pois_nom, RooRealVar*& tau ) {
313 TIterator* iter_list = constraints->createIterator();
314 RooAbsArg* term_constr=NULL;
315 bool FoundConstraintTerm=
false;
316 RooAbsPdf* constraintTerm=NULL;
317 while((term_constr=(RooAbsArg*)iter_list->Next())) {
318 std::string TermName = term_constr->GetName();
322 if( term_constr->dependsOn( *gamma_stat) ) {
323 if( TermName.find(
"_constraint")!=std::string::npos ) {
324 FoundConstraintTerm=
true;
325 constraintTerm = (RooAbsPdf*) term_constr;
330 if( FoundConstraintTerm==
false ) {
331 std::cout <<
"Error: Couldn't find constraint term for parameter: " << gamma_stat->GetName()
332 <<
" among constraints: " << constraints->GetName() << std::endl;
333 constraints->Print(
"V");
334 throw std::runtime_error(
"Failed to find Gamma ConstraintTerm");
352 bool FoundNomMean=
false;
353 TIterator* iter_pois = constraintTerm->serverIterator();
354 RooAbsArg* term_pois ;
355 while((term_pois=(RooAbsArg*)iter_pois->Next())) {
356 std::string serverName = term_pois->GetName();
358 if( serverName.find(
"nom_")!=std::string::npos ) {
360 pois_nom = (RooRealVar*) term_pois;
363 if( !FoundNomMean || !pois_nom ) {
364 std::cout <<
"Error: Did not find Nominal Pois Mean parameter in gamma constraint term PoissonMean: "
365 << constraintTerm->GetName() << std::endl;
366 throw std::runtime_error(
"Failed to find Nom Pois Mean");
369 if(verbose) std::cout <<
"Found Poisson 'data' term: " << pois_nom->GetName() << std::endl;
376 TIterator* iter_constr = constraintTerm->serverIterator();
377 RooAbsArg* pois_mean_arg=NULL;
378 bool FoundPoissonMean =
false;
379 while(( pois_mean_arg = (RooAbsArg*) iter_constr->Next() )) {
380 std::string serverName = pois_mean_arg->GetName();
381 if( pois_mean_arg->dependsOn( *gamma_stat ) ) {
382 FoundPoissonMean=
true;
387 if( !FoundPoissonMean || !pois_mean_arg ) {
388 std::cout <<
"Error: Did not find PoissonMean parameter in gamma constraint term: "
389 << constraintTerm->GetName() << std::endl;
390 throw std::runtime_error(
"Failed to find PoissonMean");
394 if(verbose) std::cout <<
"Found Poisson 'mean' term: " << pois_mean_arg->GetName() << std::endl;
399 TIterator* iter_product = pois_mean_arg->serverIterator();
400 RooAbsArg* term_in_product ;
402 while((term_in_product=(RooAbsArg*)iter_product->Next())) {
403 std::string serverName = term_in_product->GetName();
405 if( serverName.find(
"_tau")!=std::string::npos ) {
407 tau = (RooRealVar*) term_in_product;
410 if( !FoundTau || !tau ) {
411 std::cout <<
"Error: Did not find Tau parameter in gamma constraint term PoissonMean: "
412 << pois_mean_arg->GetName() << std::endl;
413 throw std::runtime_error(
"Failed to find Tau");
416 if(verbose) std::cout <<
"Found Poisson 'tau' term: " << tau->GetName() << std::endl;