Logo ROOT   6.16/01
Reference Guide
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
17
18////////////////////////////////////////////////////////////////////////////////
19/// Default constructor for streamer
20
22{
23 fDim =0;
24 fCoords =0;
25}
26
27////////////////////////////////////////////////////////////////////////////////
28/// User constructor creating n-dimensional vector
29/// and allocating dynamically array of components
30
32{
33 Int_t i;
34 fDim=n;
35 fCoords = 0;
36 if (n>0) {
37 fCoords = new Double_t[fDim];
38 if(gDebug) {
39 if(fCoords == 0)
40 Error("TFoamVect", "Constructor failed to allocate\n");
41 }
42 for (i=0; i<n; i++) *(fCoords+i)=0.0;
43 }
44 if(gDebug) Info("TFoamVect", "USER CONSTRUCTOR TFoamVect(const Int_t)\n ");
45}
46
47////////////////////////////////////////////////////////////////////////////////
48/// Copy constructor
49
51{
52 fDim = Vect.fDim; fCoords = 0;
53 if(fDim > 0) fCoords = new Double_t[fDim];
54
55 if(gDebug) {
56 if(fCoords == 0) {
57 Error("TFoamVect", "Constructor failed to allocate fCoords\n");
58 }
59 }
60
61 for(Int_t i=0; i<fDim; i++)
62 fCoords[i] = Vect.fCoords[i];
63
64}
65
66////////////////////////////////////////////////////////////////////////////////
67/// Destructor
68
70{
71 if(gDebug) Info("TFoamVect"," DESTRUCTOR TFoamVect~ \n");
72 delete [] fCoords; // free(fCoords)
73 fCoords=0;
74}
75
76
77//////////////////////////////////////////////////////////////////////////////
78// Overloading operators //
79//////////////////////////////////////////////////////////////////////////////
80
81////////////////////////////////////////////////////////////////////////////////
82/// substitution operator
83
85{
86 Int_t i;
87 if (&Vect == this) return *this;
88 if( Vect.fDim < 0 )
89 Error("TFoamVect","operator= : invalid dimensions : %d and %d \n ",fDim,Vect.fDim);
90 if( fDim != Vect.fDim ) { // cleanup
91 delete [] fCoords;
92 fCoords = new Double_t[Vect.fDim];
93 }
94 fDim=Vect.fDim;
95 for(i=0; i<fDim; i++)
96 fCoords[i] = Vect.fCoords[i];
97 if(gDebug) Info("TFoamVect", "SUBSITUTE operator =\n ");
98 return *this;
99}
100
101////////////////////////////////////////////////////////////////////////////////
102/// [] is for access to elements as in ordinary matrix like a[j]=b[j]
103/// (Perhaps against some strict rules but rather practical.)
104/// Range protection is built in, consequently for substitution
105/// one should use rather use a=b than explicit loop!
106
108{
109 if ((n<0) || (n>=fDim)) {
110 Error( "TFoamVect","operator[], out of range \n");
111 }
112 return fCoords[n];
113}
114
115////////////////////////////////////////////////////////////////////////////////
116/// unary multiplication operator *=
117
119{
120 for(Int_t i=0;i<fDim;i++)
121 fCoords[i] = fCoords[i]*x;
122 return *this;
123}
124
125////////////////////////////////////////////////////////////////////////////////
126/// unary addition operator +=; adding vector c*=x,
127
129{
130 if( fDim != Shift.fDim){
131 Error( "TFoamVect","operator+, different dimensions= %d %d \n",fDim,Shift.fDim);
132 }
133 for(Int_t i=0;i<fDim;i++)
134 fCoords[i] = fCoords[i]+Shift.fCoords[i];
135 return *this;
136}
137
138////////////////////////////////////////////////////////////////////////////////
139/// unary subtraction operator -=
140
142{
143 if( fDim != Shift.fDim) {
144 Error( "TFoamVect","operator+, different dimensions= %d %d \n",fDim,Shift.fDim);
145 }
146 for(Int_t i=0;i<fDim;i++)
147 fCoords[i] = fCoords[i]-Shift.fCoords[i];
148 return *this;
149}
150
151////////////////////////////////////////////////////////////////////////////////
152/// addition operator +; sum of 2 vectors: c=a+b, a=a+b,
153/// NEVER USE IT, VERY SLOW!!!
154
156{
157 TFoamVect temp(fDim);
158 temp = (*this);
159 temp += p2;
160 return temp;
161}
162
163////////////////////////////////////////////////////////////////////////////////
164/// subtraction operator -; difference of 2 vectors; c=a-b, a=a-b,
165/// NEVER USE IT, VERY SLOW!!!
166
168{
169 TFoamVect temp(fDim);
170 temp = (*this);
171 temp -= p2;
172 return temp;
173}
174
175////////////////////////////////////////////////////////////////////////////////
176/// Loading in ordinary double prec. vector, sometimes can be useful
177
179{
180 Int_t i;
181 for(i=0; i<fDim; i++)
182 fCoords[i] = Vect[i];
183 return *this;
184}
185
186////////////////////////////////////////////////////////////////////////////////
187/// Loading in double prec. number, sometimes can be useful
188
190{
191 if(fCoords != 0) {
192 for(Int_t i=0; i<fDim; i++)
193 fCoords[i] = x;
194 }
195 return *this;
196}
197//////////////////////////////////////////////////////////////////////////////
198// OTHER METHODS //
199//////////////////////////////////////////////////////////////////////////////
200
201////////////////////////////////////////////////////////////////////////////////
202/// Printout of all vector components on "std::cout"
203
204void TFoamVect::Print(Option_t *option) const
205{
206 if(!option) Error("Print ", "No option set \n");
207 Int_t i;
208 Int_t pr = std::cout.precision(7);
209 std::cout << "(";
210 for(i=0; i<fDim-1; i++) std::cout << std::setw(12) << *(fCoords+i) << ",";
211 std::cout << std::setw(12) << *(fCoords+fDim-1);
212 std::cout << ")";
213 std::cout.precision(pr);
214}
215
216
217///////////////////////////////////////////////////////////////////////////////
218// End of Class TFoamVect //
219///////////////////////////////////////////////////////////////////////////////
static double p2(double t, double a, double b, double c)
int Int_t
Definition: RtypesCore.h:41
double Double_t
Definition: RtypesCore.h:55
const char Option_t
Definition: RtypesCore.h:62
#define ClassImp(name)
Definition: Rtypes.h:363
R__EXTERN Int_t gDebug
Definition: Rtypes.h:90
TFoamVect & operator=(const TFoamVect &)
substitution operator
Definition: TFoamVect.cxx:84
TFoamVect operator+(const TFoamVect &)
addition operator +; sum of 2 vectors: c=a+b, a=a+b, NEVER USE IT, VERY SLOW!!!
Definition: TFoamVect.cxx:155
TFoamVect operator-(const TFoamVect &)
subtraction operator -; difference of 2 vectors; c=a-b, a=a-b, NEVER USE IT, VERY SLOW!...
Definition: TFoamVect.cxx:167
TFoamVect & operator*=(const Double_t &)
unary multiplication operator *=
Definition: TFoamVect.cxx:118
TFoamVect & operator+=(const TFoamVect &)
unary addition operator +=; adding vector c*=x,
Definition: TFoamVect.cxx:128
TFoamVect & operator-=(const TFoamVect &)
unary subtraction operator -=
Definition: TFoamVect.cxx:141
Double_t * fCoords
Definition: TFoamVect.h:22
Int_t fDim
Definition: TFoamVect.h:21
TFoamVect()
Default constructor for streamer.
Definition: TFoamVect.cxx:21
Double_t & operator[](Int_t)
[] is for access to elements as in ordinary matrix like a[j]=bj Range protection is built in,...
Definition: TFoamVect.cxx:107
virtual ~TFoamVect()
Destructor.
Definition: TFoamVect.cxx:69
void Print(Option_t *option) const
Printout of all vector components on "std::cout".
Definition: TFoamVect.cxx:204
Mother of all ROOT objects.
Definition: TObject.h:37
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:880
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition: TObject.cxx:854
Double_t x[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16