Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TFoamVect.cxx
Go to the documentation of this file.
1 // @(#)root/foam:$Id$
2 // Author: S. Jadach <mailto:Stanislaw.jadach@ifj.edu.pl>, P.Sawicki <mailto:Pawel.Sawicki@ifj.edu.pl>
3 
4 //_____________________________________________________________________________
5 // //
6 // Auxiliary class TFoamVect of n-dimensional vector, with dynamic allocation //
7 // used for the cartesian geometry of the TFoam cells //
8 // //
9 //_____________________________________________________________________________
10 
11 #include "Riostream.h"
12 #include "TSystem.h"
13 #include "TFoamVect.h"
14 
15 /** \class TFoamVect
16 
17 Auxiliary class TFoamVect of n-dimensional vector, with dynamic allocation
18 used for the cartesian geometry of the TFoam cells
19 
20 */
21 
22 ClassImp(TFoamVect);
23 
24 ////////////////////////////////////////////////////////////////////////////////
25 /// Default constructor for streamer
26 
27 TFoamVect::TFoamVect()
28 {
29  fDim =0;
30  fCoords =0;
31 }
32 
33 ////////////////////////////////////////////////////////////////////////////////
34 /// User constructor creating n-dimensional vector
35 /// and allocating dynamically array of components
36 
37 TFoamVect::TFoamVect(Int_t n)
38 {
39  Int_t i;
40  fDim=n;
41  fCoords = 0;
42  if (n>0) {
43  fCoords = new Double_t[fDim];
44  if(gDebug) {
45  if(fCoords == 0)
46  Error("TFoamVect", "Constructor failed to allocate\n");
47  }
48  for (i=0; i<n; i++) *(fCoords+i)=0.0;
49  }
50  if(gDebug) Info("TFoamVect", "USER CONSTRUCTOR TFoamVect(const Int_t)\n ");
51 }
52 
53 ////////////////////////////////////////////////////////////////////////////////
54 /// Copy constructor
55 
56 TFoamVect::TFoamVect(const TFoamVect &Vect): TObject(Vect)
57 {
58  fDim = Vect.fDim; fCoords = 0;
59  if(fDim > 0) fCoords = new Double_t[fDim];
60 
61  if(gDebug) {
62  if(fCoords == 0) {
63  Error("TFoamVect", "Constructor failed to allocate fCoords\n");
64  }
65  }
66 
67  for(Int_t i=0; i<fDim; i++)
68  fCoords[i] = Vect.fCoords[i];
69 
70 }
71 
72 ////////////////////////////////////////////////////////////////////////////////
73 /// Destructor
74 
75 TFoamVect::~TFoamVect()
76 {
77  if(gDebug) Info("TFoamVect"," DESTRUCTOR TFoamVect~ \n");
78  delete [] fCoords; // free(fCoords)
79  fCoords=0;
80 }
81 
82 ////////////////////////////////////////////////////////////////////////////////
83 /// substitution operator
84 
85 TFoamVect& TFoamVect::operator =(const TFoamVect& Vect)
86 {
87  Int_t i;
88  if (&Vect == this) return *this;
89  if( Vect.fDim < 0 )
90  Error("TFoamVect","operator= : invalid dimensions : %d and %d \n ",fDim,Vect.fDim);
91  if( fDim != Vect.fDim ) { // cleanup
92  delete [] fCoords;
93  fCoords = new Double_t[Vect.fDim];
94  }
95  fDim=Vect.fDim;
96  for(i=0; i<fDim; i++)
97  fCoords[i] = Vect.fCoords[i];
98  if(gDebug) Info("TFoamVect", "SUBSITUTE operator =\n ");
99  return *this;
100 }
101 
102 ////////////////////////////////////////////////////////////////////////////////
103 /// [] is for access to elements as in ordinary matrix like a[j]=b[j]
104 /// (Perhaps against some strict rules but rather practical.)
105 /// Range protection is built in, consequently for substitution
106 /// one should use rather use a=b than explicit loop!
107 
108 Double_t &TFoamVect::operator[](Int_t n)
109 {
110  if ((n<0) || (n>=fDim)) {
111  Error( "TFoamVect","operator[], out of range \n");
112  }
113  return fCoords[n];
114 }
115 
116 ////////////////////////////////////////////////////////////////////////////////
117 /// unary multiplication operator *=
118 
119 TFoamVect& TFoamVect::operator*=(const Double_t &x)
120 {
121  for(Int_t i=0;i<fDim;i++)
122  fCoords[i] = fCoords[i]*x;
123  return *this;
124 }
125 
126 ////////////////////////////////////////////////////////////////////////////////
127 /// unary addition operator +=; adding vector c*=x,
128 
129 TFoamVect& TFoamVect::operator+=(const TFoamVect& Shift)
130 {
131  if( fDim != Shift.fDim){
132  Error( "TFoamVect","operator+, different dimensions= %d %d \n",fDim,Shift.fDim);
133  }
134  for(Int_t i=0;i<fDim;i++)
135  fCoords[i] = fCoords[i]+Shift.fCoords[i];
136  return *this;
137 }
138 
139 ////////////////////////////////////////////////////////////////////////////////
140 /// unary subtraction operator -=
141 
142 TFoamVect& TFoamVect::operator-=(const TFoamVect& Shift)
143 {
144  if( fDim != Shift.fDim) {
145  Error( "TFoamVect","operator+, different dimensions= %d %d \n",fDim,Shift.fDim);
146  }
147  for(Int_t i=0;i<fDim;i++)
148  fCoords[i] = fCoords[i]-Shift.fCoords[i];
149  return *this;
150 }
151 
152 ////////////////////////////////////////////////////////////////////////////////
153 /// addition operator +; sum of 2 vectors: c=a+b, a=a+b,
154 /// NEVER USE IT, VERY SLOW!!!
155 
156 TFoamVect TFoamVect::operator+(const TFoamVect &p2)
157 {
158  TFoamVect temp(fDim);
159  temp = (*this);
160  temp += p2;
161  return temp;
162 }
163 
164 ////////////////////////////////////////////////////////////////////////////////
165 /// subtraction operator -; difference of 2 vectors; c=a-b, a=a-b,
166 /// NEVER USE IT, VERY SLOW!!!
167 
168 TFoamVect TFoamVect::operator-(const TFoamVect &p2)
169 {
170  TFoamVect temp(fDim);
171  temp = (*this);
172  temp -= p2;
173  return temp;
174 }
175 
176 ////////////////////////////////////////////////////////////////////////////////
177 /// Loading in ordinary double prec. vector, sometimes can be useful
178 
179 TFoamVect& TFoamVect::operator =(Double_t Vect[])
180 {
181  Int_t i;
182  for(i=0; i<fDim; i++)
183  fCoords[i] = Vect[i];
184  return *this;
185 }
186 
187 ////////////////////////////////////////////////////////////////////////////////
188 /// Loading in double prec. number, sometimes can be useful
189 
190 TFoamVect& TFoamVect::operator =(Double_t x)
191 {
192  if(fCoords != 0) {
193  for(Int_t i=0; i<fDim; i++)
194  fCoords[i] = x;
195  }
196  return *this;
197 }
198 
199 ////////////////////////////////////////////////////////////////////////////////
200 /// Printout of all vector components on "std::cout"
201 
202 void TFoamVect::Print(Option_t *option) const
203 {
204  if(!option) Error("Print ", "No option set \n");
205  Int_t i;
206  Int_t pr = std::cout.precision(7);
207  std::cout << "(";
208  for(i=0; i<fDim-1; i++) std::cout << std::setw(12) << *(fCoords+i) << ",";
209  std::cout << std::setw(12) << *(fCoords+fDim-1);
210  std::cout << ")";
211  std::cout.precision(pr);
212 }