#ifndef __CINT__ #include "RooGlobalFunc.h" #include #endif #include #include #include "RooRealVar.h" #include "RooDataHist.h" #include "RooHistPdf.h" #include "RooArgList.h" #include "RooAddPdf.h" #include "RooFitResult.h" #include "TH1D.h" /* Jim Henderson 03/06/14 Problem - idea was to write a class that can take any number of template histograms and fit them to a data histo - it is observed that if we try to fill the vector of RooHistPdfs in the same loop as the RooDataHists we get a seg. fault - This can be avoided by filling the RooHistPdfs vector in a separate, identical loop To observed the seg fault, comment out lines 60-63 */ void badLoop() { TH1D* data = new TH1D("data","data",3,-0.5,2.5); TH1D* mc1 = new TH1D("mc1","mc1",3,-0.5,2.5); TH1D* mc2 = new TH1D("mc2","mc2",3,-0.5,2.5); std::vector mc_hists; mc_hists.push_back( mc1 ); mc_hists.push_back( mc2 ); data->Fill(1); mc1->Fill(1); mc2->Fill(2); RooRealVar fit_variable("fit_variable","fit_variable", -0.5, 2.5); RooDataHist data_RooDataHist("data_RooDataHist", "data_RooDataHist", fit_variable, RooFit::Import(*data)); std::vector< RooDataHist > mc_RooDataHists; std::vector< RooRealVar > mc_integrals; std::vector< RooHistPdf > mc_RooHistPdf; for ( int mc_it = 0; mc_it < mc_hists.size(); mc_it++ ){ std::stringstream mc_component; mc_component << mc_it; mc_RooDataHists.push_back( RooDataHist( ( "RooDataHist_" + mc_component.str() ).c_str(), ( "RooDataHist_" + mc_component.str() ).c_str(), fit_variable, RooFit::Import( *(mc_hists.at(mc_it)) ) ) ); mc_integrals.push_back( RooRealVar( ( "mc_integrals_" + mc_component.str() ).c_str(), ( "mc_integrals_" + mc_component.str() ).c_str(), mc_hists.at(mc_it)->Integral(), 0.0, 10.0 * mc_hists.at(mc_it)->Integral() ) ); /* } // Comment me out - to see seg fault for ( int mc_it = 0; mc_it < mc_hists.size(); mc_it++ ){ // Comment me out std::stringstream mc_component; // Comment me out mc_component << mc_it; // Comment me out */ mc_RooHistPdf.push_back( RooHistPdf( ( "mcRooHistPdf_" + mc_component.str() ).c_str(), ( "mcRooHistPdf_" + mc_component.str() ).c_str(), fit_variable, mc_RooDataHists.at(mc_it) ) ); } RooArgList mc_RooHistPdfArgList( mc_RooHistPdf.at(0), mc_RooHistPdf.at(1) ); RooArgList mc_integralsArgList( mc_integrals.at(0), mc_integrals.at(1) ); RooAddPdf model( "model", "model", mc_RooHistPdfArgList, mc_integralsArgList ); RooFitResult* result = model.fitTo( data_RooDataHist ); delete data; delete mc1; delete mc2; delete result; }