Read branch from root tree

Hi,

Iam trying to read a branch from my root tree. The branch and its contents are defined in an external object called Hits. I want to read out 4 entries from Hits, m_row, m_column, m_charge and m_id and dump them to a file using ofstream. When i plot these entries with Draw() everything works fine and i get a beautiful hitmap when i print these entries i always get zero unless initializing the variable with something else then i always get something else which means the variable is not touched somehow. Can anyone help me with this issue ? The code i use is shown below.

Cheers
Roger

#include <iostream>
#include <fstream>
using namespace std;
void readOut() {
  TFile *f1 = new TFile("file.root");
  TTree *Content;
  f1->GetObject("tree", Content);

  ofstream myfile;
  myfile.open ("hits.dat");

  Short_t row;
  Short_t col;
  Short_t adc;
  UInt_t id;
  Int_t all;

  TH2F *histo  = new TH2F("histo", "histo", 250,0,251,768,0,769); //name histogram ,N bin, von, bis
  histo->SetTitle("");
  histo->SetXTitle("column");
  histo->SetYTitle("row");

  Content->Draw("Hits.m_row:Hits.m_column>>histo","" ,"");

  Content->SetBranchAddress("Hits.m_row", &row);
  Content->SetBranchAddress("Hits.m_column", &col);
  Content->SetBranchAddress("Hits.m_charge", &adc);
  Content->SetBranchAddress("Hits.m_id", &id);
  
  all = Content->GetEntries();
  for(int i = 0 ; i < all ; i++){
    Content->GetEntry(i);
    cout << row << "\n";
    myfile << row;
    myfile << "\n";
    myfile << col;
    myfile << "\n";
    myfile << adc;
    myfile << "\n";
    myfile << id;
    myfile << "\n";
    histo->Fill(row,col);
  }
  myfile.close();
  
  histo->Draw("colz");
}

Attach your “file.root”.

I think thats not possible since the file is 2GB large sorry. To provide some more information i can only say that the file consists of multiple branches such as Hits with many entries and i just want to access these 4 entries from Hits. The Hits object is defined in a framework outside of root but has TObject.h in its header and can also be accessed via shared library. Still iam confused that Draw() is working but still the entries written to file are zero.

Before “SetBranchAddress”, try to add:

  Content->SetMakeClass(1);
  Content->SetBranchStatus("*", 0);
  Content->SetBranchStatus("Hits.m_row", 1);
  Content->SetBranchStatus("Hits.m_column", 1);
  Content->SetBranchStatus("Hits.m_charge", 1);
  Content->SetBranchStatus("Hits.m_id", 1);

This helped a little more now my output on the root shell is :

root [0] .x root_macros/readOut.C
Info in TCanvas::MakeDefCanvas: created default TCanvas with name c1
0
0
0
0
174

*** Break *** segmentation violation

174 is a valid number for the row entry so this looks fine. But still the four entries before are all zero and after one loop i get an segmentation violation.

I think, the best for you would be to try an “analysis skeleton”. See, for example, links in: How are multiple TTree->Draw()s done?

Ok thanks! I’ll have a look at this.