Start root with a root file and script

I would like to start root in the following way

root -l file.root

file.root contains TH1F histo

The contents of the script are the following

TCanvas c;
histo -> Draw();

The complaint from root is

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;

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

1 Like

Calling like this

I get the following problem:

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

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

1 Like


$> root -l '"file.root")'

to prevent too eager shell evaluation.


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