Сopying the .root directory

Good evening, everybody! Please help me to copy a name of a directory from .root file to a “char” variable.
How I can do it?

Hi,

auto myName = mydirectory.GetName();

Cheers,
D

1 Like

Mercy!
But I’m novice in Root and now I have another problem((
Inside my .root file I have TDirectoryFile which name I don’t know (actualy I know first 5 symbols of this TDirectroryFile). So I want copy a name of this TDirectoryFile to a “char” variable, but in this case your method doesn’t work.
I will be grateful if you can help me with this problem.

Hi,

if you want to list the content of a file, you can use the rootls utility or, programmatically, the TFile::ls() method.

Cheers,
D

You allright! But in my case it is not enough?

For example:

root[0] TFile f("file.root");
root[1] f.ls();
TFile**         file.root
 TFile*         file.root
  KEY: TDirectoryFile   something_what_I_need;1  something_what_I_need

I want save the name “something_what_I_need” into char t_directory_file_name[100] variable.

Thank you in advance for your reply!

Hi,

f.cd("something_what_I_need");
gDirectory->ls();

Cheers,
D

Sometimes I have .root files with the same structures and file “something_what_I_need” can has some useful parameters in his name. For example: “Range_100_Max_34_Min_5”. So I want to take this parameters from a filename. For this I want to save file name to a variable, and after some manipulations take this parameters and use it in my macros after that, like :

 sprintf(filename,"Range_%d_Max_%d_Min_%d",range,max,min);
            TH1F * hist = (TH1F*)f->Get(filename));
            hist->Draw();
            hist->Fit("gaus","W","",min,max);

I apologize if I do not understand something.

Hi,

you need to form the full path to the histogram inside the file.

Best,
D

Yes, I know. It was just short example.
It works when I use ls() or something like that, look the name and type it. But maybe exists another way?
Perhaps I can’t type the filename just put somehow it in a variable and already work with it further like:


char filename[100];
....
f.cd("file.root");
gDirectory->....Get_the_name_of_unknown_for_me_TDirectoryFile_in_this_rootfile_and_put_it_in_char?!

Hi,

I think I am a bit lost. Do you want to loop over elements in a file? Perhaps its TKeys?

Cheers,
D

1 Like

Thank you!
This is my solution:

 TFile f("file.root");
       TIter next(f.GetListOfKeys());
       TKey *key;

       while ((key=(TKey*)next())) {
           sprintf(cFolderInTheRootFile,"%s",key->GetName());
           cout <<cFolderInTheRootFile<<endl;
       }

Hi,

very good example!

D

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