using namespace RooFit; void jimMacro() { // Set up p.d.f. TString xName("x"); RooRealVar *x = new RooRealVar(xName,xName,-3.0,3.0); RooRealVar m("m","m",0.0), s("s","s",1.0); RooAbsPdf *pdf = new RooGaussian("pdf","pdf",*x,m,s); // Create histo from p.d.f. TString hName("h"); TH1* h = pdf->createHistogram(hName,*x,Binning(20)); // As I understand it, h is owned by the current directory (gDirectory) TString pName(hName); pName += "__"; pName += xName; TH1 *p = (TH1*)gDirectory->GetList()->FindObject(pName); // Check that the object was found cout << "\nBefore h->SetDirectory(0) call:\n" << endl; cout << "Address pointed to by h = " << h << endl; cout << "Address pointed to by p = " << p << endl; // Remove from directory h->SetDirectory(0); // Should no longer be able to find in gDirectory TString qName(pName); TH1 *q = (TH1*)gDirectory->GetList()->FindObject(qName); cout << "\nAfter h->SetDirectory(0) call:\n" << endl; cout << "Address pointed to by h = " << h << endl; cout << "Address pointed to by q = " << q << endl << endl; // It is now my responsibility to delete h when I've finished with it delete h; h = NULL; p = NULL; // Tidy up other stuff delete x; x = NULL; delete pdf; pdf = NULL; }