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