Hello, for an application I am writing I need to extract a number of histograms (1d/2d) and graphs (including TGraphErrors) from a file. I figured a good way to keep track to all of them is to have them stored in a TList. I have a couple of questions related to the simple code below:
(1) What is the magic command(s) that allow me to keep my objects (and my list) in memory after I close the file? I have tried a number of things without success (involving moving the list to the “previous” directory which exists before the file is opened).
(2) How do I get the actual class name (TH1F, TH2F, TGraph…) of my objects in memory? See printout below. This has to be straightforward, but I cannot find an equivalent of GetClassName() for a TObject.
OBJ: TList TList Doubly linked list : 0
OBJ: TH1F h_CopyHisto T2 rate : 0 at: 0x7fbdd88b6c20
OBJ: TH2F h_Ex2D detector 5 x 5 in LAB (detected) : 0 at: 0x7fbdd8904340
OBJ: TGraph g_T2rate : 0 at: 0x7fbdd8916e20
OBJ: TGraph g_AoP : 0 at: 0x7fbdd8916d70
Thanks in advance. Fred.
TList *GetObjects(char *filename)
{
TList *myObjects = new TList();
TFile *f = new TFile(filename);
TIter next(f->GetListOfKeys());
TKey *key;
while (key = (TKey*)next()) {
TClass *cl = gROOT->GetClass(key->GetClassName());
if (cl->InheritsFrom("TH1") || cl->InheritsFrom("TGraph")) {
myObjects->Add(key->ReadObj());
}
}
// (1) HOW CAN I CLOSE THE FILE AND KEEP MY LIST AND ALL ITS OBJECTS IN MEMORY?
// f->Close();
myObjects->ls();
return myObjects;
}
ReadObjects(char *filename)
{
TObject *obj;
TList *list_of_objects = GetObjects(filename);
TIter next(list_of_objects);
while (obj = next()) {
// (2) HOW DO I GET THE ACTUAL CLASS NAME OF MY OBJECTS?
cout << obj->GetName() << " is a " << "<TH1F/TH2F/TGraph/TGraphErrors/etc...>" << endl;
}
return 0;
}