Create a new folder

Hi, in the attached macro, I open a root file, then I save plots in an existing directory (Currently the directory is D:/). Given that I have a lot of root file (si-500408, si-500330, si-500407 etc etc) and for each files I’ve to study energies in all the subdetectors, I wrote a code so that I just write the root file number (for example 500408) by defining

int ifile=500408; 

and the subdetecors numbers

int a=8; //First subdetector name to analyze. To change each time
	int b=9; // Second subdetector name to analyze. To change each time

then the macro generates files by using this numbers for example:

TString cdname = TString::Format("si-%d: Time_{%s}_{%d}-Time_{%s}_{%d}", ifile,timenamesuba,a,timenamesubb,b);

Currently the folder in wich to save plots must exist, otherwise, the macro can’t save them; I’d like to write a function so that if, for example, the root file number is 500407 and the detector numbers are 8 and 9, ROOT create the folder D:/si-500407/sub_8-9/ in which to save the files, so that I don’t have to create subidretories by hand; should it be possible?
Thanks

calo.cpp (12.9 KB)


ROOT Version: 5.34/38
Platform: Windows


Hi,

https://root.cern.ch/doc/master/classTSystem.html#a5bd72c8b0e57e0aed8b418adf4650475

is what you are looking for.

Cheers,
D

Hi dpiparo, I didn’t understand how to use it. I replaced

char dirout[200]="D:/";

with:

TString dirout=TSystem::mkdir	("D:/si-%d/sub_%d-%d/",ifile,a,b );

but I get the error in the attachment.

gSystem->mkdir(TString::Format("D:/si-%d/sub_%d-%d/", ifile, a, b), kTRUE);

Hi Wile thank you for your reply.

I used your code

gSystem->mkdir(TString::Format("D:/si-%d/sub_%d-%d/", ifile, a, b), kTRUE);

and I replaced the lines as for example

TString myplotdeltatimeout = TString::Format("%ssi-%d_deltatime%d-%d.pdf",dirout,ifile,a,b);

with

TString myplotdeltatimeout = TString::Format("si-%d_deltatime%d-%d.pdf",ifile,a,b);

unfortunately, it doesn’t create a new folder but it saves the plots in the ROOT main folder.

I also tried to write:

TString myplottimeout = gSystem->mkdir(TString::Format("D:/si-%d/sub_%d-%d/si-%d_time%d-%d.pdf",ifile,a,b,ifile,a,b), kTRUE);

but it just produces .ps files in the ROOT main folder.

Hi wile, I included

#include "TSystem.h"

and I used your code

gSystem->mkdir(TString::Format("D:/si-%d/sub_%d-%d/", ifile, a, b), kTRUE);

but when I run the macro, it stops without doing anything and ROOT automatically closes.

Hi,

You have to do it in two steps:

gSystem->mkdir(TString::Format("D:/si-%d/", ifile), kTRUE);
gSystem->mkdir(TString::Format("D:/si-%d/sub_%d-%d/", ifile, a, b), kTRUE);

Cheers, Bertrand.

Why “in two steps”?
The last parameter “recursive = kTRUE” which should “make parent directories as needed”.

it doesn’t seem to work properly on Windows…

Hi bellenot! Thanks for your post! By your code ROOT crated the folder
D:/si-%d/sub_%d-%d/
but I noticed that writing, for example,

	TString myplotdeltatimeout = TString::Format("si-%d_deltatime%d-%d.pdf",ifile,a,b);

ROOT saves the plot in the ROOT main folder instead of in my D:/si-%d/sub_%d-%d/

Then I think that I’ve to set the folder where to save plots

then I wrote

gSystem->mkdir(TString::Format("%ssi-%d/", dirout,ifile), kTRUE);
	gSystem->mkdir(TString::Format("%ssi-%d/sub_%d-%d/",dirout,ifile, a, b), kTRUE);
	TString outfolder=TString::Format("%ssi-%d/sub_%d-%d/",dirout,ifile, a, b);
TString myplotdeltatimeout = TString::Format("%ssi-%d_deltatime%d-%d.pdf",outfolder,ifile,a,b);

I used the %s for the variable TString outfolder variable (as for the char variables), but it doen’t work. I think that the problem is that %s just works for char variables. What is the similiar command of %s for a TString variable?

Try with:

TString myplotdeltatimeout = TString::Format("%ssi-%d_deltatime%d-%d.pdf",outfolder.Data(),ifile,a,b);

See the TString Class Reference, you can read:

Hi bellenot! Thank you! It worked!

1 Like

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