Hey, im trying to do this script but it wont work. I copy and pasted the exact text from this post. It won’t work for me. I set the correct path and extensions but when I run the program through terminal/ROOT i get nothing. Just goes to the next command line in Terminal.
Go to the ROOT “tutorials” subdirectory and try (don’t forget to use ROOT 5.34 or newer):
root [0] .L list_files.cxx
root [1] list_files("./", “.root”); // you should see “gallery.root"
root [2] list_files(”./mlp/", “.root”); // you should see “mlpHiggs.root"
root [3] list_files(”./", “.C”); // you should see about 15 “.C” files
root [4] list_files("./mlp/", “.C”); // you should see “mlpHiggs.C” and “mlpRegression.C”
Note: in the end of the “list_files” function, one should add (just before returning): delete files;
BTW. If you are using any ROOT version below 5.34, replace the line: TList *files = dir.GetListOfFiles(); with: TString pwd(gSystem->pwd());
TList *files = dir.GetListOfFiles();
gSystem->cd(pwd.Data());
I am new user of ROOT. I tried the following code by some changes but its not working. Please help me out.
void list_files(const char *dirname=“N:/”, const char *ext=".data") //I am trying to get all .data files from N: directory. But its not working.
{
TSystemDirectory dir(dirname, dirname);
TList *files = dir.GetListOfFiles();
if (files) {
TSystemFile file;
TString fname;
TIter next(files);
while ((file=(TSystemFile)next())) {
fname = file->GetName();
if (!file->IsDirectory() && fname.EndsWith(ext)) {
cout << fname.Data() << endl;
}
}
}
}
//While executing a program on ROOT command bar :
root[0] .L N:/list_files.c //files is located in N:/ directory
root[1] list_files(".abc",".data"); //.data file name staring with abc as a prefix
Try: root[0] .L list_files.cxx
root[1] list_files("N:/", ".data"); // list all "N:/*.data" files
root[2] list_files("N:/", ".data", "abc"); // list all "N:/abc*.data" files using the following “list_files.cxx”: [code]#include “TSystem.h” #include “TSystemFile.h” #include “TSystemDirectory.h” #include “TList.h” #include “TString.h”
[quote=“zintinos”]Hi !
I’m new to ROOT and this Forum, so hopefully I’ve posted my problem in the right place! Right now I’m using Root in Windows 7, I’ve managed to plot histograms and fit them. Now i would like to loop inside a directory with the files containing the information for my histograms. I tried this way :
[/quote]
Actually, it’s not really right place, since this is not a ROOT-related problem, but your C++ problem:
It’s not a big surprise, your code does not work the way you expected. When you do dir + name, do you really understand, what happens? You add some integer number to the pointer (after an array-to-pointer conversion) and got some pointer again as a result (quite soon it’s an invalid pointer, as soon as ‘name’ is bigger than number of elements in your array).
Try this to see what I mean:
void pointer_arithmetics()
{
const char dir[] = "C:/root/folder/";
for(unsigned i = 0; i < 5; ++i)
{
char name = i;//I have this line only because you have it.
std::cout<<dir + name<<std::endl;
}
}
So, either make you ‘dir’ to have TString type (and add name as Long_t or ULong_t, since TString has overloaded operator+ for these types) or use something else, but not pointer arithmetics .
Try to “implement” [url=https://root-forum.cern.ch/t/open-files-in-a-directory-with-a-for-loop/12471/9 example[/url].
Don’t miss [url=https://root-forum.cern.ch/t/how-to-define-and-assign-values-to-a-string-array-in-header/15250/8 and [url=https://root-forum.cern.ch/t/basic-histogram-using-previously-binned-data/15179/2
Because a char* is a constant pointer and is not possible to assign a string to it and a string variable is a pointer to an array of characters that can be redefined? Now I’m using again c++ after a long time without using it and learning ROOT, so I’m not sure about some concepts. I had the impresion that what I was doing was valid. Please kindly clarify this conceptually.
You do not show any place where you open and close your “rootf”.
You have two choices, either you create one single “rootf” file and then you need to create your trees with distinct names, i.e. new TTree(“EachTreeDifferentName”, “same Title no problem”), or you keep the same name of every tree, but then you need to write them to distinct “rootf” files.
I think you are improperly using “strcat” in “createTreeName” (the “treeName” will be equal to the “token[0]”), unless you really know what you are doing there (see “man strcat” for details). One cannot check another places as it’s not clear what the “filename” in “tokenOfFileName” nor the “line[0]” in “createTreeName” are.
I believe you should stop using C strings (i.e. “chat *”, “char *[]”, …) and rewrite your code so that it solely uses C++ strings (i.e. std::string and/or TString).
Also, always try to pre-compile your source code using ACLiC (the compiler will find many possible source code bugs) … root [0] .L MySourceCode.cxx++
Thanks for the guidance. I made changes to my program and its working. But I am facing another problem. I am creating one .root file and under .root file trying to create many trees. But its only creating 2 trees instead I wants to create many trees. One tree having 20480 records of data each.
Is more number of records in data file affecting to create many trees under one .root file ? Its not showing any error or warnings.