Home | News | Documentation | Download

Variable cuts in Tree->Draw() method

hist
graphics
root
tree

#1

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)



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

#3

That works perfectly, thanks :relaxed:!


#4

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.


#5

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


#6

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.


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

#8

Ah, of course!

Thanks so much, it works perfectly.