2 #include "Sti/Base/Factory.h"
3 #include "Sti/StiDetector.h"
4 #include "Sti/StiPlanarShape.h"
5 #include "Sti/StiCylindricalShape.h"
6 #include "Sti/StiPlacement.h"
7 #include "Sti/StiMaterial.h"
8 #include "Sti/StiDetectorBuilder.h"
12 #include "Sti/StiElossCalculator.h"
13 #include "StDetectorDbMaker/StiDefaultTrackingParameters.h"
14 #include "StThreeVector.hh"
16 #include "StThreeVectorD.hh"
21 int StiDetectorBuilder::_debug = 0;
23 StiDetectorBuilder::StiDetectorBuilder(
const string & name,
bool active)
24 :
Named(name+
"Builder"),
29 _detectorFactory(
StiToolkit::instance()->getDetectorFactory() ),
30 _trackingParameters(0),
33 cout <<
"StiDetectorBuilder::StiDetectorBuilder() - INFO - Instantiating builder named:"<<name<<endl;
34 fCurrentDetectorBuilder =
this;
38 StiDetectorBuilder::~StiDetectorBuilder()
42 bool StiDetectorBuilder::hasMore()
const
45 return mDetectorIterator != mDetectorMap.end();
52 if (mDetectorIterator != mDetectorMap.end())
53 return (mDetectorIterator++)->second;
59 StiMaterial* StiDetectorBuilder::findMaterial(
const string& szName)
const
61 materialMap::const_iterator where = mMaterialMap.find(
NameMapKey(szName));
62 return (where!= mMaterialMap.end()) ? (*where).second : 0;
66 StiShape* StiDetectorBuilder::findShape(
const string& szName)
const
68 shapeMap::const_iterator where = mShapeMap.find(
NameMapKey(szName));
69 return (where!=mShapeMap.end()) ? (*where).second: 0;
73 StiDetector* StiDetectorBuilder::findDetector(
const string& szName)
const
75 detectorMap::const_iterator where = mDetectorMap.find(
NameMapKey(szName));
76 return (where!=mDetectorMap.end()) ? (*where).second: 0;
83 mMaterialMap.insert( materialMapValType(key,material) );
91 mShapeMap.insert( shapeMapValType(key, shape) );
98 setNSectors(row,sector+1);
99 if (_detectors[row][sector]) {
100 printf(
"***ERROR*** StiDetectorBuilder::add(%d,%d,\"%s\") "
101 ,row,sector,detector->
getName().c_str());
102 printf(
" is replacing %s\n",_detectors[row][sector]->
getName().c_str());
103 assert( !_detectors[row][sector]);
106 if (_debug || sector == 0) {
107 cout <<
"StiDetectorBuilder::add(" << row <<
"," << sector <<
") detector ";
108 if (detector) cout << detector->
getName();
109 else cout <<
" NULL ??";
112 return add(detector);
124 cout <<
"StiDetectorBuilder::add(" << detector << old <<
"existing with the same name " << detector->
getName() <<endl;
127 mDetectorMap.insert( detectorMapValType(key, detector) );
131 detector->setGroupId(_groupId);
132 if (! detector->getTrackingParameters())
133 detector->setTrackingParameters(StiDefaultTrackingParameters::instance());
137 void StiDetectorBuilder::del(UInt_t row, UInt_t sector)
143 assert(row < _detectors.size());
144 assert(sector < _detectors[row].size());
146 StiDetector* stiDetector = getDetector(row, sector);
149 cout <<
"StiDetectorBuilder::del(" << row <<
"," << sector <<
") detector " << stiDetector->
getName() <<endl;
151 mDetectorMap.erase(stiDetector->
getName());
154 getDetectorFactory()->
free(stiDetector);
156 _detectors[row][sector] = 0;
161 void StiDetectorBuilder::build(
StMaker& source)
163 buildDetectors(source);
165 mDetectorIterator = mDetectorMap.begin();
169 void StiDetectorBuilder::buildDetectors(
StMaker& source)
172 void StiDetectorBuilder::AverageVolume(TGeoPhysicalNode *nodeP)
174 if (debug()) {cout <<
"StiDetectorBuilder::AverageVolume -I TGeoPhysicalNode\t" << nodeP->GetName() << endl;}
175 TGeoVolume *volP = nodeP->GetVolume();
176 TGeoMaterial *matP = volP->GetMaterial();
if (debug()) matP->Print(
"");
177 TGeoShape *shapeP = nodeP->GetShape();
if (debug()) {cout <<
"New Shape\t"; StiVMCToolKit::PrintShape(shapeP);}
178 TGeoHMatrix *hmat = nodeP->GetMatrix();
if (debug()) hmat->Print(
"");
179 Double_t PotI = StiVMCToolKit::GetPotI(matP);
184 matP->GetDensity()*matP->GetRadLen(),
189 StiShape *sh = findShape(volP->GetName());
190 Double_t *xyz = hmat->GetTranslation();
196 if (!shapeP->TestShapeBit(TGeoShape::kGeoTube))
break;
197 TGeoTube *shapeC = (TGeoTube *)shapeP;
198 Double_t Rmax = shapeC->GetRmax();
199 Double_t Rmin = shapeC->GetRmin();
200 Double_t delta=fabs(xyz[0])+fabs(xyz[1]);
201 if (delta>0.1*Rmin)
break;
202 Double_t dZ = shapeC->GetDz();
203 Double_t dPhi = 2*TMath::Pi();
204 Double_t dR = Rmax - Rmin;
206 if (shapeP->TestShapeBit(TGeoShape::kGeoTubeSeg)) {
207 TGeoTubeSeg *shapeS = (TGeoTubeSeg *) shapeP;
209 Double_t Phi1 = TMath::DegToRad()*shapeS->GetPhi1();
210 Double_t Phi2 = TMath::DegToRad()*shapeS->GetPhi2();
211 if (Phi2<Phi1) Phi2+=M_PI*2;
212 Double_t PhiM = (Phi2+Phi1)/2;
214 Double_t locV[3]={cos(PhiM),sin(PhiM),0};
215 hmat->LocalToMaster(locV,gloV);
216 Phi = atan2(gloV[1],gloV[0]);
219 TString Name(nodeP->GetName());
227 pPlacement->setZcenter(xyz[2]);
228 pPlacement->setLayerRadius((Rmin+Rmax)*0.5);
229 pPlacement->setLayerAngle(Phi);
230 pPlacement->setRegion(StiPlacement::kMidRapidity);
231 pPlacement->setNormalRep(Phi,0.5*(Rmin+Rmax), 0);
236 shapeP->ComputeBBox();
238 TGeoBBox *box = (TGeoBBox*) shapeP;
239 TGeoRotation geoRotation(*hmat);
242 double euler_phi = geoRotation.GetPhiRotation()/180*M_PI;
245 TVector3 centerVec(xyz);
247 double halfThickness = box->GetDX();
248 double halfWidth = box->GetDY();
249 double dz = box->GetDZ();
250 double r = centerVec.Perp();
251 double phi = centerVec.Phi();
254 TVector3 normVec(cos(euler_phi), sin(euler_phi), 0);
255 TVector3 normVecPerp(-sin(euler_phi), cos(euler_phi), 0);
257 double centerOrient = centerVec.DeltaPhi(normVec);
262 if ( fabs(centerVec.Dot(normVecPerp)) > fabs(centerVec.Dot(normVec)) )
264 halfThickness = box->GetDY();
265 halfWidth = box->GetDX();
266 normVec = normVecPerp;
270 if (normVec.Dot(centerVec) < 0) {
277 sh =
new StiPlanarShape(volP->GetName(), dz, 2*halfThickness, halfWidth);
281 centerOrient = centerVec.DeltaPhi(normVec);
283 double normVecMag = fabs(r*cos(centerOrient));
284 double normVecOffset = r*sin(centerOrient);
287 pPlacement->setZcenter(xyz[2]);
288 pPlacement->setLayerRadius(r);
289 pPlacement->setLayerAngle(phi);
290 pPlacement->setRegion(StiPlacement::kMidRapidity);
291 pPlacement->setNormalRep(normVec.Phi(), normVecMag, normVecOffset);
295 TString nameP(nodeP->GetName());
296 nameP.ReplaceAll(
"HALL_1/CAVE_1/",
"");
298 pDetector->
setName(nameP.Data());
300 pDetector->setShape(sh);
301 pDetector->setPlacement(pPlacement);
302 pDetector->setGas(GetCurrentDetectorBuilder()->getGasMat());
303 pDetector->setMaterial(matS);
305 if (mThkSplit>0 && mMaxSplit>1) {
307 pDetector->splitIt(dv,mThkSplit,mMaxSplit);
308 for (
int i=0;i<(int)dv.size();i++) {
309 int layer = getNRows();
311 cout <<
"StiDetectorBuilder::AverageVolume build detector " << dv[i]->getName() <<
" at layer " << layer << endl;
314 int layer = getNRows();
315 add(layer,0,pDetector);
316 cout <<
"StiDetectorBuilder::AverageVolume build detector " << pDetector->
getName() <<
" at layer " << layer << endl;
327 assert(row<_detectors.size());
328 return _detectors[row].size();
333 StiDetector * StiDetectorBuilder::getDetector(UInt_t row, UInt_t sector)
const
335 assert(row<_detectors.size());
336 assert(sector<_detectors[row].size());
337 return _detectors[row][sector];
341 void StiDetectorBuilder::setDetector(UInt_t row, UInt_t sector,
StiDetector *detector)
343 setNSectors(row+1,sector+1);
344 assert(!_detectors[row][sector]);
349 void StiDetectorBuilder::Print()
const
351 std::cout <<
"StiDetectorBuilder::Print(): " <<
getName() << std::endl;
352 std::copy(mDetectorMap.begin(), mDetectorMap.end(), std::ostream_iterator<DetectorMapPair>(std::cout,
"\n"));
356 ostream&
operator<<(ostream& os,
const DetectorMapPair& detMapEntry)
358 return os << *detMapEntry.second;
friend ostream & operator<<(ostream &os, const StiHit &h)
virtual void free(Abstract *obj)=0
Free an object for reuse.
const StiDetector * detector() const
virtual Abstract * getInstance()=0
Get a pointer to instance of objects served by this factory.
void setName(const string &newName)
Set the name of the object.
virtual UInt_t getNSectors(UInt_t row=0) const
const string & getName() const
Get the name of the object.
Class implements an object which is never active.