Problem reading TGraphErrors from TFile

Hi all,

I’ve a problem I can’t figure out. I’m am getting several TGraphErrors from different TFiles.

When I use:

 TFile *f1 = TFile::Open("Plots/MyRootFile1.root", "READ");
f1->ls();  cout << endl;

 TGraphErrors * gAND1 = (TGraphErrors*)f1->Get("Ar:CO_{2} 70:30 - Thr12-Lat14"); 

It works. I read three different rootfiles and get three different TGraphErrros in the same way, and I get no problem.

Later in the code I do exactly the same with another rootfile and another Graph:

 TFile *f4 = TFile::Open("Plots/MyRootFile2.root", "READ"); 
f4->ls();  cout << endl;
 TGraphErrors * gX = (TGraphErrors*)f4->Get("Ar:CO_{2}:CF_{4} 45:15:40 - Thr20-Lat14"); 
 gX->SetMarkerColor(kBlue);

The TFile is read because command “f4->ls()” correctly lists the content of the file.
But I get the error:

   Error: illegal pointer to class object gX 0x0 1675  print.C:224:
   *** Interpreter error recovered ***

I really don’t understand what’s the reason, I don’t see any difference from the working code and the not workin one. Maybe it’s stupid, but could it be the length of the key?

Thanks for any hellp,
Best

The TGraphErrors name looks a bit complex:

“Ar:CO_{2}:CF_{4} 45:15:40 - Thr20-Lat14”

Are you sure you have not made any mistake typing it ?

Hi,

this was also my first guess.

So I copied and pasted the name of the TGraphErrors many times copying it from the output of f4->ls() command.

Precisely, it shows:
[b] KEY: TGraphErrors Ar:CO_{2}:CF_{4} 45:15:40

  • Thr20-Lat15;1 Ar:CO_{2}:CF_{4} 45:15:40
  • Thr20-Lat15[/b]

I noticed that the key is printed on two lines, separated in between “45:15:40” and " - Thr".
I checked the TString that gives such name: there’s no newline there.

I have tried:

TGraphErrors * gX = (TGraphErrors*)f4->Get("Ar:CO_{2}:CF_{4} 45:15:40\n - Thr20-Lat14");
TGraphErrors * gX = (TGraphErrors*)f4->Get("Ar:CO_{2}:CF_{4} 45:15:40
 - Thr20-Lat14");
TGraphErrors * gX = (TGraphErrors*)f4->Get("Ar:CO_{2}:CF_{4} 45:15:40 - Thr20-Lat14;1");

But no way…

May be this can help:

root.cern.ch/how/how-draw-objec … s-its-name

If I correctly understood this code, I have to set
obj_name = “ThisIsMyGraphsName” ;
In this way I get the same error, because I have to write manually the name of the TGraphErrors.

Anyway I have mixed it with another idea.
This is a bit tricky, but I used the following code:

// get the list of keys
   TList *list = (TList *)f4->GetListOfKeys();
   if (!list) {
      std::cout << "Cannot get the list of TKeys! Aborting..." << std::endl;
      return;
   }
   
   TObject *obj;
   TListIter *it = new TListIter(list,kIterForward);
   TString GrName;
   while ((obj = it->Next()))
     {
     TString title = obj->GetTitle();
     if (  title.BeginsWith("Ar:CO_{2}:CF_{4}") && title.EndsWith("Thr20-Lat14"))
     GrName = obj->GetTitle();
     }
     
  cout << GrName << endl;

The Title printed on screen is:
Ar:CO_{2}:CF_{4} 45:15:40

  • Thr20-Lat14

If now I get tha TGraphErrors like this:

TGraphErrors * gX = (TGraphErrors*)f4->Get(GrName);

it works. A bit tricky but… at least it works.

Thanks!

yes your graph names are more like graph titles … you should make a difference … the name is the handle you use to access them so, if there is some control characters in it (I suspect “:” might be one) then you cannot access it by name.