How to draw all entries in TMatrix

Hi, I am trying to draw 45 entries in Tmatrix together in one canvas. I am using the following code:

  TFile* outFile = new TFile("myrootfile.root");
  TTree* outTree = (TTree*)outFile->Get("out_tree");
  outTree->Print(); // print tree to be sure of its content
  TMatrixD *matrix1 = nullptr;
  outTree->SetBranchAddress("myBranch", &matrix1);
  Long64_t events = outTree->GetEntries();
  cout<<"Number of events : "<<events<<endl;

 for(int i=0; i<events; i++){
	  outTree->GetEntry(i);
	  TH2D *h = new TH2D(*matrix1);
	  h->SetNameTitle("h", "matrix;x axis;y axis");
	  h->Draw("colz");
	  h->SetStats(0);
  
  }

But I am only getting one entry (event) instead of all of them on the same canvas. Is it possible?

Thank you!

If you recreate the same TH2D and call Draw() inside the loop, then you’ll see only the last one…
EDIT: Sorry, I overlooked at your code. Can you provide the root file?

No problem. Here it is.
outputFile.root (371.7 KB)

EDIT: can I maybe create something that draws events when I call them in the command line?
For example: typing “1” draws event number 1 and so on. So I don’t have to edit the code every time.

If you try this:

   for (int i=0; i<events; i++) {
      cout<<"Reading Event "<<i<<endl;
      outTree->GetEntry(i);
      TH2D *h = new TH2D(*matrix1);
      h->SetNameTitle("h", "matrix;x axis;y axis");
      h->Draw("colz");
      h->SetStats(0);
      gPad->Modified();
      gPad->Update();
      gSystem->Sleep(1000);
   }

You’ll see the events one after the other.
So you can create a function with parameters, like for example:

#include "TFile.h"
#include "TTree.h"
#include "TMatrixD.h"

void print_info(const char *file_name = "outputFile.root", const char *tree_name = "out_tree")
{
   TFile* outFile = TFile::Open(file_name);
   TTree* outTree = (TTree*)outFile->Get(tree_name);
   outTree->Print(); // print tree to be sure of its content
   outFile->Close();
   delete outFile;
}

void read_output(Int_t entry = 0, const char *file_name = "outputFile.root", const char *branch_name = "PadCharge",
                 const char *tree_name = "out_tree")
{
   TFile* outFile = TFile::Open(file_name);
   TTree* outTree = (TTree*)outFile->Get(tree_name);
   TMatrixD *matrix1 = nullptr;
   outTree->SetBranchAddress(branch_name, &matrix1);
   outTree->GetEntry(entry);
   TH2D *h = new TH2D(*matrix1);
   h->SetDirectory(nullptr);
   h->SetNameTitle("h", "matrix;x axis;y axis");
   h->Draw("colz");
   h->SetStats(0);
   gPad->Modified();
   gPad->Update();
   outFile->Close();
   delete outFile;
}

And then you can use it that way:

C:\Users\bellenot\rootdev\Forum>root -l
root [0] .L read_output.C
root [1] print_info()
******************************************************************************
*Tree    :out_tree  : out_tree                                               *
*Entries :       46 : Total =         1536133 bytes  File  Size =     374839 *
*        :          : Tree compression factor =   4.10                       *
******************************************************************************
*Br    0 :PadCharge : TMatrixD                                               *
*Entries :       46 : Total  Size=     759250 bytes  File Size  =     145942 *
*Baskets :       16 : Basket Size=      64000 bytes  Compression=   5.20     *
*............................................................................*
*Br    1 :PadTime   : TMatrixD                                               *
*Entries :       46 : Total  Size=     759212 bytes  File Size  =     225817 *
*Baskets :       16 : Basket Size=      64000 bytes  Compression=   3.36     *
*............................................................................*
*Br    2 :SilCharge : TVectorD                                               *
*Entries :       46 : Total  Size=       8634 bytes  File Size  =       1393 *
*Baskets :        1 : Basket Size=      64000 bytes  Compression=   5.84     *
*............................................................................*
*Br    3 :SilID     : TVectorD                                               *
*Entries :       46 : Total  Size=       8618 bytes  File Size  =        691 *
*Baskets :        1 : Basket Size=      64000 bytes  Compression=  11.77     *
*............................................................................*
root [2] read_output(2)
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
root [3]
1 Like

Thanks. And is it also possible to show all in one canvas. Maybe read_output(all) or so?

Sure, but then you have to divide your canvas

Yes, dividing is one way but is it possible to see all events together on one histogram?

You mean add them, or superimpose?

Indeed superimpose is what I meant.

h->Draw("colz same");

1 Like

Great, thanks!