TTree : drawing only once per event


Is it possible to tell the Draw() function to draw only the first iteration having passed the selection criteria ?

For exemple something like this :

tree->Draw(“array”,“array>1 && onlyfirst”)

would fill only the first element of array that is greater than 1 in each event.




There is no easy way (that I know of) of doing this with a formula.
You need to either either MakeSelector or MakeProxy or to pass a script to Draw. Something like:void onlyfirst() { Int_t len = ... here grab the length of your array ...; for(int i=0;i < len; ++i) { if (array[i]>1) { htemp->Fill(array[i); return; } }
and usetree->Draw("onlyfirst.C","","nohist");


For those interrested I found an other solution.
I use the code generated by MakeClass() from my tree and then define a Draw() like method :

void WZan::DrawFirst(TString varexp, TString  selection, Option_t* option , Long64_t nentries , Long64_t firstentry )
   if (fChain == 0) return;

   nentries = (nentries < fChain->GetEntriesFast() ) ? : nentries , fChain->GetEntriesFast() ;

   TTreeFormula *exp = fChain->GetVar1();
   TTreeFormula *sel = fChain->GetSelect();
   cout << exp << endl;
   Int_t nbytes = 0, nb = 0;
   for (Long64_t jentry=firstentry; jentry<nentries;jentry++) {
      Long64_t ientry = LoadTree(jentry);
      if (ientry < 0) break;
      nb = fChain->GetEntry(jentry);   nbytes += nb;
      bool doplot=false;
      int i=0;
      if(sel) {for(i=0;i<exp->GetNdata();i++){
          if(sel->EvalInstance(i) > 0){doplot=true; break;}
      } else doplot = true;
      if(doplot) m_histo->Fill(exp->EvalInstance(i));

Here you need to add a TH1* member to the class and have it valid before the call of the function.
Load it compiled, so the execution is not too long (using .“L theclass.C++”).
I find it to work well.
The remaining problems : only 1D graph, and no automatique computing of the histo range as in TTree::Draw(), and “same” option won’t work.