NCFS-Pack
A generic (astro)particle physics analysis framework
 
Loading...
Searching...
No Matches
NcObjMatrix.cxx
Go to the documentation of this file.
1
31
33
96
97#include "NcObjMatrix.h"
98#include "Riostream.h"
99
100ClassImp(NcObjMatrix); // Class implementation to enable ROOT I/O
101
104{
113
114 fRows=0;
115 fOwn=0;
116 fSwap=0;
117 fMaxrow=0;
118 fMaxcol=0;
119 fObjects=0;
120}
121
123{
129
130 if (fRows)
131 {
132 delete fRows;
133 fRows=0;
134 }
135 if (fObjects)
136 {
137 delete fObjects;
138 fObjects=0;
139 }
140}
141
143{
149
150 fRows=0;
151 fMaxrow=0;
152 fMaxcol=0;
153 fObjects=0;
154
155 fOwn=m.fOwn;
156 fSwap=m.fSwap;
157
158 Int_t maxrow=m.GetMaxRow();
159 Int_t maxcol=m.GetMaxColumn();
160 for (Int_t irow=1; irow<=maxrow; irow++)
161 {
162 for (Int_t icol=1; icol<=maxcol; icol++)
163 {
164 TObject* obj=m.GetObject(irow,icol);
165 if (obj)
166 {
167 if (!fOwn)
168 {
169 EnterObject(irow,icol,obj);
170 }
171 else
172 {
173 EnterObject(irow,icol,obj->Clone());
174 }
175 }
176 }
177 }
178}
179
181{
190
191 if (fRows)
192 {
193 delete fRows;
194 fRows=0;
195 }
196 if (fObjects)
197 {
198 delete fObjects;
199 fObjects=0;
200 }
201
202 fMaxrow=0;
203 fMaxcol=0;
204}
205
207{
216
217 fOwn=own;
218
219 if (!fRows) return;
220
221 for (Int_t irow=0; irow<fRows->GetSize(); irow++)
222 {
223 TObjArray* mrow=(TObjArray*)fRows->At(irow);
224 if (mrow)
225 {
226 if (own)
227 {
228 mrow->SetOwner(kTRUE);
229 }
230 else
231 {
232 mrow->SetOwner(kFALSE);
233 }
234 }
235 }
236}
237
239{
245
246 return fOwn;
247}
248
250{
266
267 if (!fRows)
268 {
269 fSwap=swap;
270 }
271 else
272 {
273 cout << " *NcObjMatrix::SetSwapMode* Matrix not empty ==> No action." << endl;
274 }
275}
276
278{
284
285 return fSwap;
286}
287
288void NcObjMatrix::EnterObject(Int_t row,Int_t col,TObject* obj)
289{
300
301 if (row<1 || col<1)
302 {
303 cout << " *NcObjMatrix::AddObject* Invalid argument(s) (row,col) : ("
304 << row << "," << col << ")" << endl;
305 return;
306 }
307
308 if (row>fMaxrow) fMaxrow=row;
309 if (col>fMaxcol) fMaxcol=col;
310
311 Int_t rowx=row;
312 if (fSwap) rowx=col;
313 Int_t colx=col;
314 if (fSwap) colx=row;
315
316 if (!fRows)
317 {
318 fRows=new TObjArray(rowx);
319 fRows->SetOwner();
320 }
321 else
322 {
323 if (rowx > fRows->GetSize()) fRows->Expand(rowx);
324 }
325
326 TObjArray* mrow=(TObjArray*)fRows->At(rowx-1);
327
328 if (!mrow)
329 {
330 TObjArray* columns=new TObjArray(colx);
331 if (fOwn) columns->SetOwner();
332 fRows->AddAt(columns,rowx-1);
333 mrow=columns;
334 }
335 else
336 {
337 if (colx > mrow->GetSize()) mrow->Expand(colx);
338 }
339
340 TObject* old=(TObject*)mrow->At(colx-1);
341 if (old)
342 {
343 fObjects->Remove(old);
344 fObjects->Compress();
345 if (fOwn) delete old;
346 }
347
348 mrow->AddAt(obj,colx-1);
349
350 if (!fObjects) fObjects=new TObjArray();
351 fObjects->Add(obj);
352}
353
354void NcObjMatrix::RemoveObject(Int_t row,Int_t col)
355{
364
365 TObject* obj=0;
366
367 if (!fRows || row<1 || col<1) return;
368
369
370 Int_t rowx=row;
371 if (fSwap) rowx=col;
372 Int_t colx=col;
373 if (fSwap) colx=row;
374
375 TObjArray* mrow=0;
376 if (rowx <= fRows->GetSize()) mrow=(TObjArray*)fRows->At(rowx-1);
377
378 if (!mrow) return;
379
380 if (colx <= mrow->GetSize()) obj=(TObject*)mrow->At(colx-1);
381
382 if (obj)
383 {
384 fObjects->Remove(obj);
385 fObjects->Compress();
386 mrow->Remove(obj);
387 if (fOwn) delete obj;
388 }
389}
390
391void NcObjMatrix::RemoveObjects(TObject* obj,Int_t row,Int_t col)
392{
428
429 TArrayI rows;
430 TArrayI cols;
431 Int_t nrefs=0;
432
433 if (row && col)
434 {
435 if (!obj)
436 {
437 RemoveObject(row,col);
438 }
439 else
440 {
441 TObject* objx=GetObject(row,col);
442 if (objx==obj) RemoveObject(row,col);
443 }
444 return;
445 }
446
447 if (!row && !col)
448 {
449 if (!obj)
450 {
451 Reset();
452 return;
453 }
454 else
455 {
456 nrefs=GetIndices(obj,rows,cols);
457 }
458 }
459
460 if (row && !col) nrefs=GetIndices(obj,row,cols);
461 if (!row && col) nrefs=GetIndices(obj,rows,col);
462
463 // Remove the selected objects based on the obtained row and column indices
464 Int_t irow,icol;
465 for (Int_t i=0; i<nrefs; i++)
466 {
467 irow=row;
468 if (!irow) irow=rows.At(i);
469 icol=col;
470 if (!icol) icol=cols.At(i);
471 RemoveObject(irow,icol);
472 }
473}
474
475TObject* NcObjMatrix::GetObject(Int_t row,Int_t col) const
476{
485
486 TObject* obj=0;
487
488 if (!fRows || row<1 || col<1) return obj;
489
490
491 Int_t rowx=row;
492 if (fSwap) rowx=col;
493 Int_t colx=col;
494 if (fSwap) colx=row;
495
496 TObjArray* mrow=0;
497 if (rowx <= fRows->GetSize()) mrow=(TObjArray*)fRows->At(rowx-1);
498
499 if (!mrow) return obj;
500
501 if (colx <= mrow->GetSize()) obj=(TObject*)mrow->At(colx-1);
502
503 return obj;
504}
505
506TObject* NcObjMatrix::GetObject(Int_t j) const
507{
519
520 TObject* obj=0;
521 Int_t nobj=0;
522 if (fObjects) nobj=fObjects->GetSize();
523
524 if (j>0 && j<=nobj) obj=(TObject*)fObjects->At(j-1);
525
526 return obj;
527}
528
530{
542
543 return fObjects;
544}
545
547{
553
554 return fMaxrow;
555}
556
558{
564
565 return fMaxcol;
566}
567
569{
575
576 Int_t nobj=0;
577 if (fObjects) nobj=fObjects->GetEntries();
578
579 return nobj;
580}
581
582Int_t NcObjMatrix::GetNrefs(TObject* obj) const
583{
590
591 Int_t nobjs=GetNobjects();
592
593 if (!obj) return nobjs;
594
595 Int_t nrefs=0;
596 for (Int_t i=1; i<=nobjs; i++)
597 {
598 TObject* objx=GetObject(i);
599 if (objx==obj) nrefs++;
600 }
601 return nrefs;
602}
603
604Int_t NcObjMatrix::GetIndices(TObject* obj,TArrayI& rows,TArrayI& cols) const
605{
633
634 Int_t nrefs=GetNrefs(obj);
635 rows.Reset();
636 cols.Reset();
637 rows.Set(nrefs);
638 cols.Set(nrefs);
639 if (!nrefs) return 0;
640
641 Int_t irow,icol;
642 Int_t jref=0;
643 for (Int_t i=0; i<fRows->GetSize(); i++)
644 {
645 TObjArray* columns=(TObjArray*)fRows->At(i);
646 if (!columns) continue;
647
648 for (Int_t j=0; j<columns->GetSize(); j++)
649 {
650 TObject* objx=(TObject*)columns->At(j);
651 if (objx && (objx==obj || !obj))
652 {
653 irow=i+1;
654 if (fSwap) irow=j+1;
655 icol=j+1;
656 if (fSwap) icol=i+1;
657 rows.AddAt(irow,jref);
658 cols.AddAt(icol,jref);
659 jref++;
660 }
661 // All references found ==> Done
662 if (jref==nrefs) break;
663 }
664 // All references found ==> Done
665 if (jref==nrefs) break;
666 }
667 return nrefs;
668}
669
670Int_t NcObjMatrix::GetIndices(TObject* obj,Int_t row,TArrayI& cols) const
671{
703
704 cols.Reset();
705
706 if (row<0 || row>GetMaxRow()) return 0;
707
708 Int_t nrefs=GetNrefs(obj);
709 cols.Set(nrefs);
710 if (!nrefs) return 0;
711
712 Int_t irow,icol;
713 Int_t jref=0;
714
715 // No specific row selection
716 if (!row)
717 {
718 TArrayI ar;
719 TArrayI ac;
720 Int_t n=GetIndices(obj,ar,ac);
721 Int_t found=0;
722 for (Int_t idx=0; idx<n; idx++)
723 {
724 icol=ac.At(idx);
725 found=0;
726 for (Int_t k=0; k<jref; k++)
727 {
728 if (icol==cols.At(k)) found=1;
729 }
730 if (!found)
731 {
732 cols.AddAt(icol,jref);
733 jref++;
734 }
735 }
736 // Set the array size to the actual number of different column indices
737 cols.Set(jref);
738
739 return jref;
740 }
741
742 // Specific row selection
743 for (Int_t i=0; i<fRows->GetSize(); i++)
744 {
745 TObjArray* columns=(TObjArray*)fRows->At(i);
746 if (!columns) continue;
747
748 for (Int_t j=0; j<columns->GetSize(); j++)
749 {
750 TObject* objx=(TObject*)columns->At(j);
751 if (objx && (objx==obj || !obj))
752 {
753 irow=i+1;
754 if (fSwap) irow=j+1;
755 icol=j+1;
756 if (fSwap) icol=i+1;
757 if (irow==row)
758 {
759 cols.AddAt(icol,jref);
760 jref++;
761 }
762 }
763 // All references found ==> Done
764 if (jref==nrefs) break;
765 }
766 // All references found ==> Done
767 if (jref==nrefs) break;
768 }
769 // Set the array size to the actual number of found occurrences
770 cols.Set(jref);
771
772 return jref;
773}
774
775Int_t NcObjMatrix::GetIndices(TObject* obj,TArrayI& rows,Int_t col) const
776{
808
809 rows.Reset();
810
811 if (col<0 || col>GetMaxColumn()) return 0;
812
813 Int_t nrefs=GetNrefs(obj);
814 rows.Set(nrefs);
815 if (!nrefs) return 0;
816
817 Int_t irow,icol;
818 Int_t jref=0;
819
820 // No specific column selection
821 if (!col)
822 {
823 TArrayI ar;
824 TArrayI ac;
825 Int_t n=GetIndices(obj,ar,ac);
826 Int_t found=0;
827 for (Int_t idx=0; idx<n; idx++)
828 {
829 irow=ar.At(idx);
830 found=0;
831 for (Int_t k=0; k<jref; k++)
832 {
833 if (irow==rows.At(k)) found=1;
834 }
835 if (!found)
836 {
837 rows.AddAt(irow,jref);
838 jref++;
839 }
840 }
841 // Set the array size to the actual number of different row indices
842 rows.Set(jref);
843
844 return jref;
845 }
846
847 // Specific column selection
848 for (Int_t i=0; i<fRows->GetSize(); i++)
849 {
850 TObjArray* columns=(TObjArray*)fRows->At(i);
851 if (!columns) continue;
852
853 for (Int_t j=0; j<columns->GetSize(); j++)
854 {
855 TObject* objx=(TObject*)columns->At(j);
856 if (objx && (objx==obj || !obj))
857 {
858 irow=i+1;
859 if (fSwap) irow=j+1;
860 icol=j+1;
861 if (fSwap) icol=i+1;
862 if (icol==col)
863 {
864 rows.AddAt(irow,jref);
865 jref++;
866 }
867 }
868 // All references found ==> Done
869 if (jref==nrefs) break;
870 }
871 // All references found ==> Done
872 if (jref==nrefs) break;
873 }
874 // Set the array size to the actual number of found occurrences
875 rows.Set(jref);
876
877 return jref;
878}
879
880TObject* NcObjMatrix::Clone(const char* name) const
881{
890
891 NcObjMatrix* m=new NcObjMatrix(*this);
892 if (name)
893 {
894 if (strlen(name)) m->SetName(name);
895 }
896 return m;
897}
898
ClassImp(NcObjMatrix)
Handling of a matrix structure of objects.
Definition NcObjMatrix.h:13
Int_t GetNrefs(TObject *obj) const
TObjArray * fObjects
Definition NcObjMatrix.h:44
virtual TObject * GetObject(Int_t row, Int_t col) const
virtual void EnterObject(Int_t row, Int_t col, TObject *obj)
virtual TObject * Clone(const char *name="") const
virtual Int_t GetMaxColumn() const
void RemoveObject(Int_t row, Int_t col)
virtual void SetOwner(Int_t own=1)
virtual ~NcObjMatrix()
TObjArray * fRows
Definition NcObjMatrix.h:39
Int_t GetIndices(TObject *obj, TArrayI &rows, TArrayI &cols) const
virtual Int_t GetSwapMode() const
virtual Int_t GetOwner() const
virtual TObjArray * GetObjects()
void RemoveObjects(TObject *obj, Int_t row=0, Int_t col=0)
Int_t fMaxrow
Definition NcObjMatrix.h:42
virtual Int_t GetMaxRow() const
virtual void SetSwapMode(Int_t swap=1)
Int_t fMaxcol
Definition NcObjMatrix.h:43
virtual Int_t GetNobjects() const
virtual void Reset()