# How to draw all entries in TMatrix

**URL:** <https://root-forum.cern.ch/t/how-to-draw-all-entries-in-tmatrix/45653>\
**Category:** Newbie\
**Tags:** root\
**Created:** [July 2, 2021, 9:19am UTC](https://root-forum.cern.ch/t/how-to-draw-all-entries-in-tmatrix/45653 "2021-07-02T09:19:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karl007](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/8491ac/32.png) [@Karl007](https://root-forum.cern.ch/u/Karl007)\
**Post date:** [July 2, 2021, 9:19am UTC](https://root-forum.cern.ch/t/how-to-draw-all-entries-in-tmatrix/45653/1 "2021-07-02T09:19:02Z")

</div>

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

```auto
  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!

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 2, 2021, 9:36am UTC](https://root-forum.cern.ch/t/how-to-draw-all-entries-in-tmatrix/45653/2 "2021-07-02T09:36:45Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Karl007](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/8491ac/32.png) [@Karl007](https://root-forum.cern.ch/u/Karl007)\
**Post date:** [July 2, 2021, 9:41am UTC](https://root-forum.cern.ch/t/how-to-draw-all-entries-in-tmatrix/45653/3 "2021-07-02T09:41:02Z")

</div>

No problem. Here it is.  
[outputFile.root](https://root-forum.cern.ch/uploads/short-url/vPk7b98zBPpBsG7VPE7rFk3HUlD.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.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 2, 2021, 10:05am UTC](https://root-forum.cern.ch/t/how-to-draw-all-entries-in-tmatrix/45653/4 "2021-07-02T10:05:41Z")

</div>

If you try this:

```auto
   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:

```auto
#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:

```auto
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]

```

---

<div class="post-metadata">

**Author:** ![Karl007](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/8491ac/32.png) [@Karl007](https://root-forum.cern.ch/u/Karl007)\
**Post date:** [July 2, 2021, 10:19am UTC](https://root-forum.cern.ch/t/how-to-draw-all-entries-in-tmatrix/45653/5 "2021-07-02T10:19:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 2, 2021, 10:30am UTC](https://root-forum.cern.ch/t/how-to-draw-all-entries-in-tmatrix/45653/6 "2021-07-02T10:30:01Z")

</div>

Sure, but then you have to divide your canvas

---

<div class="post-metadata">

**Author:** ![Karl007](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/8491ac/32.png) [@Karl007](https://root-forum.cern.ch/u/Karl007)\
**Post date:** [July 2, 2021, 10:41am UTC](https://root-forum.cern.ch/t/how-to-draw-all-entries-in-tmatrix/45653/7 "2021-07-02T10:41:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 2, 2021, 10:42am UTC](https://root-forum.cern.ch/t/how-to-draw-all-entries-in-tmatrix/45653/8 "2021-07-02T10:42:21Z")

</div>

You mean add them, or superimpose?

---

<div class="post-metadata">

**Author:** ![Karl007](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/8491ac/32.png) [@Karl007](https://root-forum.cern.ch/u/Karl007)\
**Post date:** [July 2, 2021, 10:43am UTC](https://root-forum.cern.ch/t/how-to-draw-all-entries-in-tmatrix/45653/9 "2021-07-02T10:43:28Z")

</div>

Indeed superimpose is what I meant.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 2, 2021, 10:56am UTC](https://root-forum.cern.ch/t/how-to-draw-all-entries-in-tmatrix/45653/10 "2021-07-02T10:56:04Z")

</div>

`h->Draw("colz same");`

---

<div class="post-metadata">

**Author:** ![Karl007](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/8491ac/32.png) [@Karl007](https://root-forum.cern.ch/u/Karl007)\
**Post date:** [July 2, 2021, 11:07am UTC](https://root-forum.cern.ch/t/how-to-draw-all-entries-in-tmatrix/45653/11 "2021-07-02T11:07:07Z")

</div>

Great, thanks!
