Draw result of TMath::MaxElement

I have a TTree with a branch variable which is an array of floats called fPosition_mm. The length of this array varies from event-to-event and there is a branch variable which specifies that length in each event called iRecordSize.

I would like to draw a histogram of the max element of fPosition_mm using TTree::Draw(), however, the following does not work:

tree->Draw("TMath::MaxElement(iRecordSize,fPosition_mm)","","hist")

Could someone clarify where my notation for using the TMath::MaxElement() function is going wrong? Thanks!

I think Draw does not support arguments like this. You could use a Proxy (see TTree::MakeProxy() and the h1analysisProxy example), but it may be simpler to just make your own histogram and fill it by looping over the tree; something like (in mymacro.C):

double maxval(int n, int* x) {
  return TMath::MaxElement(n,x);
}

void mymacro() {
  TFile *f = new TFile("myfile.root");
  TTree *T = (TTree*)f->Get("T");
  int n, x[3];  // here all have size 3; use n if saved in the tree
  T->SetBranchAddress("x",x);
// T->SetBranchAddress("n",&n);  // array sizes, if given in the tree
  TH1D *h = new TH1D("h","h",12,0,12);  // use your own values
  T->Scan("x");
  double v = 0;
  for (int i=0; i<T->GetEntries(); ++i) {
    T->GetEvent(i);
    v = maxval(3,x);  // (n,x);  alternatively:  v = TMath::MaxElement(3,x);
    cout << v << endl;
    h->Fill(v);
  }
  h->Draw();
}

which gives, in this case:

***********************************
*    Row   * Instance *         x *
***********************************
*        0 *        0 *         0 *
*        0 *        1 *         1 *
*        0 *        2 *         2 *
*        1 *        0 *         1 *
*        1 *        1 *         2 *
*        1 *        2 *         3 *
*        2 *        0 *         4 *
*        2 *        1 *         5 *
*        2 *        2 *         6 *
*        3 *        0 *         9 *
*        3 *        1 *        10 *
*        3 *        2 *        11 *
***********************************
2
3
6
11

Try: tree->Draw("Max$(fPosition_mm)", "", "hist");

1 Like

Thanks! For those curious, this Max$ function is part of the special functions listed here (scroll down!).

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