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