Home | News | Documentation | Download

How does one draw TMatrix?

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:


But no idea how to use it. So far what I did:
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!

Perhaps @moneta can help?

1 Like

Hi,

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

{
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

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

  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:

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

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:

 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"); 
1 Like

Thank you I got something now.

What does this exactly do?

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

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:

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(""); 

yes, this will work !