#include "geantdef.h" #include "hgeoset.h" #include "hnamedlistfc.h" #include "utility.h" #include #include #include "geafuncdec.h" template class HNamedList; template class HArray; template class HVector; typedef HVector DVec; // ************************************************************************ // ---------------------- readKeepIn ----------------------------------- // ************************************************************************ int HGeoSet::readKeepIn(HGeoInfo & geoInfo, HGeoMedia & geoMedia) { int noKeepIn=0; #ifdef WITHORACLE HGeoParam ele; int ns=geoInfo.getNs(); for(int i=1;i<=ns;i++) { HString volName=keepinName + HString(i); ele.objIndex=geoInfo.geoDb->getGeomParam(volName,ele); if (ele.objIndex>0) { int rc; if (i==1) rc=readParam(geoInfo,volName,ele,geoMedia); else readCopyParam(geoInfo,ele); if (rc==HSUCCESS) { append(volName,ele); noKeepIn++; #ifdef ILSESHOW showAll(volName,ele); #endif } else { cerr << " cannot read " << volName << " from oracle" << endl; return 0; } } } #endif return noKeepIn; } // ************************************************************************ // ---------------------- readModule ------------------------------------ // ************************************************************************ int HGeoSet::readModule(HGeoInfo & geoInfo, HGeoMedia & geoMedia) { #ifdef WITHORACLE HGeoParam ele; int no=0, iMo=-1; int ns=geoInfo.getNs(); for(int i=1;i<=ns;i++) { HString volName=detName + HString(i); ele.objIndex=geoInfo.geoDb->getGeomParam(volName,ele); if (ele.objIndex>0) { int rc; if (i==1) rc=readParam(geoInfo,volName,ele,geoMedia); else readCopyParam(geoInfo,ele); if (rc==HSUCCESS) { if (i==1) iMo=ele.objIndex; no++; append(volName,ele); #ifdef ILSESHOW showAll(volName,ele); #endif } else { cerr << " cannot read " << volName << " from oracle" << endl; return 0; } } } if (no>0 && iMo>0) { if (readDaughters(geoInfo,iMo,geoMedia)>=0) return no; else { cerr << "no daugthers found in database" << endl;} } return 0; #endif } // ************************************************************************ // ---------------------- readSingleModule ------------------------------ // ************************************************************************ int HGeoSet::readSingleModule(HGeoInfo & geoInfo, HGeoMedia & geoMedia, int breakLoop) { #ifdef WITHORACLE int no=0; HGeoParam ele; ele.objIndex=geoInfo.geoDb->getGeomParam(detName,ele); if (ele.objIndex>0) { if (readParam(geoInfo,detName,ele,geoMedia)==HSUCCESS) { no++; append(detName,ele); #ifdef ILSESHOW showAll(detName,ele); #endif } else { cerr << " cannot read " << detName << " from oracle" << endl; } } if (no>0 && ele.objIndex>0) { if (readDaughters(geoInfo,ele.objIndex,geoMedia,breakLoop)>=0) return no; else { cerr << "no daugthers found in database" << endl; } } #endif return 0; } // ************************************************************************ // ---------------------- readParam ------------------------------------- // ************************************************************************ int HGeoSet::readParam(HGeoInfo & geoInfo, HString & volName, HGeoParam & ele, HGeoMedia & geoMedia) { #ifdef WITHORACLE ele.inout=1; // not stored in database if (ele.shape.length()<4) ele.shape += ' '; if (volName.length()>4) { int found=0; HString copyName=volName(0,4); for (int i=0;igetTransform(ele.objIndex,ele.labTrans,ele.labRot) <=0) return HFAILURE; if (calcMotherTransform(geoInfo,ele)==HFAILURE) return HFAILURE; return HSUCCESS; } } copyNames[++noCopyNames]=copyName; } if (geoMedia.readMediumDb(geoInfo, ele.material)==HFAILURE) return HFAILURE; if (pShapes->readPointsDb(geoInfo,ele)==HFAILURE) return HFAILURE; if (geoInfo.geoDb->getTransform(ele.objIndex,ele.labTrans,ele.labRot) <=0) return HFAILURE; if (calcMotherTransform(geoInfo,ele)==HFAILURE) return HFAILURE; #endif return HSUCCESS; } // ************************************************************************ // ---------------------- readCopyParam --------------------------------- // ************************************************************************ int HGeoSet::readCopyParam(HGeoInfo & geoInfo, HGeoParam & ele) { #ifdef WITHORACLE if (geoInfo.geoDb->getTransform(ele.objIndex,ele.labTrans,ele.labRot) <=0) return HFAILURE; if (calcMotherTransform(geoInfo,ele)==HFAILURE) return HFAILURE; #endif return HSUCCESS; } // ************************************************************************ // ---------------------- readDaughters --------------------------------- // ************************************************************************ int HGeoSet::readDaughters(HGeoInfo & geoInfo, int iMo, HGeoMedia & geoMedia, int breakLoop) { int nD=0; #ifdef WITHORACLE HString volName; HGeoParam ele; nD=geoInfo.geoDb->getNrDaughters(iMo,breakLoop); if (nD>0) { for(int i=0;igetDaughterIndex(i); volName=geoInfo.geoDb->getGeomParam(ele.objIndex,ele); if (volName.length()>0) { if (readParam(geoInfo,volName,ele,geoMedia)==HSUCCESS) { append(volName,ele); #ifdef ILSESHOW showAll(volName,ele); #endif } else { cerr << "cannot read " << volName << " from database" << endl; return -1; } } else { cerr << "cannot read volume with index " << ele.objIndex << endl; return -1; } } } #endif return nD; } // ************************************************************************ // ---------------------- readKeepIn ------------------------------------ // ************************************************************************ int HGeoSet::readKeepIn(HGeoInfo & geoInfo) { HGeoParam ele; HString volName(' ',120), copyName; int n=0, noKeepIn=0, retVal, lName; int lKeepinName=keepinName.length(); int ns=geoInfo.getNs(); while(!geoInfo.fin.eof() && n> volName; if (volName.empty() || volName(0)=='/') geoInfo.fin.getline(volName,120); else { if (volName(0,lKeepinName)==keepinName) { n++; readInout(geoInfo,ele); geoInfo.fin >> ele.mother.assign(10); lName=volName.length(); if (lName<=4) retVal=readParam(geoInfo,ele); else retVal=readCopy(geoInfo,ele); if (retVal==HSUCCESS) { if (ele.inout==1) noKeepIn++; append(volName,ele); } if (ele.mother.find("SEC")<0) return noKeepIn; } } } return noKeepIn; } // ************************************************************************ // ---------------------- readModule ------------------------------------ // ************************************************************************ int HGeoSet::readModule(HGeoInfo & geoInfo) { HGeoParam ele; HString volName(' ',120); int lName, retVal, noKeepIn=0; int lDetName=detName.length(); int lEleName=eleName.length(); while(!geoInfo.fin.eof()) { geoInfo.fin >> volName; if (volName.empty() || volName(0)=='/') geoInfo.fin.getline(volName,120); else { if ((volName(0,lDetName)==detName) || (volName(0,lEleName)==eleName)) { if (!volName.find(detName)) { readInout(geoInfo,ele); if (ele.inout==1) noKeepIn++; } else ele.inout=1; lName=volName.length(); geoInfo.fin >> ele.mother.assign(10); if (lName<=4) retVal=readParam(geoInfo,ele); else retVal=readCopy(geoInfo,ele); if (retVal==HSUCCESS) { append(volName,ele); #ifdef ILSESHOW showAll(volName,ele); #endif } } } } return noKeepIn; } // ************************************************************************ // ---------------------- readInout ------------------------------------- // ************************************************************************ void HGeoSet::readInout(HGeoInfo & geoInfo, HGeoParam & ele){ char c; do { c=geoInfo.fin.get(); } while (c==' ' || c=='\n'); if (c=='0') ele.inout=0; else { ele.inout=1; if (c!='1') geoInfo.fin.putback(c); } return; } // ************************************************************************ // ---------------------- readVol --------------------------------------- // ************************************************************************ int HGeoSet::readVol(HGeoInfo & geoInfo, HGeoParam & ele) { if (pShapes->readPoints(geoInfo,ele)==HSUCCESS) return HSUCCESS; return HFAILURE; } // ************************************************************************ // ---------------------- readParam ------------------------------------- // ************************************************************************ int HGeoSet::readParam(HGeoInfo & geoInfo, HGeoParam & ele) { geoInfo.fin >> ele.shape.assign(10) >> ele.material.assign(25); if (ele.shape.length()<4) ele.shape += ' '; if (readVol(geoInfo,ele)==HSUCCESS) { geoInfo.fin >> ele.trans >> ele.rot; return HSUCCESS; } return HFAILURE; } // ************************************************************************ // ---------------------- readCopy -------------------------------------- // ************************************************************************ int HGeoSet::readCopy(HGeoInfo & geoInfo, HGeoParam & ele) { char c; do { c=geoInfo.fin.get(); } while (c==' ' || c=='\n'); int i=int(c); geoInfo.fin.putback(c); if (isalpha(i)) { // long version return(readParam(geoInfo,ele)); } geoInfo.fin >> ele.trans >> ele.rot; return HSUCCESS; } // ************************************************************************ // ---------------------- createKeepIn ---------------------------------- // ************************************************************************ int HGeoSet::createKeepIn(HGeoInfo & geoInfo,HGeoMedia & geoMedia) { int secno, matNo, ivolu, no=0; HGeoParam ele; HGeoParam mo; HString volName; int lName=keepinName.length(); int ns=geoInfo.getNs(); do { volName=name(); if (volName(0,lName)==keepinName) { no++; ele=element(); if (ele.mother.find("SEC")>=0) { secno=int(ele.mother(3,1)); if (geoInfo.getnsSet(secno)==0) ele.inout=0; replace(volName,ele); } mo.shape="PGON"; mo.geaPos=0.0F; mo.geaRot=0.0F; mo.geaRot[0]=1.0F; mo.geaRot[4]=1.0F; mo.geaRot[8]=1.0F; HGeoShapePar & shapePar=pShapes->shapePar; if (pShapes->posShape(ele,mo,pShapes->shapePar)==HFAILURE) return HFAILURE; if (ele.inout==1) { if (pShapes->calcShape(ele)==HFAILURE) return HFAILURE; matNo=geoMedia.createMedium(ele.material,geoInfo); #ifdef ILSESHOW cout << volName << " matNo: " << matNo << '\n'; pShapes->showShapePar(); #endif if (!matNo) { cerr << "material for " << volName << " not defined" << endl; return HFAILURE; } HString vtmp=volName(0,4); char* cN=vtmp; char* eS=ele.shape; HString mtmp=ele.mother(0,4); char* moN=mtmp; #ifdef WITHGEANT GSVOLU(cN,eS,matNo,&(shapePar.volPar[0]),shapePar.noPar,ivolu); char cTmp[]="ONLY"; GSPOS(cN,1,moN,shapePar.pos[0],shapePar.pos[1],shapePar.pos[2], shapePar.nRot,cTmp); #endif } replace(volName,ele); } } while (next(1) && no<=ns); if (no>0) return HSUCCESS; else return HFAILURE; } // ************************************************************************ // ---------------------- createModule ---------------------------------- // ************************************************************************ int HGeoSet::createModule(HGeoInfo & geoInfo,HGeoMedia & geoMedia) { int secno, matNo, copyNo, copyId, lName, ivolu; HGeoParam ele; HGeoParam mo; HNamedList copyList; HString volName, copyName; int lKeepinName=keepinName.length(); int lDetName=detName.length(); int lEleName=eleName.length(); do { volName=name(); if ((lDetName>0 && volName(0,lDetName)==detName) || (lEleName>0 && volName(0,lEleName)==eleName)) { lName=volName.length(); ele=element(); if (volName(0,lDetName)==detName) { if (lKeepinName>0 && ele.mother(0,lKeepinName)==keepinName) { mo=peek(ele.mother); if (mo.inout==0) ele.inout=0; } else { mo.geaPos=0.0F; mo.geaRot=0.0F; mo.geaRot[0]=1.0F; mo.geaRot[4]=1.0F; mo.geaRot[8]=1.0F; if (ele.mother.find("CAVE")<0) { mo.shape="PGON"; secno=int(ele.mother(3,1)); if (geoInfo.getnsSet(secno)==0) ele.inout=0; } else mo.shape="BOX "; } } else mo=peek(ele.mother); HGeoShapePar & shapePar=pShapes->shapePar; copyName=volName(0,4); char* cN=copyName; copyNo=1; copyId=1; matNo=0; if (lName>4 && ele.inout==1) { if (copyList.find(copyName)) copyNo=++copyList.element(0); else copyList.append(copyName,copyNo); HString s=volName(4,lName-4); copyId=int(s); } if (pShapes->posShape(ele,mo,shapePar)==HFAILURE) return HFAILURE; replace(volName,ele); if (ele.inout==1) { if (copyNo==1) { if (pShapes->calcShape(ele)==HFAILURE) return HFAILURE; matNo=geoMedia.createMedium(ele.material,geoInfo); if (!matNo) { cerr << "material for " << volName << " not defined" << endl; return HFAILURE; } char* eS=ele.shape; #ifdef WITHGEANT GSVOLU(cN,eS,matNo,&(shapePar.volPar[0]),shapePar.noPar,ivolu); #endif } HString tmp=ele.mother(0,4); char* moN=tmp; #ifdef WITHGEANT char cTmp[]="ONLY"; GSPOS(cN,copyId,moN,shapePar.pos[0],shapePar.pos[1], shapePar.pos[2],shapePar.nRot,cTmp); #endif if (copyNo==1 && geoMedia.getCurrentSensFlag()==1) { if (createHits(volName)==HFAILURE) { cerr << "cannot create hits for " << volName << endl; return HFAILURE; } } #ifdef ILSESHOW cout << volName << " matNo: " << matNo << '\n'; pShapes->showShapePar(); #endif } } } while(next(1)); return HSUCCESS; } // ************************************************************************ // ---------------------- readHits ---------------------------------- // ************************************************************************ int HGeoSet::readHits(HString & hitsFileName) { ifstream hfin; HString tmp(' ',120),s1(' ',4); int i; hfin.open(hitsFileName,ios::in); if(hfin.fail()) { hfin.close(); hfin.open(hitsFileName,ios::in); } if(hfin.fail()) { cerr << "failed to open file " << hitsFileName << endl; return HFAILURE; } while(!hfin.eof()) { hfin >> tmp; if (tmp.empty() || tmp(0)=='/') hfin.getline(tmp,120); else { chset=tmp(0,4); hfin >> nh; if (nh>=nhmax) { cerr<<"number of hit components too large, only "<> tmp; s1.replace(0,tmp); strcpy(chnamh[i],s1); s1.replace(0," "); } for(i=0;i> nbitsh[i]; } for(i=0;i> orig[i]; } for(i=0;i> fact[i]; } hitsRead=1; hfin.close(); return HSUCCESS; } } hfin.close(); return HFAILURE; } // ************************************************************************ // ---------------------- restoreTree ----------------------------------- // ************************************************************************ int HGeoSet::restoreTree(HString & volName) { HString tName; HGeoParam tEle; char treeNames[10][5]; int i, j, copyNo[10]; int lDetName=detName.length(); tName=volName; nv=0; while (tName(0,1)==detName(0,1)) { if (nv>=nvmax) { cerr<<"number of levels in tree too large, only "< maxCopyNum) maxCopyNum = int(s); } } while (next()); copyNo[nv]=maxCopyNum; } nv++; if (tName(0,lDetName)==detName) break; tName=tEle.mother; } for(j=0,i=nv-1;j=0) break; ele=peek(ele.mother); } for(int i=3;i--;refPos[i]=(float)Round(tref[i],3)); return; } // ************************************************************************ // ---------------------- showAll --------------------------------------- // ************************************************************************ void HGeoSet::showAll(HString & volName, HGeoParam & ele) { HString bl=" "; cout << volName << bl << ele.inout << bl << ele.mother << bl << ele.shape << bl << ele.material << '\n'; for(int i=0;iwritePoints(fout,ele)==HFAILURE) { fout.close(); return HFAILURE; } fout<tm_mday<10) day="0" + HString(newtime->tm_mday); else day=HString(newtime->tm_mday); if (newtime->tm_mon<9) month="0" + HString((newtime->tm_mon)+1); else month=HString((newtime->tm_mon)+1); sdate=day + month + HString(newtime->tm_year); if (outDir.empty()) outputName=modName + "db" + sdate +".txt"; else { outDir=outDir + "/"; outputName=outDir + modName + "db" + sdate +".txt"; } do { ftest.open(outputName,ios::in); if(ftest.fail()) writeFlag=1; else { cout << "file " << outputName << " exists already" << '\n'; cout << "do you want to enter a new file name? (y or n, e=exit) >"; cin >> tmp; newchoice=tmp.lower(); if (newchoice(0,1)=="n") writeFlag=1; else { if (newchoice(0,1)=="y") { cout << "enter file name > "; cin >> tmp; if (tmp.find("/")>=0) outputName=tmp; else outputName=outDir + tmp; } else { ftest.close(); exit(1); } } } ftest.close(); ftest.clear(); } while (writeFlag==0); cout << "output file name for " << modName <<": " << outputName << endl; fout.open(outputName,ios::out); if(fout.fail()) { cerr << "cannot open " << outputName << endl; fout.close(); exit(1); } fout<<"// ***********************************************************"<<'\n'; fout<<"// geometry file for " << modName << " extracted from database"<<'\n'; fout<<"// Date: " << asctime(newtime); fout<<"// ***********************************************************"<<'\n'; fout.setf(ios::fixed,ios::floatfield); return HSUCCESS; } // ************************************************************************ // ---------------------- calcMotherTransform --------------------------- // ************************************************************************ int HGeoSet::calcMotherTransform(HGeoInfo & geoInfo, HGeoParam & ele) { #ifdef WITHORACLE HGeoParam mo; DVec v(0.0,3); DVec t(0.0,3); DVec r(0.0,9); DVec rmt(0.0,9); if (index(ele.mother)>=0) mo=peek(ele.mother); else { if (geoInfo.geoDb->getTransform(ele.moObjIndex,mo.labTrans,mo.labRot) ==HFAILURE) return HFAILURE; } transposeMatrix(mo.labRot,rmt); if (ele.labRot.norm(mo.labRot)<0.000001) ele.rot=uniRot; else { multiplyMatrices(rmt,ele.labRot,r); for(int i=9;i--;ele.rot[i]=(float)Round(r[i],6)); } v=ele.labTrans - mo.labTrans; rotateVector(rmt,v,t); for(int i=3;i--;ele.trans[i]=(float)Round(t[i],3)); #endif return HSUCCESS; } // ************************************************************************ // ---------------------- transposeMatrix ------------------------------- // ************************************************************************ void HGeoSet::transposeMatrix(DVec & r, DVec & tr) { for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { tr[j+3*i]=r[i+3*j]; } } return; } // ************************************************************************ // ---------------------- multiplyMatrices ------------------------------ // ************************************************************************ void HGeoSet::multiplyMatrices(DVec & lr, DVec & rr, DVec & pr) { pr=0.0F; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { int n=3*i+j; for(int k=0;k<3;k++) { pr[n]+=lr[3*i+k]*rr[3*k+j]; } } } return; } // ************************************************************************ // ---------------------- rotateVector ---------------------------------- // ************************************************************************ void HGeoSet::rotateVector(DVec & r, DVec & v, DVec & rv) { rv=0.0F; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { rv[i]+=r[3*i+j]*v[j]; } } return; } // ************************************************************************ // ---------------------- roundDVec ------------------------------------ // ************************************************************************ void HGeoSet::roundDVec(DVec & v, const int n) { for(int i=v.length();i--;v[i]=Round(v[i],n)); } // ************************************************************************ // ---------------------- show.... -------------------------------------- // ************************************************************************ // shows the unique substring of the name of the keepin-volume HString HGeoSet::showKeepinName() {return keepinName;} // shows the unique substring of the name of the module HString HGeoSet::showDetName() {return detName;} // shows the substring of the names of all daughtervolumes of the module HString HGeoSet::showEleName() {return eleName;}