BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
EmcLSSMatrix.cxx
Go to the documentation of this file.
1//--------------------------------------------------------------------------
2//
3// Description:
4// Class EmcLSSMatrix - Implementation of a Large Sparse Symmetric Matrix
5//
6// Environment:
7// Software developed for the BESIII Detector at the BEPCII.
8//
9// Author List:
10// Chunxiu Liu
11//
12// Copyright Information:
13// Copyright (C) 2005 IHEP
14//
15//------------------------------------------------------------------------
16
17//-----------------------
18// This Class's Header --
19//-----------------------
20#include "EmcLSSMatrix.h"
21
22//-------------
23// C Headers --
24//-------------
25extern "C" {}
26
27//---------------
28// C++ Headers --
29//---------------
30
31#include <fstream>
32
33using namespace std;
34
35// ----------------------------------------
36// -- Public Function Member Definitions --
37// ----------------------------------------
38
39//----------------
40// Constructors --
41//----------------
43 : _size( 0 )
44 , _nrrows( 0 )
45 , _nrcol( 0 )
46 , _matrix( 0 )
47 , _columns( 0 )
48 , _rows( 0 )
49 , _nothing( 0 )
50 , _verb( false ) {}
51
52EmcLSSMatrix::EmcLSSMatrix( int rows, int nonzero_col )
53 : _size( rows * nonzero_col ), _nrrows( rows ), _nrcol( nonzero_col ) {
54 _matrix = new double[_size];
55 _rows = new int[_size];
56 _columns = new int[_size];
57 for ( int i = 0; i < _size; i++ )
58 {
59 _matrix[i] = 0.;
60 _rows[i] = -1;
61 _columns[i] = -1;
62 }
63 _nothing = new double;
64 _verb = false;
65
66 _MsgFlag = 0;
67}
68
69EmcLSSMatrix::EmcLSSMatrix( int rows, int nonzero_col, int MsgFlag )
70 : _size( rows * nonzero_col ), _nrrows( rows ), _nrcol( nonzero_col ) {
71 _matrix = new double[_size];
72 _rows = new int[_size];
73 _columns = new int[_size];
74 for ( int i = 0; i < _size; i++ )
75 {
76 _matrix[i] = 0.;
77 _rows[i] = -1;
78 _columns[i] = -1;
79 }
80 _nothing = new double;
81 _verb = false;
82
83 _MsgFlag = MsgFlag;
84}
85
87 : _size( m1._size )
88 , _nrrows( m1._nrrows )
89 , _nrcol( m1._nrcol )
90
91{
92 _matrix = new double[_size];
93 _rows = new int[_size];
94 _columns = new int[_size];
95 _nothing = m1._nothing;
96 _verb = m1._verb;
97
98 for ( long int i = 0; i < _size; i++ )
99 {
100 _matrix[i] = m1._matrix[i];
101 _rows[i] = m1._rows[i];
102 _columns[i] = m1._columns[i];
103 };
104}
105
106//--------------
107// Destructor --
108//--------------
110 if ( 0 != _matrix )
111 {
112 delete[] _matrix;
113 _matrix = 0;
114 }
115 if ( 0 != _rows )
116 {
117 delete[] _rows;
118 _rows = 0;
119 }
120 if ( 0 != _columns )
121 {
122 delete[] _columns;
123 _columns = 0;
124 }
125 if ( 0 != _nothing )
126 {
127 delete _nothing;
128 _nothing = 0;
129 }
130}
131
132//-------------
133// Operators --
134//-------------
135double& EmcLSSMatrix::operator()( int row, int col ) {
136
137 long int found = find( row, col );
138
139 if ( -1 == found )
140 {
141 if ( _MsgFlag <= 5 )
142 {
143 std::cout << "EmcLSSMatrix:: ERROR "
144 << "EmcLSSMatrix: matrix element not found !!" << endl
145 << "EmcLSSMatrix: Return zero !" << endl;
146 }
147 _nothing = 0;
148 return *_nothing;
149 }
150
151 return ( *( _matrix + found ) );
152}
153
154//-------------
155// Selectors --
156//-------------
157long int EmcLSSMatrix::find( int row, int col ) {
158 int smaller, larger;
159
160 if ( col > row )
161 {
162 smaller = row;
163 larger = col;
164 }
165 else
166 {
167 smaller = col;
168 larger = row;
169 }
170
171 int* col_p;
172 int* row_p;
173 double* matr_p;
174 double* matr_row_max;
175
176 if ( larger < 0 || larger > _nrrows || smaller < 0 || smaller > _nrrows )
177 {
178 if ( _MsgFlag <= 5 )
179 {
180 std::cout << "EmcLSSMatrix::ERROR"
181 << "!!! ERROR in bound check of EmcLSSMatrix !!!"
182 << "!!! Return zero !!!" << endl;
183 }
184 matr_p = 0;
185 }
186 else
187 {
188 col_p = ( _columns + ( larger * _nrcol ) );
189 row_p = ( _rows + ( larger * _nrcol ) );
190 matr_p = ( _matrix + ( larger * _nrcol ) );
191 matr_row_max = ( matr_p + _nrcol );
192
193 while ( matr_p < matr_row_max )
194 {
195
196 if ( _MsgFlag <= 1 )
197 {
198 std::cout << "EmcLSSMatrix::VERBOSE"
199 << "C: " << larger << " " << smaller << " " << col_p << " " << *col_p << " "
200 << matr_p << " " << *matr_p << " " << ( _matrix + ( matr_p - _matrix ) )
201 << " " << *( _matrix + ( matr_p - _matrix ) ) << endl;
202 }
203 /*
204 cout<< "C: " << larger << " " << smaller << " "
205 << col_p << " " << *col_p << " "
206 << matr_p << " " << *matr_p << " "
207 << (_matrix+(matr_p-_matrix)) << " "
208 << *(_matrix+(matr_p-_matrix)) <<endl;
209 */
210 if ( matr_p == ( matr_row_max - 1 ) )
211 {
212 if ( _MsgFlag <= 4 )
213 {
214 std::cout << "EmcLSSMatrix::WARNING "
215 << "!! WARNING: Reached maximum number of columns "
216 << "in LSSMatrix when searching for row " << larger << " column "
217 << smaller << " !!" << endl
218 << "!! Return zero pointer !! " << endl;
219 }
220 matr_p = 0;
221 break;
222 }
223
224 // if row does already exist
225 if ( *col_p == smaller ) { break; }
226
227 // if at the end of the list, use this as new element
228 if ( ( *matr_p == 0. ) )
229 {
230 *col_p = smaller;
231 *row_p = larger;
232 // if (*row_p == 1616 ) {
233 // nun=(row_p-(_rows + (larger * _nrcol)));
234 // if (_MsgFlag <= 2) {
235 // std::cout <<"EmcLSSMatrix::DEBUG "
236 // << nun << " " << larger << " " << smaller << " "
237 // << *row_p << " " << *col_p << " " << *matr_p << endl;
238 //}
239 // }
240 break;
241 }
242
243 matr_p++;
244 col_p++;
245 row_p++;
246 }
247 }
248
249 long int diff = matr_p - _matrix;
250
251 if ( matr_p == 0 ) { diff = -1; }
252
253 return ( diff );
254}
255
256double* EmcLSSMatrix::matrix( const int& rowind ) const {
257 double* here = 0;
258
259 if ( rowind < _nrrows ) { here = ( _matrix + ( rowind * _nrcol ) ); }
260 else
261 {
262 if ( _MsgFlag <= 5 )
263 {
264 std::cout << "EmcLSSMatrix::ERROR "
265 << "EmcLSSMatrix::matrix: Error "
266 << "- larger row index than existing requested !" << endl;
267 }
268 here = 0;
269 }
270 return here;
271}
272
273int* EmcLSSMatrix::column( const int& rowind ) const {
274 int* here = 0;
275
276 if ( rowind < _nrrows ) { here = ( _columns + ( rowind * _nrcol ) ); }
277 else
278 {
279
280 if ( _MsgFlag <= 5 )
281 {
282 std::cout << "EmcLSSMatrix::ERROR "
283 << "EmcLSSMatrix.column: Error "
284 << "- larger row index than existing requested !" << endl;
285 }
286 here = 0;
287 }
288 return here;
289}
290
291int EmcLSSMatrix::num_filled_cols( const int row ) const {
292 double* search_i = _matrix + ( row * _nrcol );
293 double* max_i = search_i + _nrcol;
294 int nonZeroCol = 0;
295
296 while ( ( *search_i != 0. ) && ( search_i < max_i ) )
297 {
298 nonZeroCol++;
299 search_i++;
300 }
301 return nonZeroCol;
302}
303
304int EmcLSSMatrix::num_filled_rows( const int col ) const {
305 int nonZeroRows = 0;
306 for ( long int i = 0; i < _size; i++ )
307 {
308 if ( ( _matrix[i] != 0. ) && ( _columns[i] == col ) ) { nonZeroRows++; }
309 }
310
311 return nonZeroRows;
312}
313
315 int* col_p = _columns;
316 double* ele_p = _matrix;
317 double* mat_max_p = ( _matrix + _size );
318 long int nrele = 0;
319
320 while ( ele_p < mat_max_p )
321 {
322 if ( *ele_p != 0. ) nrele++;
323 col_p++;
324 ele_p++;
325 }
326
327 return nrele;
328}
329
330//-------------
331// Modifiers --
332//-------------
334 for ( int i = 0; i < _size; i++ )
335 {
336 _matrix[i] = 0.;
337 _rows[i] = -1;
338 _columns[i] = -1;
339 }
340}
341
342bool EmcLSSMatrix::reduce_Matrix( int* xRef_list ) {
343 bool successful = true;
344
345 // delete all zero elements in matrix
346 // save only non zero elements
347
348 long int _newIndx = 0;
349
350 for ( long int _arrayIndx = 0; _arrayIndx < _size; _arrayIndx++ )
351 {
352
353 // add 1 to matrix indices because SLAP wants indices 1..N,
354 // but Xtals counting in geometry starts with 0
355
356 if ( _matrix[_arrayIndx] > 0. )
357 {
358
359 if ( ( xRef_list[( _rows[_arrayIndx] )] ) >= 0 &&
360 ( xRef_list[( _columns[_arrayIndx] )] ) >= 0 )
361 {
362 _matrix[_newIndx] = _matrix[_arrayIndx];
363 _rows[_newIndx] = ( ( xRef_list[( _rows[_arrayIndx] )] ) + 1 );
364 _columns[_newIndx] = ( ( xRef_list[( _columns[_arrayIndx] )] ) + 1 );
365 _newIndx++;
366 }
367 else
368 {
369
370 // int indxtal;
371 if ( xRef_list[( _rows[_arrayIndx] )] < 0 )
372 {
373 if ( _MsgFlag <= 5 )
374 {
375 std::cout << "EmcLSSMatrix::ERROR "
376 << "EmcLSSMatrix: Xtal index " << _rows[_arrayIndx]
377 << " appears in matrix, "
378 << "but not in vector !!! " << _rows[_arrayIndx] << " "
379 << _columns[_arrayIndx] << endl;
380 }
381 }
382 else
383 {
384
385 if ( _MsgFlag <= 5 )
386 {
387 std::cout << "EmcLSSMatrix::ERROR "
388 << "EmcLSSMatrix: Xtal index " << _columns[_arrayIndx]
389 << " appears in matrix, "
390 << "but not in vector !!! " << _rows[_arrayIndx] << " "
391 << _columns[_arrayIndx] << endl;
392 }
393 }
394 successful = false;
395 }
396 }
397 }
398
399 if ( _verb )
400 {
401 if ( _MsgFlag <= 2 )
402 {
403 std::cout << "EmcLSSMatrix::DEBUG "
404 << "Reduced LSSMatrix !!! Number of non zeros: " << _newIndx << endl;
405 }
406 }
407
408 return successful;
409}
410
412 int* col_p = _columns;
413 int* row_p = _rows;
414 double* ele_p = _matrix;
415 double* mat_max_p = ( _matrix + _size );
416 long int wo = 0;
417 long int nrele = 0;
418
419 if ( _MsgFlag <= 2 )
420 {
421 std::cout << "EmcLSSMatrix::DEBUG "
422 << "List of nonzero Matrix elements (Matrix size: " << _size << " ) : " << endl;
423 }
424
425 while ( ele_p < mat_max_p )
426 {
427 if ( *ele_p != 0. )
428 {
429 nrele++;
430 if ( _MsgFlag <= 2 )
431 {
432 std::cout << "EmcLSSMatrix::DEBUG "
433 << "nr: " << nrele << " M( " << *row_p << " , " << *col_p
434 << " ): " << *ele_p << " array index: " << wo << endl;
435 }
436 /*
437 cout<< "nr: " << nrele
438 << " M( " << *row_p << " , " << *col_p
439 << " ): " << *ele_p
440 << " array index: " << wo << endl;
441 */
442 }
443 wo++;
444 col_p++;
445 row_p++;
446 ele_p++;
447 }
448}
449
450void EmcLSSMatrix::print_row( int rownr ) {
451 int* col_p = _columns + ( _nrcol * rownr );
452 int* row_p = _rows + ( _nrcol * rownr );
453 double* ele_p = _matrix + ( _nrcol * rownr );
454 double* mat_max_p = ( ele_p + _nrcol );
455 long int wo = 0;
456 long int nrele = 0;
457
458 if ( _MsgFlag <= 2 )
459 {
460 std::cout << "EmcLSSMatrix::DEBUG "
461 << "row length: " << num_filled_cols( rownr ) << endl;
462 }
463 while ( ele_p < mat_max_p )
464 {
465 if ( *ele_p != 0. )
466 {
467 nrele++;
468 if ( _MsgFlag <= 2 )
469 {
470 std::cout << "EmcLSSMatrix::DEBUG "
471 << "nr: " << nrele << " M( " << *row_p << " , " << *col_p
472 << " ): " << *ele_p << " array index: " << wo << endl;
473 }
474 /*
475 cout<< "nr: " << nrele
476 << " M( " << *row_p << " , " << *col_p
477 << " ): " << *ele_p
478 << " array index: " << wo << endl;
479 */
480 }
481 wo++;
482 col_p++;
483 row_p++;
484 ele_p++;
485 }
486}
487
488void EmcLSSMatrix::writeOut( ostream& Out ) {
489 int* col_p = _columns;
490 int* row_p = _rows;
491 double* ele_p = _matrix;
492 double* mat_max_p = ( _matrix + _size );
493 // long int nrele = 0;
494
495 long int nonz = num_nonZeros();
496 Out << nonz << " ";
497
498 while ( ele_p < mat_max_p )
499 {
500 if ( *ele_p != 0. ) { Out << *row_p << " " << *col_p << " " << *ele_p << " "; }
501 col_p++;
502 row_p++;
503 ele_p++;
504 }
505
506 if ( _MsgFlag <= 2 )
507 {
508 std::cout << "EmcLSSMatrix::DEBUG "
509 << "Wrote " << nonz << " non zero matrix elements to file !!!" << endl;
510 }
511}
512
513void EmcLSSMatrix::readIn( istream& In ) {
514 // long int nonz = num_nonZeros();
515 long int nonz = 0;
516 In >> nonz;
517
518 cout << "nonz=" << nonz << endl;
519
520 int theRow;
521 int theCol;
522 long int index;
523 double theEle;
524
525 for ( long int i = 0; i < nonz; i++ )
526 {
527 In >> theRow >> theCol >> theEle;
528 index = find( theRow, theCol );
529 /*
530 cout<<"index = "<<index
531 <<"row="<<theRow
532 <<"col="<<theCol<<endl;
533 */
534 if ( -1 != index )
535 {
536 _matrix[index] += theEle;
537 if ( _verb )
538 {
539 if ( i < 50 || i > ( nonz - 10 ) )
540 {
541 if ( _MsgFlag <= 2 )
542 {
543 std::cout << "EmcLSSMatrix::DEBUG "
544 << "M: " << _rows[index] << " " << _columns[index] << " "
545 << _matrix[index] << " " << index << endl;
546 }
547 }
548 }
549 }
550 }
551
552 if ( _verb )
553 {
554 if ( _MsgFlag <= 2 )
555 {
556 std::cout << "EmcLSSMatrix::DEBUG "
557 << "Have read in " << nonz << " non zero matrix elements from file !!!"
558 << endl;
559 }
560 }
561}
int num_filled_cols(const int row) const
bool reduce_Matrix(int *xRef_list)
int * row() const
long int num_nonZeros()
long int find(int row, int col)
void print_NonZeros()
void print_row(int rownr)
int * column(const int &rowind=0) const
void writeOut(ostream &Out)
double & operator()(int row, int col)
void readIn(istream &In)
double * matrix(const int &rowind=0) const
int num_filled_rows(const int col) const
double * m1
Definition qcdloop1.h:83