Passing a TString in TString::Format()

Hello co-rroters,

I am trying to open a root file that contains a bunch of histograms with the name histo_001, histo_002, …, histo_011… and then plot them all in a canvas.

My code is the follwing

void plot_histos(TString filename_in, TString histo_name, TString filename_out){
	TFile *fin = new TFile(filename_in, "OPEN");
	//string h_name = filename_in;
	for (int i=1; i<11; ++i){
        //TH1F *h = (TH1F*) fin->Get(histo_name);
        TH1F *h = (TH1F*) fin->Get(TString::Format("%s_%03i", histo_name, i) );
	    TRandom *r = new TRandom();
	    int color = (int) ((113-51)*r->Rndm()+51);
	    TCanvas *c = new TCanvas();
	    h->Draw("histo same");


However it seems that I can’t pass a TString argument in TString::Format(), since when compiling the code, using .L macro.C++ gives me the following error

././plot_histos.C:11:63: error: cannot pass non-trivial object of type 'TString' to variadic function; expected type from format string was 'char *'
        TH1F *h = (TH1F*) fin->Get(TString::Format("%s_%03i", histo_name, i) );
                                                    ~~        ^~~~~~~~~~

Any idea how to pass a TString argument inside TString::Format()?

Thanks in advance!


should do it.

Thanks for your reply!

I think you mean this, right?

TH1F *h = (TH1F*) fin->Get(TString::Format("%s_%03i", histo_name.Data(), i) );

This seems to be compiled without an issue!

Yes, this is the right way to access the character string stored in a TString.

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