TFoamVect.cxx

Go to the documentation of this file.
00001 // @(#)root/foam:$Id: TFoamVect.cxx 35526 2010-09-21 12:22:12Z moneta $
00002 // Author: S. Jadach <mailto:Stanislaw.jadach@ifj.edu.pl>, P.Sawicki <mailto:Pawel.Sawicki@ifj.edu.pl>
00003 
00004 //_____________________________________________________________________________
00005 //                                                                            //
00006 // Auxiliary class TFoamVect of n-dimensional vector, with dynamic allocation //
00007 // used for the cartesian geometry of the TFoam  cells                        //
00008 //                                                                            //
00009 //_____________________________________________________________________________
00010 
00011 #include "Riostream.h"
00012 #include "TSystem.h"
00013 #include "TFoamVect.h"
00014 
00015 
00016 ClassImp(TFoamVect);
00017 
00018 //_____________________________________________________________________________
00019 TFoamVect::TFoamVect()
00020 {
00021 // Default constructor for streamer
00022 
00023    fDim    =0;
00024    fCoords =0;
00025    fNext   =0;
00026    fPrev   =0;
00027 }
00028 
00029 //______________________________________________________________________________
00030 TFoamVect::TFoamVect(Int_t n)
00031 {
00032 // User constructor creating n-dimensional vector
00033 // and allocating dynamically array of components
00034 
00035    Int_t i;
00036    fNext=0;
00037    fPrev=0;
00038    fDim=n;
00039    fCoords = 0;
00040    if (n>0) {
00041       fCoords = new Double_t[fDim];
00042       if(gDebug) {
00043          if(fCoords == 0)
00044             Error("TFoamVect", "Constructor failed to allocate\n");
00045       }
00046       for (i=0; i<n; i++) *(fCoords+i)=0.0;
00047    }
00048    if(gDebug) Info("TFoamVect", "USER CONSTRUCTOR TFoamVect(const Int_t)\n ");
00049 }
00050 
00051 //___________________________________________________________________________
00052 TFoamVect::TFoamVect(const TFoamVect &Vect): TObject(Vect)
00053 {
00054 // Copy constructor
00055 
00056    fNext=0;
00057    fPrev=0;
00058    fDim=Vect.fDim;
00059    fCoords = 0;
00060    if(fDim>0)  fCoords = new Double_t[fDim];
00061    if(gDebug) {
00062       if(fCoords == 0) {
00063          Error("TFoamVect", "Constructor failed to allocate fCoords\n");
00064       }
00065    }
00066    for(Int_t i=0; i<fDim; i++)
00067       fCoords[i] = Vect.fCoords[i];
00068    Error("TFoamVect","+++++ NEVER USE Copy constructor !!!!!\n ");
00069 }
00070 
00071 //___________________________________________________________________________
00072 TFoamVect::~TFoamVect()
00073 {
00074 // Destructor
00075    if(gDebug) Info("TFoamVect"," DESTRUCTOR TFoamVect~ \n");
00076    delete [] fCoords; //  free(fCoords)
00077    fCoords=0;
00078 }
00079 
00080 
00081 //////////////////////////////////////////////////////////////////////////////
00082 //                     Overloading operators                                //
00083 //////////////////////////////////////////////////////////////////////////////
00084 
00085 //____________________________________________________________________________
00086 TFoamVect& TFoamVect::operator =(const TFoamVect& Vect)
00087 {
00088 // substitution operator
00089 
00090    Int_t i;
00091    if (&Vect == this) return *this;
00092    if( fDim != Vect.fDim )
00093       Error("TFoamVect","operator=Dims. are different: %d and %d \n ",fDim,Vect.fDim);
00094    if( fDim != Vect.fDim ) {  // cleanup
00095       delete [] fCoords;
00096       fCoords = new Double_t[fDim];
00097    }
00098    fDim=Vect.fDim;
00099    for(i=0; i<fDim; i++)
00100       fCoords[i] = Vect.fCoords[i];
00101    fNext=Vect.fNext;
00102    fPrev=Vect.fPrev;
00103    if(gDebug)  Info("TFoamVect", "SUBSITUTE operator =\n ");
00104    return *this;
00105 }
00106 
00107 //______________________________________________________________________
00108 Double_t &TFoamVect::operator[](Int_t n)
00109 {
00110 // [] is for access to elements as in ordinary matrix like a[j]=b[j]
00111 // (Perhaps against some strict rules but rather practical.)
00112 // Range protection is built in, consequently for substitution
00113 // one should use rather use a=b than explicit loop!
00114 
00115    if ((n<0) || (n>=fDim)) {
00116       Error( "TFoamVect","operator[], out of range \n");
00117    }
00118    return fCoords[n];
00119 }
00120 
00121 //______________________________________________________________________
00122 TFoamVect& TFoamVect::operator*=(const Double_t &x)
00123 {
00124 // unary multiplication operator *=
00125 
00126    for(Int_t i=0;i<fDim;i++)
00127       fCoords[i] = fCoords[i]*x;
00128    return *this;
00129 }
00130 
00131 //_______________________________________________________________________
00132 TFoamVect& TFoamVect::operator+=(const TFoamVect& Shift)
00133 {
00134 // unary addition operator +=; adding vector c*=x,
00135    if( fDim != Shift.fDim){
00136       Error( "TFoamVect","operator+, different dimensions= %d %d \n",fDim,Shift.fDim);
00137    }
00138    for(Int_t i=0;i<fDim;i++)
00139       fCoords[i] = fCoords[i]+Shift.fCoords[i];
00140    return *this;
00141 }
00142 
00143 //________________________________________________________________________
00144 TFoamVect& TFoamVect::operator-=(const TFoamVect& Shift)
00145 {
00146 // unary subtraction operator -=
00147    if( fDim != Shift.fDim) {
00148       Error( "TFoamVect","operator+, different dimensions= %d %d \n",fDim,Shift.fDim);
00149    }
00150    for(Int_t i=0;i<fDim;i++)
00151       fCoords[i] = fCoords[i]-Shift.fCoords[i];
00152    return *this;
00153 }
00154 
00155 //_________________________________________________________________________
00156 TFoamVect TFoamVect::operator+(const TFoamVect &p2)
00157 {
00158 // addition operator +; sum of 2 vectors: c=a+b, a=a+b,
00159 // NEVER USE IT, VERY SLOW!!!
00160    TFoamVect temp(fDim);
00161    temp  = (*this);
00162    temp += p2;
00163    return temp;
00164 }
00165 
00166 //__________________________________________________________________________
00167 TFoamVect TFoamVect::operator-(const TFoamVect &p2)
00168 {
00169 // subtraction operator -; difference of 2 vectors; c=a-b, a=a-b,
00170 // NEVER USE IT, VERY SLOW!!!
00171    TFoamVect temp(fDim);
00172    temp  = (*this);
00173    temp -= p2;
00174    return temp;
00175 }
00176 
00177 //___________________________________________________________________________
00178 TFoamVect& TFoamVect::operator =(Double_t Vect[])
00179 {
00180 // Loading in ordinary double prec. vector, sometimes can be useful
00181    Int_t i;
00182    for(i=0; i<fDim; i++)
00183       fCoords[i] = Vect[i];
00184    return *this;
00185 }
00186 
00187 //____________________________________________________________________________
00188 TFoamVect& TFoamVect::operator =(Double_t x)
00189 {
00190 // Loading in double prec. number, sometimes can be useful
00191    if(fCoords != 0) {
00192       for(Int_t i=0; i<fDim; i++)
00193          fCoords[i] = x;
00194    }
00195    return *this;
00196 }
00197 //////////////////////////////////////////////////////////////////////////////
00198 //                          OTHER METHODS                                   //
00199 //////////////////////////////////////////////////////////////////////////////
00200 
00201 //_____________________________________________________________________________
00202 void TFoamVect::Print(Option_t *option) const
00203 {
00204 // Printout of all vector components on "cout"
00205    if(!option) Error("Print ", "No option set \n");
00206    Int_t i;
00207    Int_t pr = cout.precision(7); 
00208    cout << "(";
00209    for(i=0; i<fDim-1; i++) cout  << setw(12) << *(fCoords+i) << ",";
00210    cout  << setw(12) << *(fCoords+fDim-1);
00211    cout << ")";
00212    cout.precision(pr);
00213 }
00214 //______________________________________________________________________________
00215 void TFoamVect::PrintList(void)
00216 {
00217 // Printout of all member vectors in the list starting from "this"
00218    Long_t i=0;
00219    if(this == 0) return;
00220    TFoamVect *current=this;
00221    while(current != 0) {
00222       cout<<"vec["<<i<<"]=";
00223       current->Print("1");
00224       cout<<endl;
00225       current = current->fNext;
00226       i++;
00227    }
00228 }
00229 
00230 ///////////////////////////////////////////////////////////////////////////////
00231 //                End of Class TFoamVect                                        //
00232 ///////////////////////////////////////////////////////////////////////////////

Generated on Tue Jul 5 14:30:49 2011 for ROOT_528-00b_version by  doxygen 1.5.1