Start root with a root file and script

I would like to start root in the following way

root -l file.root script.cc

file.root contains TH1F histo

The contents of the script are the following

TCanvas c;
histo -> Draw();
c.SaveAs(“histo.png”);

The complaint from root is

error:
unknown type name ‘histo’
histo -> Draw();

My idea is that as first file,root got opened root should know what is histo.
What to do?

Try this:

int script(const char *filename)
{
   TFile *f = TFile::Open(filename);
   if (!f) {
      std::cout << "cannot open " << filename << std::endl;
      return -1;
   }
   TH1F *histo;
   f->GetObject("histo", histo);
   if (!histo) {
      std::cout << filename << " doesn't contain histogram named \"histo\"" << std::endl;
      return -2;
   }
   TCanvas c;
   histo->Draw();
   c.SaveAs("histo.png");
}

And call it like this: root -l script.cc(\"file.root\")
Or, if you want to quit root immediately: root -l -q script.cc(\"file.root\")

1 Like

Calling like this

I get the following problem:

root -l script.cc(“file.root”)
-bash: syntax error near unexpected token `(’

Try root -l -q script.cc\(\"hsimple.root\"\)

1 Like

or:

$> root -l 'script.cc("file.root")'

to prevent too eager shell evaluation.

2 Likes

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