Accessing another directory then returning to the starting directory

Hi,

This is my first post on this forum, and I am also a bit new to Root.
Now I’ve read the documentation on TSystemDirectory, but it is still unclear how I should do, or even if I should use it.
My problem is, the computer I am working on macOS and I don’t have enough storage for a file which is around 40GB. What I wanted to is, hop into the external drive, read the file, then get back to the working directory. Is this possible at all, or I am trying to the undoable?e

Here’s a snip of the code:

 TString pwd(gSystem->pwd());
   if (tree == 0) {
      TString temp = "/Volumes/My Passport/work/";
      TSystemDirectory dir(temp.Data(),temp.Data());
      TFile *f = (TFile*)gROOT->GetListOfFiles()->FindObject("bigdata.root");
      gSystem->cd(pwd.Data());

      if (!f || !f->IsOpen()) {
         f = new TFile("bigdata.root");
      }
      f->GetObject("particle_tree",tree);

   }

This is part of a software that I did not write, but I am trying to understand and change it so I can use it…

It reads in the tree, then passes to another part which creates a .h file out of it, which I can include in the program I am using this for.

Thanks in advance, and also sorry, If I proposed the question in an inappropriate way or something.

You do not need to “cd” to another subdirectory. Try:

if (!tree) {
  const char *temp = "/Volumes/My Passport/work/bigdata.root";
  TFile *f = ((TFile*)(gROOT->GetListOfFiles()->FindObject(temp)));
  if ((!f) || f->IsZombie()) {
    f = TFile::Open(temp);
    if ((!f) || f->IsZombie()) { delete f; f = 0; }
  }
  if (f) f->GetObject("particle_tree", tree);
}
1 Like

This is working! Thank you!

What is interesting, I’ve been messing around with the code, and decided to give it a fresh start. I reverted back to its original form:

   if (tree == 0) {
      TFile *f = (TFile*)gROOT->GetListOfFiles()->FindObject("smalldata.root");
      if (!f || !f->IsOpen()) {
         f = new TFile("smalldata.root");
      }
      f->GetObject("particle_tree",tree);

   }

And I just changed to this

f = new TFile ("/Volumes/My Passport/work/bigdata.root");

It is working, but I do not know why exactly…
By the way, the one you provided works a little bit faster than what I made.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.