# How does one draw TMatrix?

**URL:** <https://root-forum.cern.ch/t/how-does-one-draw-tmatrix/42266>\
**Category:** Newbie\
**Tags:** tree, hist, root\
**Created:** [November 12, 2020, 2:57pm UTC](https://root-forum.cern.ch/t/how-does-one-draw-tmatrix/42266 "2020-11-12T14:57:14Z")\
**Posts on this page:** 9\
**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:** [November 12, 2020, 2:57pm UTC](https://root-forum.cern.ch/t/how-does-one-draw-tmatrix/42266/1 "2020-11-12T14:57:14Z")

</div>

I have a tree with four branches. The branches are saved in TMatrixD form, this is why normal TreeViewer doesn’t work in that case. Anyway, I would like to have 4 histograms for these 4 branches. I came across this:

> **[ROOT: math/matrix/src/TMatrixTBase.cxx Source File](https://root.cern/doc/master/TMatrixTBase_8cxx_source.html#l00623)**

  
But no idea how to use it. So far what I did:

```auto
void tmatrix() {
   
   TH1F *h1 = new TH1F("h1", "induced charge", 200,-250,250);
   
   TFile* outFile = new TFile("root_files/out_files/outputFile.root");
   TTree* outTree = (TTree*)outFile->Get("out_tree");
   Int_t dentries = outTree->GetEntries();
   cout<<"Number of event : "<<dentries<<endl;
   outTree->GetBranch("PadCharge"); // PadCharge is the branch I wanna draw
   h1->Draw(....)

Thank you!

```

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [November 12, 2020, 8:15pm UTC](https://root-forum.cern.ch/t/how-does-one-draw-tmatrix/42266/2 "2020-11-12T20:15:03Z")

</div>

Perhaps @moneta can help?

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [November 13, 2020, 10:45am UTC](https://root-forum.cern.ch/t/how-does-one-draw-tmatrix/42266/3 "2020-11-13T10:45:48Z")

</div>

Hi,

You can draw a TMatrix. Here is an example drawing the correlation matrix from a fit:

```auto
{
auto h1 = new TH1D("h","h",100,-3,3);
h1->FillRandom("gaus");
auto r = h->Fit("gaus","S");
auto corrMatrix = r->GetCorrelationMatrix();
corrMatrix.Draw("COLZ");
corrMatrix.Print();
}

```

Lorenzo

---

<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:** [November 13, 2020, 1:00pm UTC](https://root-forum.cern.ch/t/how-does-one-draw-tmatrix/42266/4 "2020-11-13T13:00:07Z")

</div>

Thanks for the answer. That gives an idea but still not sure how it works in my case. I tried:

```auto
  auto h1 = new TH1D("h1", "induced charge", 200,-250,250);
  TFile* outFile = new TFile("root_files/out_files/outputFile.root");
  TTree* outTree = (TTree*)outFile->Get("out_tree");
  Int_t dentries = outTree->GetEntries();
  cout<<"Number of entries : "<<dentries<<endl;
  
  padCharge = outTree->GetBranch("PadCharge");
  
  h1->Fill(padCharge);
  auto r = h1->Fit("gaus","S");
  auto corrMatrix = r->GetCorrelationMatrix();
  corrMatrix.Draw("COLZ");
  corrMatrix.Print();

```

But I’m getting:

```auto
error: use of undeclared identifier 'padCharge'
  h1->Fill(padCharge);

```

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [November 13, 2020, 1:45pm UTC](https://root-forum.cern.ch/t/how-does-one-draw-tmatrix/42266/5 "2020-11-13T13:45:00Z")

</div>

Hi,

Please explain your problem better .You have a branch `padCharge` saved in a Three as aTMatrixD and you would like to read it and display it ? Would you like to convert also in a 2D histogram ?

For reading from a Three you can do something like:

```auto
 TFile* outFile = new TFile("root_files/out_files/outputFile.root");
 TTree* outTree = (TTree*)outFile->Get("out_tree");
outTree->Print(); // print tree to be sure of its content
TMatrixD *matrix = nullptr;
outTree->SetBranchAddress("PadCharge", &matrix);
Long64_t nentries = outTree->GetEntries();
// this will draw matrix of first event
outTree->GetEntry(0);
matrix->Draw("COLZ"); 

```

---

<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:** [November 13, 2020, 2:29pm UTC](https://root-forum.cern.ch/t/how-does-one-draw-tmatrix/42266/6 "2020-11-13T14:29:17Z")

</div>

Thank you I got something now.

> [@moneta](#):
>
> `&matrix`

What does this exactly do?

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [November 13, 2020, 2:33pm UTC](https://root-forum.cern.ch/t/how-does-one-draw-tmatrix/42266/7 "2020-11-13T14:33:15Z")

</div>

Hi,  
This statement `&matrix` is for getting the pointer to the variable `matrix`.  
Now in the code above `matrix` is a pointer to `TMatrix`, then `&matrix` is a pointer to the pointer of TMatrix, i.e a TMatrix \*\*. See for example

> **[C++ Pointer to Pointer (Multiple Indirection) - Tutorialspoint](https://www.tutorialspoint.com/cplusplus/cpp_pointer_to_pointer.htm)**

---

<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:** [November 13, 2020, 2:39pm UTC](https://root-forum.cern.ch/t/how-does-one-draw-tmatrix/42266/8 "2020-11-13T14:39:11Z")

</div>

Oh ok it makes sense. Would you follow the same logic draw a vector as well? As you know branches 3 and 4 are vectors, so maybe something like:

```auto
outTree->Print(); // print tree to be sure of its content
TVectorD *vector= nullptr;
outTree->SetBranchAddress("SilCharge", &vector);
Long64_t nentries = outTree->GetEntries();

outTree->GetEntry(7);
vector->Draw(""); 

```

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [November 13, 2020, 3:10pm UTC](https://root-forum.cern.ch/t/how-does-one-draw-tmatrix/42266/9 "2020-11-13T15:10:48Z")

</div>

yes, this will work !
