Hi, Rooters,
When I run this script CountingModel.C which copied from this website, twiki.cern.ch/twiki/bin/view/Ro … del_and_fi .
The produced root file, CoutringModel.root, can be opened without any problem.
Because the model I’m trying to apply can’t be expressed by the functions Roofit provided.
So, I have to build a model with a C++ script.
With the help of the script of “rf104_classfactory.C”, after some struggling,
my scripts worked finally : produced a root file.
However, the problems of the root file are
1). it can’t be used to do a hyper test by applying “SimpleHypoTestInv.C”.
2). it can’t be opened normally as the "CoutringModel.root ".
It crashed, and the error message is something like :
~~~~~~~~~~~~~~~~~~ Beginning of error message ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Warning in TClass::TClass: no dictionary for class BkgLinearPdf is available
Warning in TClass::TClass: no dictionary for class OOnePdf is available
root [1] TBrowser a
root [2] Error in TBufferFile::ReadObject: trying to read an emulated class (BkgLinearPdf) to store in a compiled pointer (TObject)
*** Break *** segmentation violation
===========================================================
There was a crash.
This is the entire stack trace of all threads:
…
~~~~~~~~~~~~~~~~~~ End of error message ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
After checking around here, I found similar cases while haven’t been really solved.
[Proof crashing with autoImportClassCode
[root.cern.ch/phpBB3/viewtopic.p … Pdf#p74299](Compiling and using a custom RooFit class
Any expert in this community, could you please take a close look ?
Thanks !
For your reference, I attached my script here : it produce root file normally, but the root file can’t be opened normally.
#include "TTree.h"
using namespace RooFit;
using namespace RooStats;
void CountingModel(  int nsig = 1,         // number of signal events .
                     int nbkg = 169)       // number of background events.
{
   RooWorkspace w("w");
   ///~~~~~~~~~~~~~~~~~~  Begining of creating a model p.d.f. ~~~~~~~~~~~~~~///
  RooClassFactory::makePdf("BkgLinearPdf","x,A,B") ;
#ifdef __CINT__
  gROOT->ProcessLineSync(".x BkgLinearPdf.cxx+") ;
#endif
  RooClassFactory::makePdf("OOnePdf","x,cof") ;
#ifdef __CINT__
  gROOT->ProcessLineSync(".x OOnePdf.cxx+") ;
#endif
   w.factory("BkgLinearPdf:bkg_pdf(x[0,10], a[2.5,1.0,30.0], b[-0.0125])");/// a,interceipt; b, slope.
   ///w.factory("OOnePdf:sig_pdf(x, mass[2], sigma[0.3])");
   w.factory("OOnePdf:sig_pdf(x[0,10],cof[1.0,0.01,100.0])");
   // Define the fraction of signal .
   /// RooRealVar fsig("fsig","signal fraction",0.001,0.,1.) ;
   ///w.factory("SUM:model(fsig[0,1.0] * sig_pdf, (1 - fsig[0,1.0]) * bkg_pdf)");  // for extended model
   w.factory("SUM:model(nsig[0,170]*sig_pdf, nbkg[0,170]*bkg_pdf)");  // for extended model
                            ///[0,1000] is the range of nsig and nbkg. their values assigned above.
   ///~~~~~~~~~~~~~~~~~~  End of creating a model p.d.f. ~~~~~~~~~~~~~~///
   
   ///~~~~~~~  Begining of creating the ModelConfig object and set "b0" as a global observable ~~~///
  /// w.var("a")->setVal(a); /// this line shouldn't be necessary for our usage. 
   w.var("a")->setConstant(true); // needed for being treated as global observables
   ///w.var("sigmab")->setVal(sigmab*b);  
   
   ModelConfig mc("ModelConfig",&w);/// declare the name and reference of ModelConfig as "mc" and "ModelConfig".
   mc.SetPdf(*w.pdf("model")); /// The pdf of mc is the "model(pdf, constraint)" from w factory.
   mc.SetParametersOfInterest(*w.var("cof")); /// For ModelConfig, the POI is the "s" in w factory.
   mc.SetObservables(*w.var("x"));  /// For ModelConfig, the observables is the "nobs" in w factory.
   ///mc.SetNuisanceParameters(*w.var("a"),"nbkg" );/// For ModelConfig, the NP is the "b" in w factory.
   w.defineSet("nuisParams","a,nbkg");
   mc.SetNuisanceParameters(*w.set("nuisParams") );/// For ModelConfig, the NP is the "b" in w factory.
   // these are needed for the hypothesis tests
   mc.SetSnapshot(*w.var("cof"));
   // need now to set the global observable
   mc.SetGlobalObservables(*w.var("a0"));
   mc.Print();
   // import model in the workspace 
   w.import(mc);
   
   ///~~~~~~~  End of creating the ModelConfig object and set "b0" as a global observable ~~~///
  // import a tree
  TFile *treefile = TFile::Open("measuredEnergyTree.root");
  TTree* tree = (TTree*) treefile->Get("ntuple");
   
  // Define observables x
  RooRealVar x("x","x",1,0,11) ;
  // Import only observables (x)
  RooDataSet ds("ds","ds",RooArgSet(x),Import(*tree)) ;
  ds.Print() ;
  ds.add( RooArgSet(x));
  RooPlot* frame3 = x.frame(Title("import data from a tree")) ;
  ds.plotOn(frame3) ;
  TCanvas* c = new TCanvas("import data from a tree","import data from a tree ",800,800) ;
   gPad->SetLeftMargin(0.15) ; frame3->GetYaxis()->SetTitleOffset(1.4) ; frame3->Draw() ;
   w.import(ds);
   w.Print();
   TString fileName = "CountingModel.root"; 
   // write workspace in the file (recreate file if already existing)
   w.writeToFile(fileName, true);
}Thanks !
Best,
Junhui
 .
 .
 
   .
 . .
.