Please read tips for efficient and successful posting and posting code
ROOT Version: ROOT 6.26/04 sourced from CVMFS
Platform: MacOS 12.6.1
Compiler: Apple clang version 14.0.0
Hi, I’m trying to make a macro file which reads root file with histograms in it, and draw simple plots. I’m trying to run it after compiling it so that I can provide arguments more easily. (./Macro.exe <args>
instead of root -l 'Macro.cc("<args>")'
)
But whenever I compile the macro and make Macro.exe executable file, the TCanvas won’t appear on my screen, even though TCanvas->SaveAs("~~.png")
will save proper plot in png format.
Also when executing macro with root -l 'Macro.cc("args")'
, the plot will be drawn & shown properly on my screen. (so this means there’s hardly anything wrong with my compiler & drawing code)
This code is for prompt DQM purpose so I’d like to keep command as short as possible & want TCanvas to be displayed immediately on screen.
Here’s the snippet of my code.
Compile version draw.cc
int main(int argc, char* argv[]) {
std::string file_number = argv[1];
TFile* file = TFile::Open( ("file_" + file_number + ".root").c_str() );
TH1F* hist_1 = (TH1F*) file->Get("Histogram_name_1");
TH1F* hist_2 = (TH1F*) file->Get("Histogram_name_2");
TCanvas* c = new TCanvas("c", "c", 1000, 1000);
c->Draw();
c->cd();
c->Divide(2);
c->cd(0);
hist_1->Draw("hist");
c->cd(1);
hist_2->Draw("hist");
c->Modified();
c->Update();
// Without this lines, the executable terminates right away...
// Is there any way to stop it?
std::cout << "Type any key if you want to quit..." << std::endl;
std::string key;
std::cin >> key;
hist_file->Close();
return 0;
}
Then I compile the above macro with
g++ `root-config --cflags --libs` draw.cc -o draw.exe
and execute it with ./draw.exe <file number>
This won’t display any plots… but when I put c->SaveAs("plot.png");
after c->Update();
, the proper plot image file will be saved.
However since I may need to adjust pads on-the-fly, I’d like plots to be displayed on TCanvas so that I can drag and control plots right on my screen.
Here’s non compiled version. draw.cc
int draw(std::string file_number) {
TFile* file = TFile::Open( ("file_" + file_number + ".root").c_str() );
TH1F* hist_1 = (TH1F*) file->Get("Histogram_name_1");
TH1F* hist_2 = (TH1F*) file->Get("Histogram_name_2");
TCanvas* c = new TCanvas("c", "c", 1000, 1000);
c->Draw();
c->cd();
c->Divide(2);
c->cd(0);
hist_1->Draw("hist");
c->cd(1);
hist_2->Draw("hist");
c->Modified();
c->Update();
std::cout << "Type any key if you want to quit..." << std::endl;
std::string key;
std::cin >> key;
hist_file->Close();
return 0;
}
and I execute this macro with
root -l 'draw.cc("<file number>")'
Then, the TCanvas will appear and the plots will be draw as desired.
But since it is bothersome to type the command, I want to use compiled macro with Tcanvas displayed.
Is there any good way to do this?
Thanks.