1#include "GaudiKernel/AlgFactory.h"
2#include "GaudiKernel/MsgStream.h"
3#include "GaudiKernel/SmartDataPtr.h"
5#include "GaudiKernel/NTuple.h"
6#include "GaudiKernel/INTuple.h"
7#include "GaudiKernel/INTupleSvc.h"
8#include "GaudiKernel/SmartDataPtr.h"
10#include "CLHEP/Geometry/Vector3D.h"
11#include "CLHEP/Geometry/Point3D.h"
12#include "CLHEP/Units/PhysicalConstants.h"
14#include "MagneticField/IMagneticFieldSvc.h"
15#include "MagneticField/MagFieldReader.h"
18#include "CLHEP/Random/RandFlat.h"
28#ifndef ENABLE_BACKWARDS_COMPATIBILITY
32#ifndef ENABLE_BACKWARDS_COMPATIBILITY
48 ISvcLocator* pSvcLocator)
49 : Algorithm ( name , pSvcLocator )
52 declareProperty(
"Zmin", m_zMin = -1200.0*mm);
53 declareProperty(
"Zmax", m_zMax = 1200.0*mm);
54 declareProperty(
"Step", m_step = 50.0*mm);
55 declareProperty(
"Xmin", m_xMin = -900.0*mm);
56 declareProperty(
"Xmax", m_xMax = 900.0*mm);
57 declareProperty(
"Ymin", m_yMin = -900.0*mm);
58 declareProperty(
"Ymax", m_yMax = 900.0*mm);
59 declareProperty(
"filename", m_filename =
"rawmode3_out.dat");
67 MsgStream msg(
msgSvc(), name());
69 msg << MSG::INFO <<
"FieldReader intialize() has been called" << endreq;
70 StatusCode status = service(
"MagneticFieldSvc", m_pIMF,
true);
71 if( !status.isSuccess() ) {
72 msg << MSG::FATAL <<
"Unable to open Magnetic field service"
74 return StatusCode::FAILURE;
77 msg << MSG::DEBUG <<
" Book ntuple" << endreq;
80 if(nt1) m_tuple1 = nt1;
82 m_tuple1 =
ntupleSvc()->book(
"FILE1/n1",CLID_ColumnWiseTuple,
"Field");
84 status = m_tuple1->addItem(
"x", m_x );
85 status = m_tuple1->addItem(
"y", m_y );
86 status = m_tuple1->addItem(
"z", m_z );
87 status = m_tuple1->addItem(
"r", m_r );
88 status = m_tuple1->addItem(
"Bx", m_Bx );
89 status = m_tuple1->addItem(
"By", m_By );
90 status = m_tuple1->addItem(
"Bz", m_Bz );
91 status = m_tuple1->addItem(
"SigmaBx", m_sigma_bx );
92 status = m_tuple1->addItem(
"SigmaBy", m_sigma_by );
93 status = m_tuple1->addItem(
"SigmaBz", m_sigma_bz );
96 msg << MSG::ERROR <<
" Cannot book N-tuple:" <<long(m_tuple1)<< endreq;
97 return StatusCode::FAILURE;
102 if(nt2) m_tuple2 = nt2;
104 m_tuple2 =
ntupleSvc()->book(
"FILE1/n2",CLID_ColumnWiseTuple,
"Field");
106 status = m_tuple2->addItem(
"x", m_x2 );
107 status = m_tuple2->addItem(
"y", m_y2 );
108 status = m_tuple2->addItem(
"z", m_z2 );
109 status = m_tuple2->addItem(
"r", m_r2 );
110 status = m_tuple2->addItem(
"Bx", m_Bx2 );
111 status = m_tuple2->addItem(
"By", m_By2 );
112 status = m_tuple2->addItem(
"Bz", m_Bz2 );
115 msg << MSG::ERROR <<
" Cannot book N-tuple:" <<long(m_tuple2)<< endreq;
116 return StatusCode::FAILURE;
121 if(nt3) m_tuple3 = nt3;
123 m_tuple3 =
ntupleSvc()->book(
"FILE1/n3",CLID_ColumnWiseTuple,
"Field");
125 status = m_tuple3->addItem(
"x", m_x3 );
126 status = m_tuple3->addItem(
"y", m_y3 );
127 status = m_tuple3->addItem(
"z", m_z3 );
128 status = m_tuple3->addItem(
"r", m_r3 );
129 status = m_tuple3->addItem(
"phi", m_phi3 );
130 status = m_tuple3->addItem(
"Bx", m_Bx3 );
131 status = m_tuple3->addItem(
"By", m_By3 );
132 status = m_tuple3->addItem(
"Bz", m_Bz3 );
135 msg << MSG::ERROR <<
" Cannot book N-tuple:" <<long(m_tuple3)<< endreq;
136 return StatusCode::FAILURE;
141 if(nt4) m_tuple4 = nt4;
143 m_tuple4 =
ntupleSvc()->book(
"FILE1/n4",CLID_ColumnWiseTuple,
"Field");
145 status = m_tuple4->addItem(
"time", m_time );
148 msg << MSG::ERROR <<
" Cannot book N-tuple:" <<long(m_tuple4)<< endreq;
149 return StatusCode::FAILURE;
153 status = service(
"BesTimerSvc", m_timersvc);
154 if (status.isFailure()) {
155 msg << MSG::ERROR << name() <<
": Unable to locate BesTimer Service" << endreq;
156 return StatusCode::FAILURE;
158 m_timer = m_timersvc->
addItem(
"Read field Time");
160 msg << MSG::INFO <<
"MagFieldReader initialized" << endreq;
161 return StatusCode::SUCCESS;
169 MsgStream msg(
msgSvc(), name() );
171 msg << MSG::DEBUG <<
"==> Execute MagFieldReader" << endreq;
281 for(
int i = 0; i < 20000; i++)
283 double px,py,pz,bx,by,bz;
284 double max_x = 1.8*m, min_x = -1.8*m, max_y = 1.8*m, min_y = -1.8*m, max_z = 2.1*m, min_z = -2.1*m;
285 px = min_x + (max_x - min_x)*RandFlat::shoot();
286 py = min_y + (max_y - min_y)*RandFlat::shoot();
287 pz = min_z + (max_z - min_z)*RandFlat::shoot();
306 msg << MSG::INFO <<
"MagFieldReader executed" << endreq;
307 return StatusCode::SUCCESS;
315 MsgStream msg(
msgSvc(), name());
316 msg << MSG::DEBUG <<
"==> Finalize MagFieldReader" << endreq;
318 return StatusCode::SUCCESS;
326StatusCode MagFieldReader::readField() {
328 MsgStream msg(
msgSvc(), name() );
329 msg << MSG::DEBUG <<
"m_pIMF = " << m_pIMF << endreq;
332 msg << MSG::DEBUG <<
"The reference field is " << referfield <<
" tesla" <<endreq;
334 if(ifrealfield) msg << MSG::DEBUG <<
"Use the real field"<<endreq;
335 else msg << MSG::DEBUG <<
"Use the fake field"<<endreq;
371 double bphi1[
n2],bphi2[
n2],bphi3[
n2],bphi4[
n2],bphi5[
n2],bphi6[
n2];
374 double x2[n3],bx1[n3],bx2[n3],bx3[n3],bx4[n3],bx5[n3],bx6[n3];
375 double by1[n3],by2[n3],by3[n3],by4[n3],by5[n3],by6[n3];
378 double globle_x[n4],globle_bz[n4];
382 for( px = -2600; px <= 2600; px += 10 ) {
383 pos[0] = px; pos[1] = 0; pos[2] = 0;
386 globle_bz[i]=B.z()/tesla;
390 for ( pz = m_zMin; pz <= m_zMax; pz += 10 ) {
391 pos[0] = 0; pos[1] = 0; pos[2] = pz;
396 pos[0] = 200; pos[1] = 0; pos[2] = pz;
400 pos[0] = 400; pos[1] = 0; pos[2] = pz;
404 pos[0] = 600; pos[1] = 0; pos[2] = pz;
408 pos[0] = 800; pos[1] = 0; pos[2] = pz;
414 double tbx,tby,tbz,tbr,tbp,tbphi;
415 for ( pz = 0; pz <= m_zMax; pz += 10 ) {
416 pos[0] = 50; pos[1] = 0; pos[2] = pz;
422 tbr=tbx*pos[0]/sqrt(pos[0]*pos[0]+pos[1]*pos[1])+tby*pos[1]/sqrt(pos[0]*pos[0]+pos[1]*pos[1]);
423 tbphi=tbx*pos[1]/sqrt(pos[0]*pos[0]+pos[1]*pos[1])+tby*pos[0]/sqrt(pos[0]*pos[0]+pos[1]*pos[1]);
424 tbp=tbz-tbr*pz/sqrt(pos[0]*pos[0]+pos[1]*pos[1]);
428 bphi1[j] = tbphi*10000;
430 pos[0] = 100; pos[1] = 0; pos[2] = pz;
435 tbr=tbx*pos[0]/sqrt(pos[0]*pos[0]+pos[1]*pos[1])+tby*pos[1]/sqrt(pos[0]*pos[0]+pos[1]*pos[1]);
436 tbphi=tbx*pos[1]/sqrt(pos[0]*pos[0]+pos[1]*pos[1])+tby*pos[0]/sqrt(pos[0]*pos[0]+pos[1]*pos[1]);
437 tbp=tbz-tbr*pz/sqrt(pos[0]*pos[0]+pos[1]*pos[1]);
441 bphi2[j] = tbphi*10000;
443 pos[0] = 200; pos[1] = 0; pos[2] = pz;
448 tbr=tbx*pos[0]/sqrt(pos[0]*pos[0]+pos[1]*pos[1])+tby*pos[1]/sqrt(pos[0]*pos[0]+pos[1]*pos[1]);
449 tbphi=tbx*pos[1]/sqrt(pos[0]*pos[0]+pos[1]*pos[1])+tby*pos[0]/sqrt(pos[0]*pos[0]+pos[1]*pos[1]);
450 tbp=tbz-tbr*pz/sqrt(pos[0]*pos[0]+pos[1]*pos[1]);
454 bphi3[j] = tbphi*10000;
456 pos[0] = 400; pos[1] = 0; pos[2] = pz;
461 tbr=tbx*pos[0]/sqrt(pos[0]*pos[0]+pos[1]*pos[1])+tby*pos[1]/sqrt(pos[0]*pos[0]+pos[1]*pos[1]);
462 tbphi=tbx*pos[1]/sqrt(pos[0]*pos[0]+pos[1]*pos[1])+tby*pos[0]/sqrt(pos[0]*pos[0]+pos[1]*pos[1]);
463 tbp=tbz-tbr*pz/sqrt(pos[0]*pos[0]+pos[1]*pos[1]);
467 bphi4[j] = tbphi*10000;
469 pos[0] = 600; pos[1] = 0; pos[2] = pz;
474 tbr=tbx*pos[0]/sqrt(pos[0]*pos[0]+pos[1]*pos[1])+tby*pos[1]/sqrt(pos[0]*pos[0]+pos[1]*pos[1]);
475 tbphi=tbx*pos[1]/sqrt(pos[0]*pos[0]+pos[1]*pos[1])+tby*pos[0]/sqrt(pos[0]*pos[0]+pos[1]*pos[1]);
476 tbp=tbz-tbr*pz/sqrt(pos[0]*pos[0]+pos[1]*pos[1]);
480 bphi5[j] = tbphi*10000;
482 pos[0] = 800; pos[1] = 0; pos[2] = pz;
487 tbr=tbx*pos[0]/sqrt(pos[0]*pos[0]+pos[1]*pos[1])+tby*pos[1]/sqrt(pos[0]*pos[0]+pos[1]*pos[1]);
488 tbphi=tbx*pos[1]/sqrt(pos[0]*pos[0]+pos[1]*pos[1])+tby*pos[0]/sqrt(pos[0]*pos[0]+pos[1]*pos[1]);
489 tbp=tbz-tbr*pz/sqrt(pos[0]*pos[0]+pos[1]*pos[1]);
493 bphi6[j] = tbphi*10000;
497 for(py = m_yMin; py<= m_yMax; py +=10){
498 pos[0] = 0; pos[1] = py; pos[2] = 0;
500 tbx=
B.x()/tesla*10000;
501 tby=
B.y()/tesla*10000;
502 tbz=
B.z()/tesla*10000;
507 pos[0] = 100; pos[1] = py; pos[2] = 100;
509 tbx=
B.x()/tesla*10000;
510 tby=
B.y()/tesla*10000;
511 tbz=
B.z()/tesla*10000;
515 pos[0] = 400; pos[1] = py; pos[2] = 400;
517 tbx=
B.x()/tesla*10000;
518 tby=
B.y()/tesla*10000;
519 tbz=
B.z()/tesla*10000;
525 TGraph2D *
dt =
new TGraph2D();
527 for ( pz = -3000; pz <= 3000; pz += 50 ) {
528 for( py = -2700; py <= 2700; py += 50){
529 pos[0] = 0; pos[1] = py; pos[2] = pz;
534 dt->SetPoint(k,pz/m,py/m,tbz);
538 TGraph2D *dt1 =
new TGraph2D();
540 for ( pz = -3000; pz <= 3000; pz += 50 ) {
541 for( py = -3000; py <= 3000; py += 50){
542 pos[0] = 0; pos[1] = py; pos[2] = pz;
547 double btot = sqrt(tbx*tbx+tby*tby+tbz*tbz);
548 dt1->SetPoint(k,pz/m,py/m,btot);
552 TGraph2D *dt2 =
new TGraph2D();
554 for ( pz = m_zMin; pz <= m_zMax; pz += 50 ) {
555 for( py = m_yMin; py <= m_yMax; py += 50){
556 pos[0] = 0; pos[1] = py; pos[2] = pz;
562 dt2->SetPoint(k,pz/m,py/m,tbz);
566 gStyle->SetPalette(1);
567 gStyle->SetOptTitle(1);
568 gStyle->SetOptStat(0);
570 TFile*
f1=
new TFile(
"graph.root",
"RECREATE");
571 TGraph *gr1 =
new TGraph(
n1,x,y);
572 TGraph *gr2 =
new TGraph(
n1,x,y1);
573 TGraph *gr3 =
new TGraph(
n1,x,y2);
574 TGraph *gr4 =
new TGraph(
n1,x,y3);
575 TGraph *gr5 =
new TGraph(
n1,x,y4);
577 TGraph *
g1 =
new TGraph(
n2,x1,bz1);
578 TGraph *g2 =
new TGraph(
n2,x1,bz2);
579 TGraph *g3 =
new TGraph(
n2,x1,bz3);
580 TGraph *g4 =
new TGraph(
n2,x1,bz4);
581 TGraph *g5 =
new TGraph(
n2,x1,bz5);
582 TGraph *g6 =
new TGraph(
n2,x1,bz6);
584 TGraph *g7 =
new TGraph(
n2,x1,br1);
585 TGraph *g8 =
new TGraph(
n2,x1,br2);
586 TGraph *g9 =
new TGraph(
n2,x1,br3);
587 TGraph *g10 =
new TGraph(
n2,x1,br4);
588 TGraph *g11 =
new TGraph(
n2,x1,br5);
589 TGraph *g12 =
new TGraph(
n2,x1,br6);
591 TGraph *g13 =
new TGraph(
n2,x1,bp1);
592 TGraph *g14 =
new TGraph(
n2,x1,bp2);
593 TGraph *g15 =
new TGraph(
n2,x1,bp3);
594 TGraph *g16 =
new TGraph(
n2,x1,bp4);
595 TGraph *g17 =
new TGraph(
n2,x1,bp5);
596 TGraph *g18 =
new TGraph(
n2,x1,bp6);
598 TGraph *g19 =
new TGraph(
n2,x1,bphi1);
599 TGraph *g20 =
new TGraph(
n2,x1,bphi2);
600 TGraph *g21 =
new TGraph(
n2,x1,bphi3);
601 TGraph *g22 =
new TGraph(
n2,x1,bphi4);
602 TGraph *g23 =
new TGraph(
n2,x1,bphi5);
603 TGraph *g24 =
new TGraph(
n2,x1,bphi6);
605 TGraph *g25 =
new TGraph(n3,x2,bx1);
606 TGraph *g26 =
new TGraph(n3,x2,bx2);
607 TGraph *g27 =
new TGraph(n3,x2,bx3);
609 TGraph *g28 =
new TGraph(n3,x2,by1);
610 TGraph *g29 =
new TGraph(n3,x2,by2);
611 TGraph *g30 =
new TGraph(n3,x2,by3);
613 TGraph *g31 =
new TGraph(n4,globle_x,globle_bz);
615 TCanvas *
c1 =
new TCanvas(
"c1",
"Two Graphs",200,10,600,400);
616 TCanvas *c2 =
new TCanvas(
"c2",
"Two Graphs",200,10,600,400);
620 gr1->SetLineColor(2);
621 gr1->SetLineWidth(2);
623 gr1->SetTitle(
"bz vs z (y=0,x=0,200,400,600,800mm)");
624 gr1->GetXaxis()->SetTitle(
"m");
625 gr1->GetYaxis()->SetTitle(
"tesla");
626 gr2->SetLineWidth(2);
627 gr2->SetLineColor(3);
629 gr3->SetLineWidth(2);
630 gr3->SetLineColor(4);
632 gr4->SetLineWidth(2);
633 gr4->SetLineColor(5);
635 gr5->SetLineWidth(2);
636 gr5->SetLineColor(6);
643 g1->SetTitle(
"bz(red),bendp vs z (y=0,x=50,100,200,400,600,800mm)");
644 g1->GetXaxis()->SetTitle(
"m");
645 g1->GetYaxis()->SetTitle(
"tesla");
646 g1->GetYaxis()->SetRangeUser(0.2,2);
663 g13->SetLineWidth(2);
664 g13->SetLineColor(4);
666 g14->SetLineWidth(2);
667 g14->SetLineColor(4);
669 g15->SetLineWidth(2);
670 g15->SetLineColor(4);
672 g16->SetLineWidth(2);
673 g16->SetLineColor(4);
675 g17->SetLineWidth(2);
676 g17->SetLineColor(4);
678 g18->SetLineWidth(2);
679 g18->SetLineColor(4);
686 g7->SetTitle(
"br vs z (y=0,x=50,100,200,400,600,800mm)");
687 g7->GetXaxis()->SetTitle(
"m");
688 g7->GetYaxis()->SetTitle(
"gauss");
689 g7->GetYaxis()->SetRangeUser(-1100,1000);
696 g10->SetLineWidth(2);
697 g10->SetLineColor(5);
699 g11->SetLineWidth(2);
700 g11->SetLineColor(6);
702 g12->SetLineWidth(2);
703 g12->SetLineColor(7);
707 g19->SetLineWidth(2);
708 g19->SetLineColor(2);
710 g19->SetTitle(
"bphi vs z (y=0,x=50,100,200,400,600,800mm)");
711 g19->GetXaxis()->SetTitle(
"m");
712 g19->GetYaxis()->SetTitle(
"gauss");
713 g19->GetYaxis()->SetRangeUser(-1000,200);
714 g20->SetLineWidth(2);
715 g20->SetLineColor(3);
717 g21->SetLineWidth(2);
718 g21->SetLineColor(4);
720 g22->SetLineWidth(2);
721 g22->SetLineColor(5);
723 g23->SetLineWidth(2);
724 g23->SetLineColor(6);
726 g24->SetLineWidth(2);
727 g24->SetLineColor(7);
730 TCanvas *c3 =
new TCanvas(
"c3",
"Two Graphs",200,10,600,400);
735 dt->Draw(
"z sinusoidal");
736 dt->SetTitle(
"bz vs y,z (x=0)");
738 TCanvas *c4 =
new TCanvas(
"c4",
"Two Graphs",200,10,600,400);
740 dt1->Draw(
"z sinusoidal");
741 dt1->SetTitle(
"btot vs y,z (x=0)");
743 TCanvas *c5 =
new TCanvas(
"c5",
"Two Graphs",200,10,600,400);
745 dt2->Draw(
"z sinusoidal");
746 dt2->SetTitle(
"bz vs y,z (x=0)");
749 g25->SetLineWidth(2);
750 g25->SetLineColor(2);
752 g25->SetTitle(
"bx(red),by vs y (x,z=0,100,400mm)");
753 g25->GetXaxis()->SetTitle(
"m");
754 g25->GetYaxis()->SetTitle(
"gauss");
755 g25->GetYaxis()->SetRangeUser(-200,300);
756 g26->SetLineWidth(2);
757 g26->SetLineColor(2);
759 g27->SetLineWidth(2);
760 g27->SetLineColor(2);
763 g28->SetLineWidth(2);
764 g28->SetLineColor(3);
766 g29->SetLineWidth(2);
767 g29->SetLineColor(3);
769 g30->SetLineWidth(2);
770 g30->SetLineColor(3);
774 g31->SetLineWidth(2);
775 g31->SetLineColor(2);
777 g31->SetTitle(
"bz vs x (y,z=0)");
778 g31->GetXaxis()->SetTitle(
"m");
779 g31->GetYaxis()->SetTitle(
"tesla");
807 return StatusCode::SUCCESS;
HepGeom::Point3D< double > HepPoint3D
HepGeom::Vector3D< double > HepVector3D
float elapsed(void) const
virtual BesTimer * addItem(const std::string &name)=0
virtual StatusCode fieldVector(const HepGeom::Point3D< double > &xyz, HepGeom::Vector3D< double > &fvec) const =0
virtual double getReferField()=0
virtual bool ifRealField() const =0
MagFieldReader(const std::string &name, ISvcLocator *pSvcLocator)
Standard constructor.
virtual StatusCode execute()
Algorithm execution.
virtual StatusCode finalize()
Algorithm finalization.
virtual StatusCode initialize()
Destructor.