00001 #include "emcClusterContainerv1.h" 00002 #include "emcClusterContentv1.h" 00003 #include "TClonesArray.h" 00004 #include <iostream> 00005 #include <algorithm> 00006 #include "phool.h" 00007 00008 ClassImp(emcClusterContainerv1); 00009 00010 using namespace std; 00011 00012 namespace { 00013 void clear(TClonesArray& array) 00014 { 00015 #if ROOT_VERSION_CODE > ROOT_VERSION(3,01,5) 00016 // for recent version, it's taken care of by ROOT itself. Easy. 00017 array.Clear("C"); 00018 #else 00019 // we have do to it ourselves. 00020 int n = array.GetEntriesFast(); 00021 for ( int i = 0; i < n; ++i ) 00022 { 00023 TObject* obj = array.At(i); 00024 if ( obj ) 00025 { 00026 obj->Clear(); 00027 } 00028 } 00029 array.SetOwner(false); 00030 array.Clear(); 00031 #endif 00032 } 00033 } 00034 00035 const unsigned int emcClusterContainerv1::fgDefaultSize = 1000; 00036 const unsigned int emcClusterContainerv1::fgMaxSize = 24768; 00037 00038 //_____________________________________________________________________________ 00039 emcClusterContainerv1::emcClusterContainerv1() : fEmcClusters(0) 00040 { 00041 allocate(emcClusterContainerv1::fgDefaultSize); 00042 } 00043 00044 //_____________________________________________________________________________ 00045 emcClusterContainerv1::emcClusterContainerv1(const emcClusterContainerv1& o) : fEmcClusters(0) 00046 { 00047 o.copy(*this); 00048 } 00049 00050 //_____________________________________________________________________________ 00051 emcClusterContainerv1& 00052 emcClusterContainerv1::operator=(const emcClusterContainerv1& o) 00053 { 00054 if ( this != &o ) 00055 { 00056 Reset(); 00057 o.copy(*this); 00058 } 00059 return *this; 00060 } 00061 00062 //_____________________________________________________________________________ 00063 emcClusterContainerv1::~emcClusterContainerv1() 00064 { 00065 clear(*fEmcClusters); 00066 delete fEmcClusters; 00067 } 00068 00069 //_____________________________________________________________________________ 00070 emcClusterContentv1* 00071 emcClusterContainerv1::addCluster(unsigned int i) 00072 { 00073 if ( static_cast<int>(i) > fEmcClusters->GetSize() ) 00074 { 00075 bool ok = resize(std::max(capacity()*2,fgMaxSize)); 00076 if ( !ok ) 00077 { 00078 cerr << PHWHERE << " object is full ?!" << endl; 00079 return 0; 00080 } 00081 } 00082 00083 emcClusterContentv1* rv = new((*fEmcClusters)[i]) emcClusterContentv1; 00084 return rv; 00085 } 00086 00087 //_____________________________________________________________________________ 00088 emcClusterContentv1* 00089 emcClusterContainerv1::addCluster(unsigned int i, const emcClusterContent& c) 00090 { 00091 const emcClusterContentv1* test = 00092 dynamic_cast<const emcClusterContentv1*>(&c); 00093 00094 if (!test) 00095 { 00096 cerr << PHWHERE << " emcClusterContent is not of type v1" 00097 << endl; 00098 return 0; 00099 } 00100 00101 if ( static_cast<int>(i) > fEmcClusters->GetSize() ) 00102 { 00103 bool ok = resize(std::max(capacity()*2,fgMaxSize)); 00104 if ( !ok ) 00105 { 00106 cerr << PHWHERE << " object is full ?!" << endl; 00107 return 0; 00108 } 00109 } 00110 00111 return new((*fEmcClusters)[i]) emcClusterContentv1(*test); 00112 } 00113 00114 //_____________________________________________________________________________ 00115 void 00116 emcClusterContainerv1::allocate(unsigned int thesize) 00117 { 00118 if (fEmcClusters) 00119 { 00120 clear(*fEmcClusters); 00121 delete fEmcClusters; 00122 } 00123 fEmcClusters = new TClonesArray("emcClusterContentv1",thesize); 00124 } 00125 00126 //_____________________________________________________________________________ 00127 unsigned int 00128 emcClusterContainerv1::capacity(void) const 00129 { 00130 return fEmcClusters->GetSize(); 00131 } 00132 00133 //_____________________________________________________________________________ 00134 emcClusterContainerv1* 00135 emcClusterContainerv1::clone(void) const 00136 { 00137 return new emcClusterContainerv1(*this); 00138 } 00139 00140 //_____________________________________________________________________________ 00141 emcClusterContainerv1* 00142 emcClusterContainerv1::create(void) const 00143 { 00144 return new emcClusterContainerv1; 00145 } 00146 00147 //_____________________________________________________________________________ 00148 void 00149 emcClusterContainerv1::copy(emcClusterContainerv1& dest) const 00150 { 00151 TClonesArray* destarray = dest.fEmcClusters; 00152 if ( !destarray ) 00153 { 00154 dest.allocate(fEmcClusters->GetSize()); 00155 destarray = dest.fEmcClusters; 00156 } 00157 else 00158 { 00159 dest.Reset(); 00160 } 00161 00162 unsigned int idest = 0; 00163 00164 for ( unsigned int i = 0; i < size(); ++i ) 00165 { 00166 emcClusterContentv1* clus = dest.addCluster(idest++); 00167 *clus = *(getCluster(i)); 00168 } 00169 } 00170 00171 //_____________________________________________________________________________ 00172 emcClusterContentv1* 00173 emcClusterContainerv1::findCluster(int clusterid) const 00174 { 00175 for ( size_t i = 0; i < size(); ++i ) 00176 { 00177 if ( getCluster(i)->id() == clusterid ) 00178 { 00179 return getCluster(i); 00180 } 00181 } 00182 return 0; 00183 } 00184 00185 //_____________________________________________________________________________ 00186 emcClusterContentv1* 00187 emcClusterContainerv1::getCluster(unsigned int i) const 00188 { 00189 return static_cast<emcClusterContentv1*>(fEmcClusters->At(i)); 00190 } 00191 00192 //_____________________________________________________________________________ 00193 void 00194 emcClusterContainerv1::identify(ostream& os) const 00195 { 00196 cout << "emcClusterContainerv1::identify : size=" << size() << endl; 00197 } 00198 00199 //_____________________________________________________________________________ 00200 int 00201 emcClusterContainerv1::isValid() const 00202 { 00203 return 1; 00204 } 00205 00206 //_____________________________________________________________________________ 00207 void 00208 emcClusterContainerv1::Reset() 00209 { 00210 clear(*fEmcClusters); 00211 } 00212 00213 //_____________________________________________________________________________ 00214 bool 00215 emcClusterContainerv1::removeCluster(unsigned int i) 00216 { 00217 if ( i < size() ) 00218 { 00219 fEmcClusters->RemoveAt(i); 00220 fEmcClusters->Compress(); 00221 return true; 00222 } 00223 else 00224 { 00225 return false; 00226 } 00227 } 00228 00229 //_____________________________________________________________________________ 00230 bool 00231 emcClusterContainerv1::resize(unsigned int newsize) 00232 { 00233 if ( newsize < emcClusterContainerv1::fgMaxSize ) 00234 { 00235 fEmcClusters->Expand(newsize); 00236 return true; 00237 } 00238 else 00239 { 00240 return false; 00241 } 00242 } 00243 00244 //_____________________________________________________________________________ 00245 unsigned int 00246 emcClusterContainerv1::size(void) const 00247 { 00248 return fEmcClusters->GetLast()+1; 00249 }