Making trees using a for loop

Hello,

I want to make an array of trees where each tree reads a text file and plots data from those text files onto a histogram. Does it work the same way as making an array of histograms by using a for loop? I will copy my code into this post. Root is also giving me the same message and a blank canvas every time I run the code. Here is the message:

[/usr/local/Cellar/root/6.26.06_2/lib/root/libCling.so] cling_runtime_internal_throwIfInvalidPointer (no debug info)

[] (no debug info)

[/usr/local/Cellar/root/6.26.06_2/lib/root/libCling.so] cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const (no debug info)

[/usr/local/Cellar/root/6.26.06_2/lib/root/libCling.so] cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) (no debug info)

[/usr/local/Cellar/root/6.26.06_2/lib/root/libCling.so] cling::Interpreter::EvaluateInternal(std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) (no debug info)

[/usr/local/Cellar/root/6.26.06_2/lib/root/libCling.so] cling::MetaSema::actOnxCommand(llvm::StringRef, llvm::StringRef, cling::Value*) (no debug info)

[/usr/local/Cellar/root/6.26.06_2/lib/root/libCling.so] cling::MetaParser::isXCommand(cling::MetaSema::ActionResult&, cling::Value*) (no debug info)

[/usr/local/Cellar/root/6.26.06_2/lib/root/libCling.so] cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) (no debug info)

[/usr/local/Cellar/root/6.26.06_2/lib/root/libCling.so] cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) (no debug info)

[/usr/local/Cellar/root/6.26.06_2/lib/root/libCling.so] HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) (no debug info)

[/usr/local/Cellar/root/6.26.06_2/lib/root/libCling.so] TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) (no debug info)

[/usr/local/Cellar/root/6.26.06_2/lib/root/libCling.so] TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) (no debug info)

[/usr/local/Cellar/root/6.26.06_2/lib/root/libCore.so] TApplication::ExecuteFile(char const*, int*, bool) (no debug info)

[/usr/local/Cellar/root/6.26.06_2/lib/root/libRint.so] TRint::ProcessLineNr(char const*, char const*, int*) (no debug info)

[/usr/local/Cellar/root/6.26.06_2/lib/root/libRint.so] TRint::Run(bool) (no debug info)

[/usr/local/Cellar/root/6.26.06_2/bin/root.exe] main (no debug info)

[/usr/lib/dyld] start (no debug info)

Error in : Trying to dereference null pointer or trying to call routine taking non-null arguments.

Execution of your code was aborted.

For only on file, the code works. If I really want this to work I would have to repeat the same 3 or 4 lines of code for each text file and that. There has to be a short cut to this. Here is my code:

void RMS_mip_histogram()
{
  Int_t n = 31;
  TTree t[n];
  TH2F * h[n];
  for (Int_t i = 0; i < n; i++){
    TString hist = Form("h%d",i);
    t[i].ReadFile(Form("mipRMS_18%d",i+53),"cell:RMS:x:y");
    t[i].Draw("RMS:y>>hist","RMS>0 && RMS<100 && y<=1");
    h[i] = (TH2F*) gROOT->FindObject("hist");
    h[i]->GetYaxis()->SetRangeUser(0,30);
    h[i]->GetXaxis()->SetRangeUser(0,2);
  }


  TCanvas *c1 = new TCanvas("c1","c1",800,800);
  h[0]->Draw("CANDLEX6");
  for (Int_t i = 0; i < n; i++){
    h[i+1]->Draw("CANDLEX6 SAME");
  }
}

Hope to hear a response soon.

-Charlie Clark

_ROOT Version: ROOT 6.26/06
_Platform: MacOS

Try:

void RMS_mip_histogram() {
  const Int_t n = 31;
  TH2F *h[n];
  for (Int_t i = 0; i < n; i++) {
    TTree *t = new TTree("t", "t");
    t->ReadFile(Form("mipRMS_18%d", i + 53), "cell:RMS:x:y");
    TString hist = Form("h%d", i);
    h[i] = new TH2F(hist, hist + ";y;RMS", 40, 0., 2., 60, 0., 30.);
    t->Draw("RMS:y>>" + hist, "RMS>0 && RMS<100 && y<=1", "goff");
    delete t;
  }
  
  TCanvas *c1 = new TCanvas("c1", "c1", 800, 800);
  h[0]->Draw("CANDLEX6");
  for (Int_t i = 1; i < n; i++) {
    h[i]->Draw("CANDLEX6 SAME");
  }
  c1->Modified(); c1->Update();
}

Thanks. I will try it tomorrow.

The code works. Now I want to change the value of “y” for every histogram so that the plotted data is separated from each other. Specifically, the data from each text file is named after the day of operation. So, I want to change the “y” value from y=1 to the day of operation y=1*(i+53) in the code but it doesn’t work if I write:

t->Draw("RMS:y*(i+53)>>" + hist,"RMS>0 && RMS<100");

or if I want to draw “RMS:(i+53)”. Is there another way of doing this?

t->Draw(TString::Format("RMS : y * %d >> ", i + 53) + hist);
or:
t->Draw(TString::Format("RMS : y * %d >> %s", i + 53, hist.Data()));

Note: when creating the histogram, you set axes ranges, so you do not need the selection.

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