Variable cuts in Tree->Draw() method

It is often faster to use the draw method:

TreeName->Draw("var1:var2>>histName","var1 > x && var2 < y")

in many situations, but the only problem I face is that those x and y must be pure numbers, I don’t know how to use variables instead. For example I want to write a macro:


void macro(double x, double y)
{

  //file loading etc etc with TTree name TreeName, I am going to fill histogram named histName
TreeName->Draw("var1:var2>>histName","var1 > x && var2 < y")

}

How can I achieve that?

This question is related to this and this, but not really what I am asking.

Thanks


ROOT Version: 5.28/00a
Platform: Readhat: 2.6.18-308.11.1.el5
Compiler: gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-50)


TreeName->Draw("var1:var2>>histName",Form("var1 > %g && var2 < %g",x,y));

That works perfectly, thanks :relaxed:!

I have a related question and I am not sure if it deserves an independent question so just asking here:

We can create root macros that can be compiled and executables can be produced, for example consider a program named macro.cpp:

int main(int argc, char *argv[])
{ 
    //code body utilizing the arguments argv[] etc
   // we open a root file containing tree named TreeName and containing a leaf named var1 here and 
  //fill some histograms

  TH1F *hist = new TH1F("hist","hist",nbin,a,b);

      for (Long64_t jentry=0; jentry<nentries; jentry++) 
     {
        //blah blah
          hist->Fill(some_variable);
     }

return 0;
}

We can compile it

g++ macro.cpp `root-config --glibs --cflags` -o macro

Then we can run this from the terminal as:

./macro arg1 arg2 etc

this method works perfectly, but I was wondering if it was possible to avoid that for loop over the tree and the fill method hist->Fill(), instead we use the direct filling method

TreeName->Draw("var1>>hist",Form("var1 > %g",x));

keeping everything else the same?

Therefore the final program should look something like this:

int main(int argc, char *argv[])
{ 
       TString FileName;
       FileName.Form("%s", argv[1]); //rootfile through argument
       double cut1 = atof(argov[2]);    //cut on one of the variable var1 in the tree
  
      TFile *file = TFile::Open(FileName);
      TH1F *hist = new TH1F("hist","hist",nbin,a,b);

      TreeName->Draw("var1>>hist",Form("var1 > %g",cut1));  
     
   return 0;
}

After compiling I run:

./macro example.root someNumber

If this can be done, what should be the syntax?

Thanks a lot.

Your final program looks fine seems to me. Where is the problem ?

TreeName->Draw("var1>>hist",Form("var1 > %g",cut1));

It doesn’t recognize the tree or the variables inside e.g. var1, but this is not an issue if one works on a root shell instead of the standard terminal.

TTree *TreeName; file->GetObject("TreeName", TreeName);
if (!TreeName) return 1; // just a precaution

Ah, of course!

Thanks so much, it works perfectly.

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