Add a branch holding a TGeoMatrix

Hello,
I’m new to Root and I want to add a branch to tree holding a TGeoMatrix.
I didn’t find an example so i came up with this:

TFile *file = new TFile(“name.root”,“RECREATE”);
TTree *tree = new TTree(“name”," ");
TClonesArray *matrix = new TClonesArray(“TGeoMatrix”, 1);
tree->Branch(“matrix”,&matrix);
.
(parsing geometry)
.
TGeoMatrix *curMatrix = (TGeoMatrix *)matrix->ConstructedAt(0);
curMatrix = gGeoManager->GetCurrentMatrix();
tree->Fill();

I get an error:
input_line_102:5:21: error: allocating an object of abstract class type ‘TGeoMatrix’
*ret = new TGeoMatrix;

Сan someone tell me how to write TGeoMatrix to a tree?
Thanks in advance
Artem

@agheata could you please take a look or maybe suggest who can help here? Thank you!

TGeoMatrix is a base virtual class, so you have to store one of the derived types in a TClonesArray: TGeoTranslation/TGeoRotation/TGeoCombiTrans/TGeoHMatrix. You can do for example:

TClonesArray matrix("TGeoHMatrix", 1000);
for (auto i = 0; i < 1000; ++i) new (matrix[i]) TGeoHMatrix();
matrix[100]->Print();

output:

matrix  - tr=0  rot=0  refl=0  scl=0 shr=0 reg=0 own=0
  1.000000    0.000000    0.000000    Tx =   0.000000
  0.000000    1.000000    0.000000    Ty =   0.000000
  0.000000    0.000000    1.000000    Tz =   0.000000

Then you can do:

 TFile *file = new TFile("f1.root", "RECREATE");
 TTree *tree = new TTree("T"," ");
 tree->Branch("matrix",&matrix);
...
 tree->Fill();
 ...
tree->Write();
2 Likes