ATLAS Offline Software
AlMat.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "GaudiKernel/StatusCode.h"
6 
11 
12 #include <algorithm>
13 #include <cstdint>
14 #include <exception>
15 #include <fstream>
16 #include <iomanip>
17 #include <sstream>
18 
19 namespace Trk {
20 
22  : m_ncol(0)
23  , m_nrow(0)
24  , m_nele(0)
25  , m_pathbin("./")
26  , m_pathtxt("./")
27  , m_ptr_data(nullptr)
28  , m_transpose(false)
29 {
30 }
31 
32 AlMat::AlMat(int N, int M)
33  : m_ncol(M)
34  , m_nrow(N)
35  , m_nele(N * M)
36  , m_pathbin("./")
37  , m_pathtxt("./")
38  , m_ptr_data(new double[m_nele])
39  , m_transpose(false)
40 {
41 
42  double* p = m_ptr_data + m_nele;
43  while (p > m_ptr_data) *(--p) = 0.;
44 }
45 
47  : m_ncol(m.ncol())
48  , m_nrow(m.nrow())
49  , m_nele(m_nrow * m_ncol)
50  , m_pathbin(m.m_pathbin)
51  , m_pathtxt(m.m_pathtxt)
52  , m_ptr_data(new double[m_nele])
53  , m_transpose(false)
54 {
55 
56  copy(m);
57 }
58 
60 {if( m_ptr_data != nullptr ) delete [] m_ptr_data;}
61 
62 void AlMat::copy(const AlMat& m) {
63  int nr = nrow();
64  int nc = ncol();
65  if( nr != m.nrow() || nc != m.ncol() ) {
66  throw std::range_error( "AlMat::copy: sizes do not match!" );
67  }
68 
69  for( int i=0; i<nr; i++ ) {
70  for( int j=0; j<nc; j++ ) {
71  elemr(i, j) = m.elemc(i, j);
72  }
73  }
74 }
75 
76 void AlMat::copy(const AlSymMatBase& m) {
77  long int n = m.size();
78  if( nrow() != n || ncol() != n ) {
79  throw std::range_error( "AlMat::copy: sizes do not match!" );
80  }
81 
82  for( int i=0; i<n; i++ ) {
83  for( int j=0; j<n; j++ ) {
84  *(m_ptr_data+i*m_ncol+j) = m.elemc(i, j);
85  }
86  }
87 }
88 
89 double& AlMat::elemr(long int i,long int j) {
90 #ifdef _DEBUG
91  if( i<0 ) { throw std::out_of_range( "AlMat::elemr: Index 1 < zero! " ); };
92  if( i>=size ) { throw std::out_of_range( "AlMat::elemr: Index 1 too large!" ); };
93  if( j<0 ) { throw std::out_of_range( "AlMat::elemr: Index 2 < zero! " ); };
94  if( j>=size ) { throw std::out_of_range( "AlMat::elemr: Index 2 too large!" ); };
95 #endif
96 
97  if( m_transpose ) return *(m_ptr_data+j*m_ncol+i);
98  return *(m_ptr_data+i*m_ncol+j);
99 }
100 
101 double AlMat::elemc(long int i,long int j) const {
102 #ifdef _DEBUG
103  if( i<0 ) { throw std::out_of_range( "AlMat::elemr: Index 1 < zero! " ); };
104  if( i>=size ) { throw std::out_of_range( "AlMat::elemr: Index 1 too large!" ); };
105  if( j<0 ) { throw std::out_of_range( "AlMat::elemr: Index 2 < zero! " ); };
106  if( j>=size ) { throw std::out_of_range( "AlMat::elemr: Index 2 too large!" ); };
107 #endif
108 
109  if( m_transpose ) return *(m_ptr_data+j*m_ncol+i);
110  return *(m_ptr_data+i*m_ncol+j);
111 }
112 
113 long int AlMat::elem(long int i,long int j) const {
114 #ifdef _DEBUG
115  if( i<0 ) { throw std::out_of_range( "AlMat::elemr: Index 1 < zero! " ); };
116  if( i>=size ) { throw std::out_of_range( "AlMat::elemr: Index 1 too large!" ); };
117  if( j<0 ) { throw std::out_of_range( "AlMat::elemr: Index 2 < zero! " ); };
118  if( j>=size ) { throw std::out_of_range( "AlMat::elemr: Index 2 too large!" ); };
119 #endif
120 
121  if( m_transpose ) return (j*m_ncol+i);
122  return (i*m_ncol+j);
123 }
124 
125 
126 AlMat& AlMat::operator=(const double& d) {
127 
128  double* p = m_ptr_data + m_nele;
129  while (p > m_ptr_data) *(--p) = d;
130 
131  return *this;
132 }
133 
135  if (this==&m) return *this;
136 
137  if(( m_nrow!=0 || m_ncol!=0) && (m_nrow != m.nrow() || m_ncol != m.ncol() ))
138  {
139  throw std::range_error( "AlMat=AlMat Assignment: size do not match!" );
140  }
141 
142  if ( m_ptr_data != m.m_ptr_data ) {
143  reSize(m.nrow(), m.ncol());
144  copy(m);
145  };
146  return (*this);
147 }
148 
150  if( ( m_nrow!=0 || m_ncol!=0) && (m_nrow != m.size() || m_ncol != m.size() ))
151  {
152  throw std::range_error( "AlMat=AlSymMatBase Assignment: size do not match!" );
153  }
154 
155  if ( m_ptr_data != m.ptrData() ) {
156  reSize(m.size(), m.size());
157  copy(m);
158  }
159  return (*this);
160 }
161 
162 AlMat AlMat::operator+(const AlMat& m) const {
163  if( m_nrow != m.m_nrow || m_ncol != m.m_ncol )
164  {
165  throw std::range_error( "AlMat: operator+: size do not match!" );
166  }
167 
168  AlMat b( m_nrow, m_ncol );
169 
170  double* p = m_ptr_data + m_nele;
171  double* q = m.m_ptr_data + m_nele;
172  double* r = b.m_ptr_data + m_nele;
173  while (p > m_ptr_data) *(--r) = (*(--p))+(*(--q));
174 
175  return b;
176 }
177 
179 
180  if( m_nrow != m.m_nrow || m_ncol != m.m_ncol )
181  {
182  throw std::range_error( "AlMat: operator+=: size do not match!" );
183  }
184 
185  double* p = m_ptr_data + m_nele;
186  double* q = m.m_ptr_data + m_nele;
187  while (p > m_ptr_data) *(--p) += *(--q);
188 
189  return *this;
190 }
191 
192 AlMat AlMat::operator-(const AlMat& m) const {
193  if( m_nrow != m.m_nrow || m_ncol != m.m_ncol )
194  {
195  throw std::range_error( "AlMat: operator-: size do not match!" );
196  }
197 
198  AlMat b( m_nrow, m_ncol );
199 
200  double* p = m_ptr_data + m_nele;
201  double* q = m.m_ptr_data + m_nele;
202  double* r = b.m_ptr_data + m_nele;
203  while (p > m_ptr_data) *(--r) = (*(--p))-(*(--q));
204 
205  return b;
206 }
207 
209  if( m_nrow != m.m_nrow || m_ncol != m.m_ncol )
210  {
211  throw std::range_error( "AlMat: operator-=: size do not match!");
212  }
213 
214  double* p = m_ptr_data + m_nele;
215  double* q = m.m_ptr_data + m_nele;
216  while (p > m_ptr_data) *(--p) -= *(--q);
217 
218  return *this;
219 }
220 
221 AlMat AlMat::operator*(const AlMat& m) const {
222  if( ncol() != m.nrow() )
223  {
224  throw std::range_error( "AlMat: operator*: size do not match!" );
225  }
226 
227  int k(nrow());
228  int l(m.ncol());
229  int f(ncol());
230 
231  AlMat b( k, l);
232  for( int i=0; i<k; i++ ) {
233  for( int j=0; j<l; j++ ) {
234  for( int n=0; n<f; n++) b.elemr(i, j) += elemc(i, n)*m.elemc(n, j);
235  }
236  }
237  return b;
238 }
239 
241  if( ncol() != m.size())
242  {
243  throw std::range_error( "AlMat: operator*: size do not match!" );
244  }
245 
246  int k(nrow());
247  int l(m.size());
248 
249  AlMat b( k, l);
250  for( int i=0; i<k; i++ ) {
251  for( int j=0; j<l; j++ ) {
252  for( int n=0; n<l; n++) b.elemr(i, j) += elemc(i, n)*m.elemc(n, j);
253  }
254  }
255  return b;
256 }
257 
258 
259 AlVec AlMat::operator*(const AlVec& v) const {
260  if( ncol() != v.size() )
261  {
262  throw std::range_error( "AlMat: operator*: size do not match! " );
263  }
264 
265  int k(nrow());
266  int l(ncol());
267  double* p;
268  double* q;
269 
270  AlVec b(k);
271  for( int i=0; i<k; i++ ) {
272  p = b.ptrData()+i;
273  q = m_ptr_data+i*l;
274  for( int j=0; j<l; j++ ) *p += (*(q+j))*(*(v.ptrData()+j));
275  }
276 
277  return b;
278 }
279 
280 AlMat& AlMat::operator*=(const double& d) {
281  double* p = m_ptr_data+m_nele;
282  while (p > m_ptr_data)
283  *(--p) *= d;
284 
285  return *this;
286 }
287 
288 // transposition
289 AlMat AlMat::T() const {
290  AlMat b(m_ncol,m_nrow);
291  for( int i=0; i<b.m_nrow; i++ ) {
292  for( int j=0; j<b.m_ncol; j++ ) {
293  b[i][j]= *(m_ptr_data+j*m_ncol+i);
294  }
295  }
296  return b;
297 }
298 
299 // transposition
301 
302  m_transpose = true;
303 
304  return *this;
305 }
306 
307 // normal position
309 
310  m_transpose = false;
311 
312  return *this;
313 }
314 
315 
316 //invert sym matrix declared as non-symetric for convenience
317 void AlMat::invertS(int& ierr, double Norm = 1.) {
318  if(m_nrow!=m_ncol) {
319  throw std::range_error( "AlMat invertS: non-square matrix!" );
320  }
321 
322  AlSymMat b(m_nrow);
323  for( int i=0; i<b.size(); i++ ) {
324  for( int j=0; j<=i; j++ ) {
325  b.elemr(i, j) = elemc(i, j);
326  }
327  }
328 
329  b*=Norm;
330  ierr = b.invert();
331  b*=Norm;
332 
333  if (ierr==0) (*this).copy(b);
334 
335 }
336 
337 // reSize
338 void AlMat::reSize(int Nnew, int Mnew) {
339  if ( Nnew != m_nrow || Mnew != m_ncol ) {
340 
341  double* p = m_ptr_data;
342  m_nele = Nnew*Mnew;
343  m_ptr_data = new double[m_nele];
344  int nrow_old = m_nrow;
345  int ncol_old = m_ncol;
346  m_transpose = false;
347 
348  m_nrow = Nnew;
349  m_ncol = Mnew;
350  int k = m_nrow <= nrow_old ? m_nrow : nrow_old;
351  int l = m_ncol <= ncol_old ? m_ncol : ncol_old;
352 
353  for( int i=0; i<k; i++ ) {
354  for( int j=0; j<l; j++ ) {
355  *(m_ptr_data+i*m_ncol+j) = *(p+i*ncol_old+j);
356  }
357  }
358 
359  delete [] p;
360  }
361 }
362 
363 
365  std::ifstream inmat(filename.c_str(), std::ios::binary);
366  if(inmat.fail())
367  return StatusCode::FAILURE;
368 
369  int32_t mNrow = m_nrow;
370  inmat.read((char*)&mNrow, sizeof (mNrow));
371  int32_t mNcol = m_ncol;
372  inmat.read((char*)&mNcol, sizeof (mNcol));
373 
374  m_nrow=abs(mNrow);
375  m_ncol=abs(mNcol);
377  m_transpose = false;
378 
379  // printf("ALMat::nrow: %d \n",m_nrow);
380  // printf("ALMat::ncol: %d \n",m_ncol);
381  // printf("ALMat::nele: %d \n",m_nele);
382 
383  double melem=0;
384  for(int i=0; i<m_nrow; i++) {
385  for(int j=0; j<m_ncol; j++) {
386  inmat.read((char*)&melem, sizeof (melem));
387  *(m_ptr_data+i*m_ncol+j) = melem;
388  // printf("(%d,%d) = %.16lf \n",i,j,melem);
389  }
390  }
391 
392  inmat.close();
393  return StatusCode::SUCCESS;
394 }
395 
396 //______________________________________________________________________________
397 void AlMat::SetPathBin(const std::string &path)
398 {
399  m_pathbin.assign(path);
400 }
401 
402 //______________________________________________________________________________
403 void AlMat::SetPathTxt(const std::string &path)
404 {
405  m_pathtxt.assign(path);
406 }
407 
408 //______________________________________________________________________________
409 StatusCode AlMat::Write(const std::string &filename, bool binary, unsigned int precision)
410 {
411  std::ofstream outmat;
412 
413  if(binary) {
414  outmat.open((m_pathbin+filename).c_str(), std::ios::binary);
415  if(outmat.fail())
416  return StatusCode::FAILURE;
417 
418  int32_t mNrow = m_nrow;
419  outmat.write((char*)&mNrow, sizeof (mNrow));
420  int32_t mNcol = m_ncol;
421  outmat.write((char*)&mNcol, sizeof (mNcol));
422  }
423  else {
424  outmat.open((m_pathtxt+filename).c_str());
425  if(outmat.fail())
426  return StatusCode::FAILURE;
427  outmat.setf(std::ios::fixed);
428  if(precision>0)
429  outmat.setf(std::ios::showpoint);
430  outmat.precision(precision);
431  }
432 
433  double melem=0;
434 
435  for( int i=0; i<m_nrow; i++) {
436  for( int j=0; j<m_ncol; j++) {
437  melem = *(m_ptr_data+i*m_ncol+j);
438  if(binary)
439  outmat.write((char*)&(melem), sizeof (melem));
440  else
441  outmat << " " << std::setw(12) << melem;
442  }
443  if(!binary)
444  outmat << std::endl;
445  }
446 
447  outmat.close();
448  return StatusCode::SUCCESS;
449 }
450 
451 //______________________________________________________________________________
452 const std::string AlMat::Print(const int NColsPerSet)
453 {
454  std::ostringstream textmatrix;
455  int FirstCol = 0;
456  int LastCol = NColsPerSet;
457 
458  int NSets = (ncol()/NColsPerSet);
459  if (ncol()%NColsPerSet>0) NSets++;
460  textmatrix << std::endl;
461  textmatrix << " ++++++++++++++ AlMat ++ "
462  << nrow()
463  << " x "
464  << ncol()
465  << " +++++++++++++++++++++++++++++"<< std::endl;
466  for (int set=0; set < NSets; set++) {
467  FirstCol = set*NColsPerSet;
468  LastCol = (set+1)*NColsPerSet;
469  if (LastCol>ncol()) LastCol = ncol();
470  textmatrix << " |row\\col| ";
471  for (int col=FirstCol; col < LastCol; col++) {
472  textmatrix << std::setw(6) << col << " | ";
473  }
474  textmatrix << std::endl;
475  textmatrix << " |-------|";
476  for (int col=FirstCol; col < LastCol; col++) {
477  textmatrix << "--------|";
478  }
479  textmatrix << std::endl;
480  for (int row=0; row<nrow(); row++) {
481  textmatrix << " |" << std::setw(6) << row << " | ";
482  for (int col=FirstCol; col<LastCol; col++) {
483  double melem = *(m_ptr_data+row*ncol()+col);
484  textmatrix << std::setprecision(5) << std:: setw(6) << melem << " | ";
485  }
486  textmatrix << std::endl;
487  }
488  if (set != (NSets-1)) textmatrix << std::endl;
489  }
490  textmatrix << " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<< std::endl;
491  return textmatrix.str();
492 }
493 
494 } // end namespace Trk
495 
Trk::AlMat::operator=
AlMat & operator=(const double &)
Definition: AlMat.cxx:126
query_example.row
row
Definition: query_example.py:24
beamspotman.r
def r
Definition: beamspotman.py:676
python.CaloRecoConfig.f
f
Definition: CaloRecoConfig.py:127
Trk::AlMat::m_pathbin
std::string m_pathbin
Definition: AlMat.h:112
Trk::AlMat::T
AlMat T() const
Definition: AlMat.cxx:289
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
athena.path
path
python interpreter configuration --------------------------------------—
Definition: athena.py:126
Trk::AlMat::AlMat
AlMat()
Definition: AlMat.cxx:21
Trk::AlVec
Definition: AlVec.h:23
AlSpaMat.h
Trk::AlSymMatBase
Definition: AlSymMatBase.h:38
hist_file_dump.d
d
Definition: hist_file_dump.py:137
Trk::AlMat::m_transpose
bool m_transpose
Definition: AlMat.h:115
Trk::AlMat::operator*
AlMat operator*(const AlMat &m) const
Definition: AlMat.cxx:221
CSV_InDetExporter.new
new
Definition: CSV_InDetExporter.py:145
Trk::AlMat::reSize
void reSize(int Nnew, int Mnew)
Definition: AlMat.cxx:338
Trk::AlMat::ReadScalaPack
StatusCode ReadScalaPack(const std::string &)
Definition: AlMat.cxx:364
AlVec.h
UploadAMITag.l
list l
Definition: UploadAMITag.larcaf.py:158
Trk::AlMat::SetPathTxt
void SetPathTxt(const std::string &)
Definition: AlMat.cxx:403
Trk::AlMat::operator+
AlMat operator+(const AlMat &m) const
Definition: AlMat.cxx:162
Trk::AlMat::m_nrow
int m_nrow
Definition: AlMat.h:110
JetTiledMap::N
@ N
Definition: TiledEtaPhiMap.h:44
Trk::AlMat::Print
const std::string Print(const int NColsPerSet=10)
Definition: AlMat.cxx:452
AlSymMat.h
Trk::AlMat::ncol
long int ncol() const
Definition: AlMat.h:162
Trk::AlSymMat
Definition: AlSymMat.h:26
Trk::AlMat::operator+=
AlMat & operator+=(const AlMat &m)
Definition: AlMat.cxx:178
Trk::AlMat::invertS
void invertS(int &ierr, double Norm)
Definition: AlMat.cxx:317
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
Trk::AlMat::Transpose
AlMat & Transpose()
Definition: AlMat.cxx:300
lumiFormat.i
int i
Definition: lumiFormat.py:92
Trk::AlMat::copy
void copy(const AlMat &m)
Definition: AlMat.cxx:62
beamspotman.n
n
Definition: beamspotman.py:731
Trk::AlMat::operator-=
AlMat & operator-=(const AlMat &m)
Definition: AlMat.cxx:208
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
Trk::AlMat::SetPathBin
void SetPathBin(const std::string &)
Definition: AlMat.cxx:397
Trk::AlMat::m_ncol
int m_ncol
Definition: AlMat.h:109
JetVoronoiDiagramHelpers::Norm
Point Norm(const Point &a)
Definition: JetVoronoiDiagramHelpers.cxx:79
AlMat.h
Trk::AlMat
Definition: AlMat.h:27
xAOD::double
double
Definition: CompositeParticle_v1.cxx:159
Trk::AlMat::nrow
long int nrow() const
Definition: AlMat.h:157
CxxUtils::set
constexpr std::enable_if_t< is_bitmask_v< E >, E & > set(E &lhs, E rhs)
Convenience function to set bits in a class enum bitmask.
Definition: bitmask.h:224
Trk
Ensure that the ATLAS eigen extensions are properly loaded.
Definition: FakeTrackBuilder.h:9
Trk::AlMat::Write
StatusCode Write(const std::string &, bool, unsigned int precision=6)
Definition: AlMat.cxx:409
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
Trk::AlMat::m_nele
int m_nele
Definition: AlMat.h:111
Trk::AlMat::elemc
double elemc(long int, long int) const
Definition: AlMat.cxx:101
query_example.col
col
Definition: query_example.py:7
Trk::AlMat::~AlMat
virtual ~AlMat()
Definition: AlMat.cxx:59
Trk::AlMat::m_ptr_data
double * m_ptr_data
Definition: AlMat.h:114
Trk::AlMat::elemr
double & elemr(long int, long int)
Definition: AlMat.cxx:89
Trk::AlMat::Normal
AlMat & Normal()
Definition: AlMat.cxx:308
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
extractSporadic.q
list q
Definition: extractSporadic.py:98
Trk::AlMat::operator-
AlMat operator-(const AlMat &m) const
Definition: AlMat.cxx:192
Trk::AlMat::operator*=
AlMat & operator*=(const double &d)
Definition: AlMat.cxx:280
Trk::AlMat::elem
virtual long int elem(long int, long int) const
Definition: AlMat.cxx:113
plotBeamSpotMon.nc
int nc
Definition: plotBeamSpotMon.py:83
Trk::v
@ v
Definition: ParamDefs.h:84
Trk::AlMat::m_pathtxt
std::string m_pathtxt
Definition: AlMat.h:113
fitman.k
k
Definition: fitman.py:528