1#include "GaudiKernel/Algorithm.h"
2#include "MdcGeomSvc/MdcGeomSvc.h"
3#include "GaudiKernel/Kernel.h"
4#include "GaudiKernel/IIncidentSvc.h"
5#include "GaudiKernel/Incident.h"
6#include "GaudiKernel/IIncidentListener.h"
7#include "GaudiKernel/IInterface.h"
8#include "GaudiKernel/StatusCode.h"
9#include "GaudiKernel/SvcFactory.h"
10#include "GaudiKernel/PropertyMgr.h"
11#include "GaudiKernel/SmartDataPtr.h"
12#include "CalibData/CalibModel.h"
13#include "CalibData/Mdc/MdcAlignData.h"
14#include "GaudiKernel/MsgStream.h"
15#include "EventModel/EventHeader.h"
16#include "EventModel/Event.h"
17#include "GaudiKernel/ISvcLocator.h"
18#include "GaudiKernel/IDataProviderSvc.h"
19#include "GaudiKernel/Bootstrap.h"
31 if(getenv(
"MDCGEOMSVCROOT")){
32 m_alignFilePath = std::string(getenv(
"MDCGEOMSVCROOT"))+std::string(
"/share/MdcAlignPar.dat");
35 m_wirePosFilePath = std::string(getenv(
"MDCGEOMSVCROOT"))+std::string(
"/share/WirePosCalib.dat");
38 m_wireTensionFilePath = std::string(getenv(
"MDCGEOMSVCROOT"))+std::string(
"/share/mdcWireTension.dat");
43 std::cout<<
"A fatal error, contact wangjk..."<<std::endl;
46 declareProperty(
"doSag",
m_doSag =
true);
49 declareProperty(
"wholeShiftX",m_wholeShiftX = 0.);
50 declareProperty(
"wholeShiftY",m_wholeShiftY = 0.);
51 declareProperty(
"wholeShiftZ",m_wholeShiftZ = 0.);
52 declareProperty(
"wholeRotatX",m_wholeRotatX = 0.);
53 declareProperty(
"wholeRotatY",m_wholeRotatY = 0.);
54 declareProperty(
"wholeRotatZ",m_wholeRotatZ = 0.);
55 declareProperty(
"alignFilePath",m_alignFilePath);
56 declareProperty(
"wirePosFilePath",m_wirePosFilePath);
57 declareProperty(
"wireTensionFilePath",m_wireTensionFilePath);
64 if ( IID_IMdcGeomSvc.versionMatch(riid) ) {
67 return Service::queryInterface(riid, ppvInterface) ;
69 return StatusCode::SUCCESS;
73 MsgStream log(messageService(), name());
74 log << MSG::INFO << name() <<
": Start of run initialisation" << endreq;
76 StatusCode sc = Service::initialize();
77 if ( sc.isFailure() )
return sc;
79 m_updataalign =
false;
81 sc = service(
"IncidentSvc", incsvc);
84 incsvc -> addListener(
this,
"NewRun", priority);
90 sc = service(
"CalibDataSvc", m_pCalibDataSvc,
true);
92 if ( !sc.isSuccess() ) {
94 <<
"Could not get IDataProviderSvc interface of CalibXmlCnvSvc"
99 <<
"Retrieved IDataProviderSvc interface of CalibXmlCnvSvc"
103 return StatusCode::SUCCESS;
107 MsgStream log(messageService(), name());
108 log << MSG::INFO << name() <<
": End of Run" << endreq;
109 return StatusCode::SUCCESS;
113 for(vector<MdcGeoLayer*>::iterator it1 = fLayers.begin(); it1 != fLayers.end(); it1++)
delete *it1;
114 for(vector<MdcGeoSuper*>::iterator it2 = fSupers.begin(); it2 != fSupers.end(); it2++)
delete *it2;
115 for(vector<MdcGeoWire*>::iterator it3 = fWires.begin(); it3 != fWires.end(); it3++)
delete *it3;
116 for(vector<MdcGeoEnd*>::iterator it4 = fEnd.begin(); it4 != fEnd.end(); it4++)
delete *it4;
124void MdcGeomSvc::clean(){
125 for(vector<MdcGeoLayer*>::iterator it1 = fLayers.begin(); it1 != fLayers.end(); it1++)
delete *it1;
126 for(vector<MdcGeoSuper*>::iterator it2 = fSupers.begin(); it2 != fSupers.end(); it2++)
delete *it2;
127 for(vector<MdcGeoWire*>::iterator it3 = fWires.begin(); it3 != fWires.end(); it3++)
delete *it3;
128 for(vector<MdcGeoEnd*>::iterator it4 = fEnd.begin(); it4 != fEnd.end(); it4++)
delete *it4;
137void MdcGeomSvc::ReadFilePar(){
138 std::string geometryFilePath = getenv(
"MDCSIMROOT");
139 geometryFilePath +=
"/dat/Mdc.txt";
140 std::ifstream inFile(geometryFilePath.c_str() );
143 std::cout<<
"Error, mdc parameters file not exist"<<std::endl;
146 std::string alignFilePath;
147 std::string wirePosFilePath;
148 std::string wireTensionFilePath;
149 if (!m_updataalign) {
150 alignFilePath = std::string(getenv(
"MDCGEOMSVCROOT"))+std::string(
"/share/MdcAlignPar.dat");
151 wirePosFilePath = std::string(getenv(
"MDCGEOMSVCROOT"))+std::string(
"/share/WirePosCalib.dat");
152 wireTensionFilePath = std::string(getenv(
"MDCGEOMSVCROOT"))+std::string(
"/share/mdcWireTension.dat");
154 alignFilePath = m_alignFilePath;
155 wirePosFilePath = m_wirePosFilePath;
156 wireTensionFilePath = m_wireTensionFilePath;
158 std::ifstream alignFile(alignFilePath.c_str());
159 std::ifstream wirePosFile(wirePosFilePath.c_str());
160 std::ifstream wireTensionFile(wireTensionFilePath.c_str());
163 std::cout<<
"Error, mdc wire position file not exist..."<<std::endl;
166 if(!wireTensionFile){
167 std::cout<<
"Error, mdc wire tension file not exist..."<<std::endl;
170 std::vector<double> wireTensionVec;
174 ReadTensionDataBase(wireTensionVec);
176 cout <<
"Read wireTension from file:" << wireTensionFilePath.c_str() << endl;
179 for(
int ii=0; ii<6796; ii++){
180 wireTensionFile>>idd>>tension;
183 wireTensionVec.push_back(tension);
187 wireTensionVec.push_back(tension);
192 std::vector<vector<double> > wirePosVec(6796);
194 ReadWirePosDataBase(wirePosVec);
196 cout <<
"Read wirePosition from file:" << wirePosFilePath.c_str() << endl;
197 double dxe,dye,dze,dxw,dyw,dzw;
200 getline(wirePosFile, title);
201 wirePosFile.seekg(1,ios::cur);
202 for(
int j=0; j<6796; j++){
203 wirePosFile>>wid>>dxe>>dye>>dze>>dxw>>dyw>>dzw;
204 wirePosVec[j].push_back(dxe);
205 wirePosVec[j].push_back(dye);
206 wirePosVec[j].push_back(dze);
207 wirePosVec[j].push_back(dxw);
208 wirePosVec[j].push_back(dyw);
209 wirePosVec[j].push_back(dzw);
219 double signalWireR, fieldWireR;
220 int TLayerNo, TWireNo,TSignalLayerNo,fSignalLayer[50];
221 int i,
wireNo, firstWire,segmentNo,signalLayer;
222 double length, nomphi, phi, r,nomaadiv2, aadiv2,
223 nomaa, aa, nomrotateCell, rotateCell, wlength, innerR, outR, z;
224 std::string layer, name, line;
226 vector<int> WireNo, fSegmentNo;
227 vector<double> wLength,
R, nomPhi,
Phi,nomadiv2, adiv2,
First,
228 noma, a, nomebusing, ebusing, nomsigma, sigma, nomdeltaz, deltaz,
229 nomRotate, Rotate, fLength, fInnerR, fOutR, fZ;
230 vector<string> LayerName, fName;
231 vector<double> Sx,Sy,Sz,Rx,Ry,Rz;
232 double tmp1,tmp2,tmp3,tmp4,tmp5,tmp6;
237 ReadAliParDataBase(Sx, Sy, Sz, Rx, Ry, Rz);
239 cout <<
"Read alignment parameters from file:" << alignFilePath.c_str() << endl;
240 getline(alignFile, line);
241 alignFile.seekg(1,ios::cur);
242 for(
int k=0;k<16;k++){
243 alignFile>>line>>tmp1>>tmp2>>tmp3>>tmp4>>tmp5>>tmp6;
253 Sx.push_back(m_wholeShiftX+tmp1);
254 Sy.push_back(m_wholeShiftY+tmp2);
255 Sz.push_back(m_wholeShiftZ+tmp3);
256 Rx.push_back(m_wholeRotatX+tmp4);
257 Ry.push_back(m_wholeRotatY+tmp5);
258 Rz.push_back(m_wholeRotatZ+tmp6);
264 getline(inFile, line);
265 inFile>>TLayerNo>>TWireNo>>TSignalLayerNo>>signalWireR>>fieldWireR;
266 inFile.seekg(1,ios::cur);
267 getline(inFile, line);
268 for( i=0; i<TSignalLayerNo; i++){
271 fSignalLayer[i]=signalLayer-1;
274 inFile.seekg(1,ios::cur);
275 getline(inFile, line);
276 getline(inFile, line);
279 double delta_rotateCell;
281 for( i=0; i<TLayerNo; i++){
282 i_east=getAlignParIndexEast(i);
283 i_west=getAlignParIndexWest(i);
285 inFile>>layer>>
wireNo>>wlength>>r>>nomphi>>firstWire>>nomrotateCell;
286 getline(inFile, line);
290 nomaadiv2=2*
pi*nomrotateCell/
wireNo;
292 nomaa=2*r*
sin(nomaadiv2);
293 delta_rotateCell=(Rz[i_west]-Rz[i_east])*
wireNo/(4*
pi);
294 rotateCell=nomrotateCell+delta_rotateCell;
298 double shiftPhi=twopi/
wireNo;
300 if(nomphi<0) nomphi += shiftPhi;
301 phi=nomphi+Rz[i_east];
302 LayerName.push_back(layer);
304 wLength.push_back(wlength);
306 nomPhi.push_back(nomphi);
310 First.push_back(firstWire);
318 nomebusing.push_back(atan(nomaa/wlength));
319 ebusing.push_back(atan(aa/wlength));
325 nomRotate.push_back(nomrotateCell);
326 Rotate.push_back(rotateCell);
329 getline(inFile, line);
331 inFile.seekg(1,ios::cur);
332 getline(inFile, line);
333 getline(inFile, line);
335 for(i=0; i<segmentNo; i++){
336 inFile>>length>>innerR>>outR>>z>>name;
337 getline(inFile,line);
338 fLength.push_back(length);
339 fInnerR.push_back(innerR);
340 fOutR.push_back(outR);
342 fName.push_back(name);
354 double a1,a2,a3,nomphi0,phi0,cellphi;
355 double noma1,noma2,noma3;
356 for(i=0;i<TLayerNo;i++){
357 i_east=getAlignParIndexEast(i);
358 i_west=getAlignParIndexWest(i);
363 hlh.
NCell(WireNo[i]/2);
370 hlh.
First((
int)First[i]);
371 HepPoint3D offF(Sx[i_west],Sy[i_west],Sz[i_west]);
372 HepPoint3D offB(Sx[i_east],Sy[i_east],Sz[i_east]);
379 hlh.
Shift(Rotate[i]);
394 fGenerals.push_back(hlh);
398 for(i=0;i<TSignalLayerNo;i++){
402 int lyr=fSignalLayer[i];
405 i_east=getAlignParIndexEast(lyr);
406 i_west=getAlignParIndexWest(lyr);
418 case 0: suph->
Type(-1);
break;
419 case 1: suph->
Type( 1);
break;
420 case 2: suph->
Type( 0);
break;
421 case 3: suph->
Type( 0);
break;
422 case 4: suph->
Type( 0);
break;
423 case 5: suph->
Type(-1);
break;
424 case 6: suph->
Type( 1);
break;
425 case 7: suph->
Type(-1);
break;
426 case 8: suph->
Type( 1);
break;
427 case 9: suph->
Type( 0);
break;
428 case 10: suph->
Type(0);
break;
432 fSupers.push_back(suph);
435 cellphi=2*twopi/WireNo[lyr];
437 phi0=
Phi[lyr]+
abs(First[lyr]-1)*(cellphi/2);
438 nomphi0=nomPhi[lyr]+
abs(First[lyr]-1)*(cellphi/2);
441 for(
int wk=0; wk<i; wk++){
442 int lyrwk=fSignalLayer[wk];
443 wircount=wircount + WireNo[lyrwk]/2;
448 lh->
Slant(ebusing[lyr]);
452 lh->
RCSiz1(R[lyr]-R[lyr-1]);
453 lh->
RCSiz2(R[lyr+1]-R[lyr]);
454 lh->
PCSiz(R[lyr]*cellphi);
455 lh->
NCell(WireNo[lyr]/2);
457 lh->
Shift(Rotate[lyr]);
461 vector<MdcGeoSuper*>::iterator it1 = fSupers.end()-1;
463 fLayers.push_back(lh);
476 for(
int j=0;j<WireNo[lyr]/2;j++){
479 const int wireId = wircount+j;
483 a1 =
R[lyr]*
cos(phi0+j*cellphi)+Sx[i_east]+wirePosVec[wireId][0];
484 a2 =
R[lyr]*
sin(phi0+j*cellphi)+Sy[i_east]+wirePosVec[wireId][1];
485 a3 = wLength[lyr]/2+wirePosVec[wireId][2]+Sz[i_east];
489 double yb = a2 *
cos(Rx[i_east]) + a3 *
sin(Rx[i_east]);
490 double zb = a3 *
cos(Rx[i_east]) - a2 *
sin(Rx[i_east]);
493 double xbnew = xb *
cos(Ry[i_east]) - zb *
sin(Ry[i_east]);
495 double zbnew = xb *
sin(Ry[i_east]) + zb *
cos(Ry[i_east]);
497 noma1 =
R[lyr]*
cos(nomphi0+j*cellphi);
498 noma2 =
R[lyr]*
sin(nomphi0+j*cellphi);
500 noma3 = wLength[lyr]/2;
504 HepPoint3D wireposb(wirePosVec[wireId][0],wirePosVec[wireId][1],wirePosVec[wireId][2]);
514 a1 =
R[lyr]*
cos(phi0+(j+lh->
Shift())*cellphi)+Sx[i_west]+wirePosVec[wireId][3];
515 a2 =
R[lyr]*
sin(phi0+(j+lh->
Shift())*cellphi)+Sy[i_west]+wirePosVec[wireId][4];
517 a3 = -wLength[lyr]/2+wirePosVec[wireId][5]+Sz[i_west];
520 double yf = a2 *
cos(Rx[i_west]) + a3 *
sin(Rx[i_west]);
521 double zf = a3 *
cos(Rx[i_west]) - a2 *
sin(Rx[i_west]);
524 double xfnew = xf *
cos(Ry[i_west]) - zf *
sin(Ry[i_west]);
526 double zfnew = xf *
sin(Ry[i_west]) + zf *
cos(Ry[i_west]);
530 noma3 = -wLength[lyr]/2;
534 HepPoint3D wireposf(wirePosVec[wireId][3],wirePosVec[wireId][4],wirePosVec[wireId][5]);
548 wh->
Slant(ebusing[lyr]);
552 wh->
Tension(wireTensionVec[wireId]);
559 vector<MdcGeoLayer*>::iterator it2 = fLayers.end()-1;
561 fWires.push_back(wh);
567 fMisc.
OuterTk(fOutR[1]-fInnerR[1]);
568 fMisc.
InnerTk(fOutR[2]-fInnerR[2]);
569 fMisc.
NSWire(fWires.size());
570 fMisc.
NFWire(TWireNo-fWires.size());
575 fMisc.
SWireR(signalWireR);
578 for(i=0;i<segmentNo;i++){
590void MdcGeomSvc::ReadTensionDataBase(std::vector<double> & wireTensionVec){
591 std::string fullPath =
"/Calib/MdcAlign";
592 MsgStream log(messageService(), name());
593 log << MSG::INFO<<
"ReadTensionDataBase() wireTensionPath = "<<fullPath<< endreq;
594 cout <<
"Read wireTension from Calibration Database!" << endl;
596 SmartDataPtr<CalibData::MdcAlignData> tension(m_pCalibDataSvc, fullPath);
598 log << MSG::ERROR <<
"can not get MdcAlignConst via SmartPtr"
602 for(
int i=0;i<6796;i++){
603 double tens = tension->gettension(i);
604 wireTensionVec.push_back(tens);
607void MdcGeomSvc::ReadWirePosDataBase(std::vector<vector<double> > & wirePosVec){
608 std::string fullPath =
"/Calib/MdcAlign";
609 MsgStream log(messageService(), name());
610 log << MSG::INFO<<
"ReadWirePosDataBase() wirePositionPath = "<<fullPath<< endreq;
612 cout <<
"Read wirePosition from Calibration Database!" << endl;
613 SmartDataPtr<CalibData::MdcAlignData> wirepos(m_pCalibDataSvc, fullPath);
615 log << MSG::ERROR <<
"can not get MdcAlignConst via SmartPtr"
619 double dxe,dye,dze,dxw,dyw,dzw;
620 for(
int j=0; j<6796; j++){
622 dxe = wirepos->getdxWireEast(j);
623 dye = wirepos->getdyWireEast(j);
624 dze = wirepos->getdzWireEast(j);
625 dxw = wirepos->getdxWireWest(j);
626 dyw = wirepos->getdyWireWest(j);
627 dzw = wirepos->getdzWireWest(j);
629 wirePosVec[j].push_back(dxe);
630 wirePosVec[j].push_back(dye);
631 wirePosVec[j].push_back(dze);
632 wirePosVec[j].push_back(dxw);
633 wirePosVec[j].push_back(dyw);
634 wirePosVec[j].push_back(dzw);
638void MdcGeomSvc::ReadAliParDataBase(vector<double>& Sx, vector<double>& Sy, vector<double>& Sz,
639 vector<double>& Rx, vector<double>& Ry, vector<double>& Rz){
640 MsgStream log(messageService(), name());
641 std::string fullPath =
"/Calib/MdcAlign";
642 log << MSG::INFO<<
"ReadAliParDataBase() alignParPath = "<<fullPath<< endreq;
643 cout <<
"Read alignment parameters from Calibration Database!" << endl;
645 SmartDataPtr<CalibData::MdcAlignData> alignpar(m_pCalibDataSvc, fullPath);
647 log << MSG::ERROR <<
"can not get MdcAlignConst via SmartPtr"
651 double tmp1,tmp2,tmp3,tmp4,tmp5,tmp6;
652 for(
int k=0;k<16;k++){
653 tmp1 = alignpar->getdxEP(k);
654 tmp2 = alignpar->getdyEP(k);
655 tmp3 = alignpar->getdzEP(k);
656 tmp4 = alignpar->getrxEP(k);
657 tmp5 = alignpar->getryEP(k);
658 tmp6 = alignpar->getrzEP(k);
660 Sx.push_back(m_wholeShiftX+tmp1);
661 Sy.push_back(m_wholeShiftY+tmp2);
662 Sz.push_back(m_wholeShiftZ+tmp3);
663 Rx.push_back(m_wholeRotatX+tmp4);
664 Ry.push_back(m_wholeRotatY+tmp5);
665 Rz.push_back(m_wholeRotatZ+tmp6);
671 return fWires.size();
676 return fLayers.size();
681 return fSupers.size();
686 return fGenerals.size();
696const int MdcGeomSvc::getAlignParIndexEast(
int lyr)
const{
698 if((lyr>=0) && (lyr<=16))
return 0;
700 else if((lyr>=17) && (lyr<=20))
return 1;
702 else if((lyr>=21) && (lyr<=24))
return 2;
704 else if((lyr>=25) && (lyr<=28))
return 3;
706 else if((lyr>=29) && (lyr<=32))
return 4;
708 else if((lyr>=33) && (lyr<=36))
return 5;
710 else if((lyr>=37) && (lyr<=41))
return 6;
712 else if(lyr>=42)
return 7;
713 else std::cout<<
" Hi, ERROR OCCUR !!!"<<std::endl;
718const int MdcGeomSvc::getAlignParIndexWest(
int lyr)
const{
720 if((lyr>=0) && (lyr<=16))
return 8;
722 else if((lyr>=17) && (lyr<=20))
return 9;
724 else if((lyr>=21) && (lyr<=24))
return 10;
726 else if((lyr>=25) && (lyr<=28))
return 11;
728 else if((lyr>=29) && (lyr<=32))
return 12;
730 else if((lyr>=33) && (lyr<=36))
return 13;
732 else if((lyr>=37) && (lyr<=41))
return 14;
734 else if(lyr>=42)
return 15;
735 else std::cout<<
" Hi, ERROR OCCUR !!!"<<std::endl;
742 MsgStream log( messageService(), name() );
743 log << MSG::DEBUG <<
"handle: " << inc.type() << endreq;
744 IDataProviderSvc* m_eventSvc;
745 Gaudi::svcLocator()->service(
"EventDataSvc", m_eventSvc,
true);
746 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,
"/Event/EventHeader");
748 log << MSG::FATAL <<
"Could not find Event Header" << endreq;
750 if (m_updataalign)
return;
751 if (inc.type() ==
"NewRun" ){
752 log << MSG::DEBUG <<
"Begin Event" << endreq;
754 m_updataalign =
true;
756 int RunNo=eventHeader->runNumber();
760 cout<<
"m__RunNo="<<RunNo<<
"m_mindex="<<m_mindex<<endl;
769 if (
id < fWires.size())
777 if ((lyrid <fLayers.size()) && ((
int) wirid <
Layer(lyrid)->NCell()))
785 if (
id < fLayers.size())
793 if (
id < fSupers.size())
801 if (
id < fGenerals.size())
802 return & fGenerals[id];
814 if (
id < fEnd.size())
double Phi(RecMdcKalTrack *trk)
double sin(const BesAngle a)
double cos(const BesAngle a)
double Length(void) const
double InnerR(void) const
double Length(void) const
double nomShift(void) const
double TwistB(void) const
double SzWest(void) const
HepPoint3D OffB(void) const
double nomPhi(void) const
double RxEast(void) const
double SxEast(void) const
double TwistF(void) const
double Offset(void) const
double SyWest(void) const
double SxWest(void) const
string LayerName(void) const
double RxWest(void) const
double nomOffset(void) const
double RyWest(void) const
HepPoint3D OffF(void) const
double SzEast(void) const
double RzWest(void) const
double RzEast(void) const
double SyEast(void) const
double RyEast(void) const
double Radius(void) const
double Radius(void) const
double RCSiz2(void) const
double Length(void) const
MdcGeoSuper * Sup(void) const
double nomShift(void) const
double nomSlant(void) const
double RCSiz1(void) const
double Offset(void) const
double nomOffset(void) const
double FWireR(void) const
double OuterTk(void) const
double InnerR(void) const
double OuterR(void) const
double SWireR(void) const
double InnerTk(void) const
double LowerR(void) const
double UpperR(void) const
HepPoint3D FWirePos(void) const
HepPoint3D BWirePos(void) const
MdcGeoLayer * Lyr(void) const
HepPoint3D nomForward(void) const
HepPoint3D Forward(void) const
HepPoint3D nomBackward(void) const
HepPoint3D Backward(void) const
double Tension(void) const
double nomSlant(void) const
static bool m_readAlignParDataBase
MdcGeomSvc(const std::string &name, ISvcLocator *svcloc)
virtual StatusCode initialize()
static bool getSagFlag(void)
const MdcGeoSuper *const SuperLayer(unsigned id)
const MdcGeoWire *const Wire(unsigned id)
const MdcGeoGeneral *const GeneralLayer(unsigned id)
void handle(const Incident &inc)
this handle function is prepared for special use
const int getSuperLayerSize()
virtual StatusCode finalize()
virtual StatusCode queryInterface(const InterfaceID &riid, void **ppvUnknown)
const MdcGeoEnd *const End(unsigned id)
const MdcGeoLayer *const Layer(unsigned id)
const int getGeneralLayerSize()
const MdcGeoMisc *const Misc(void)
static bool m_nomcalignment
complex_t R(double Q2, double M2, double G, double Mp2, double Mm2)