Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TGDMLMatrix.cxx
Go to the documentation of this file.
1 // @(#)root/gdml:$Id$
2 // Author: Andrei Gheata 05/12/2018
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2011, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /** \class TGDMLMatrix
13 \ingroup Geometry_gdml
14  This class is used in the process of reading and writing the GDML "matrix" tag.
15 It represents a matrix with arbitrary number of rows and columns, storing elements
16 in double precision.
17 */
18 
19 #include "TGDMLMatrix.h"
20 
21 #include <cassert>
22 
23 ClassImp(TGDMLMatrix)
24 
25 //_____________________________________________________________________________
26 TGDMLMatrix::TGDMLMatrix(const char *name, size_t rows,size_t cols)
27  : TNamed(name, "")
28 {
29 // Constructor
30  if ((rows <= 0) || (cols <= 0))
31  {
32  Fatal("TGDMLMatrix::TGDMLMatrix(rows,cols)", "Wrong number of rows/cols");
33  }
34  fNrows = rows;
35  fNcols = cols;
36  fNelem = rows * cols;
37  fMatrix = new Double_t[fNelem];
38 }
39 
40 //_____________________________________________________________________________
41 TGDMLMatrix::TGDMLMatrix(const TGDMLMatrix& rhs)
42  : TNamed(rhs), fNelem(rhs.fNelem), fNrows(rhs.fNrows), fNcols(rhs.fNcols), fMatrix(nullptr)
43 {
44 // Copy constructor
45  if (rhs.fMatrix)
46  {
47  fMatrix = new Double_t[fNelem];
48  memcpy(fMatrix, rhs.fMatrix, fNelem * sizeof(Double_t));
49  }
50 }
51 
52 //_____________________________________________________________________________
53 TGDMLMatrix& TGDMLMatrix::operator=(const TGDMLMatrix& rhs)
54 {
55 // Assignment
56  if (this == &rhs) { return *this; }
57  TNamed::operator=(rhs);
58  fNrows = rhs.fNrows;
59  fNcols = rhs.fNcols;
60  fNelem = fNrows * fNcols;
61  if (rhs.fMatrix)
62  {
63  fMatrix = new Double_t[fNelem];
64  memcpy(fMatrix, rhs.fMatrix, fNelem * sizeof(Double_t));
65  }
66  return *this;
67 }
68 
69 //_____________________________________________________________________________
70 void TGDMLMatrix::Set(size_t r, size_t c, Double_t a)
71 {
72  assert(r < fNrows && c < fNcols);
73  fMatrix[fNcols*r+c] = a;
74 }
75 
76 //_____________________________________________________________________________
77 Double_t TGDMLMatrix::Get(size_t r, size_t c) const
78 {
79  assert(r < fNrows && c < fNcols);
80  return fMatrix[fNcols*r+c];
81 }
82 
83 //_____________________________________________________________________________
84 void TGDMLMatrix::Print(Option_t *) const
85 {
86 // Print info about this matrix
87  printf("*** matrix: %-20s coldim = %zu rows = %zu\n", GetName(), fNcols, fNrows);
88  if (!fTitle.IsNull()) {
89  printf(" %s\n", fTitle.Data());
90  return;
91  }
92  for (size_t row = 0; row < fNrows; ++row) {
93  printf(" ");
94  for (size_t col = 0; col < fNcols; ++col) {
95  printf("%8.3g", Get(row, col));
96  }
97  printf("\n");
98  }
99 }