// Either load the library before running ".x testContainer.C+" (i.e. compiled), // then you'll need the include. Or run ".x testContainer.C" and call // gSystem->Load("libCalibrationContainer.so"); in here - but then the #include // is in the way: CINT *replaces* elements from the dictionary when loading // the #include :-( // #include "CalibrationDataContainer.h" #include "TFile.h" #include "TSystem.h" #include "TH1.h" #include #include void testContainer() { // You seem to prefer histograms to behave like all other objects when it // comes to object lifetime, i.e. you want to delete them instead of TFile // deleting them: TH1::AddDirectory(kFALSE); gSystem->Load("libCalibrationContainer.so"); // create a new ROOT file TString fname("containerFile.root"); TFile* f = TFile::Open(fname, "RECREATE"); if (f->IsZombie()) { std::cout << "error: cannot create file " << fname << std::endl; delete f; return; } // create two objects, and write them to file Analysis::CalibrationDataHistogramContainer* ch = new Analysis::CalibrationDataHistogramContainer("HH"); Analysis::CalibrationDataFunctionContainer* cf = new Analysis::CalibrationDataFunctionContainer("FF"); f->WriteTObject(ch,"HF"); ch->dummy(); f->WriteTObject(cf); cf->dummy(); TH1F* hh = new TH1F("hh", "test 1D histogram; example abscissa", 50, 0.0, 5.0); hh->Write(); f->ls(); // close the file, and delete all objects delete ch; delete cf; delete hh; delete f; // now re-open the ROOT file and retrieve the objects by f = TFile::Open(fname); std::cout << "after re-opening the file: " << std::endl; f->ls(); // Type-safe version without (slow) dynamic_cast: Analysis::CalibrationDataHistogramContainer* chb = 0; f->GetObject("HF", chb); Analysis::CalibrationDataContainer* cfb = 0; f->GetObject("FF", cfb); if (chb) chb->dummy(); else std::cout << " error: null pointer for object chb" << std::endl; if (cfb) cfb->dummy(); else std::cout << " error: null pointer for object cfb" << std::endl; delete chb; delete cfb; delete f; }