Dear all,
after reading the ROOT chapter about Object Ownership and experimenting with code I have one remaining question:
How to create a directory in a TFile and store it in a smart pointer (like boost::scoped/shared_ptr).
My code is at follows:
boost::shared_ptr<TH1F> h_et;
boost::shared_ptr<TH1F> h_diElectronMass;
boost::shared_ptr<TH2F> h_ptRel_vs_DRmin;
boost::shared_ptr<TFile> outputfile;
boost::shared_ptr<TDirectory> testingDirectory;
Creating histograms in the constructor of my analysis class
Analysis::Analysis() :
h_et(new TH1F("electron_et", "electron_et", 100, 0, 100)), h_diElectronMass(new TH1F("diElectronMass", "diElectronMass", 500, 0, 500)), h_ptRel_vs_DRmin(new TH2F(
"ptRel_vs_DRmin", "ptRel_vs_DRmin", 100, 0, 1, 300, 0, 300)), outputfile(new TFile(
"egammaAnalysis.root", "RECREATE")), testingDirectory(outputfile->mkdir("testing")) {
for (unsigned int cut = 0; cut < TTbarEPlusJetsSelection::NUMBER_OF_SELECTION_STEPS; ++cut) {
cutflow[cut] = 0;
singleCuts[cut] = 0;
}
}
Writing histograms in the destructor
Analysis::~Analysis() {
testingDirectory->cd();
h_et->Write();
h_diElectronMass->Write();
h_ptRel_vs_DRmin->Write();
outputfile->Write();
outputfile->Close();
}
without the TDirectory I can save the histograms without trouble. With the histogram I get a segmentation fault. The reason being the attempt of the smart pointer to delete an already deleted pointer.
For histograms there is a SetDirectory function to disconnect these two.
I am not sure how to use the SetBit function since
did not solve the problem.
Another approach is to create the directory in memory like the histograms, however, the directory is not written:
I suppose I am missing a very small bit. Could someone please point me to it?