1#include "GaudiKernel/INTuple.h"
2#include "GaudiKernel/INTupleSvc.h"
3#include "GaudiKernel/MsgStream.h"
4#include "GaudiKernel/NTuple.h"
5#include "GaudiKernel/SmartDataPtr.h"
7#include "CLHEP/Geometry/Point3D.h"
8#include "CLHEP/Geometry/Vector3D.h"
9#include "CLHEP/Units/PhysicalConstants.h"
11#include "CLHEP/Random/RandFlat.h"
13#include "MagneticFieldSvc/IBesMagFieldSvc.h"
25#ifndef ENABLE_BACKWARDS_COMPATIBILITY
29#ifndef ENABLE_BACKWARDS_COMPATIBILITY
45 : Algorithm( name, pSvcLocator ), m_pIMF( 0 ) {
46 declareProperty(
"Zmin", m_zMin = -1200.0 * mm );
47 declareProperty(
"Zmax", m_zMax = 1200.0 * mm );
48 declareProperty(
"Step", m_step = 50.0 * mm );
49 declareProperty(
"Xmin", m_xMin = -900.0 * mm );
50 declareProperty(
"Xmax", m_xMax = 900.0 * mm );
51 declareProperty(
"Ymin", m_yMin = -900.0 * mm );
52 declareProperty(
"Ymax", m_yMax = 900.0 * mm );
53 declareProperty(
"filename", m_filename =
"rawmode3_out.dat" );
61 MsgStream msg(
msgSvc(), name() );
63 msg << MSG::INFO <<
"FieldReader intialize() has been called" << endmsg;
64 StatusCode status = service(
"MagneticFieldSvc", m_pIMF,
true );
65 if ( !status.isSuccess() )
67 msg << MSG::FATAL <<
"Unable to open Magnetic field service" << endmsg;
68 return StatusCode::FAILURE;
71 msg << MSG::DEBUG <<
" Book ntuple" << endmsg;
74 if ( nt1 ) m_tuple1 = nt1;
77 m_tuple1 =
ntupleSvc()->book(
"FILE1/n1", CLID_ColumnWiseTuple,
"Field" );
80 status = m_tuple1->addItem(
"x", m_x );
81 status = m_tuple1->addItem(
"y", m_y );
82 status = m_tuple1->addItem(
"z", m_z );
83 status = m_tuple1->addItem(
"r", m_r );
84 status = m_tuple1->addItem(
"Bx", m_Bx );
85 status = m_tuple1->addItem(
"By", m_By );
86 status = m_tuple1->addItem(
"Bz", m_Bz );
87 status = m_tuple1->addItem(
"SigmaBx", m_sigma_bx );
88 status = m_tuple1->addItem(
"SigmaBy", m_sigma_by );
89 status = m_tuple1->addItem(
"SigmaBz", m_sigma_bz );
93 msg << MSG::ERROR <<
" Cannot book N-tuple:" << long( m_tuple1 ) << endmsg;
94 return StatusCode::FAILURE;
99 if ( nt2 ) m_tuple2 = nt2;
102 m_tuple2 =
ntupleSvc()->book(
"FILE1/n2", CLID_ColumnWiseTuple,
"Field" );
105 status = m_tuple2->addItem(
"x", m_x2 );
106 status = m_tuple2->addItem(
"y", m_y2 );
107 status = m_tuple2->addItem(
"z", m_z2 );
108 status = m_tuple2->addItem(
"r", m_r2 );
109 status = m_tuple2->addItem(
"Bx", m_Bx2 );
110 status = m_tuple2->addItem(
"By", m_By2 );
111 status = m_tuple2->addItem(
"Bz", m_Bz2 );
115 msg << MSG::ERROR <<
" Cannot book N-tuple:" << long( m_tuple2 ) << endmsg;
116 return StatusCode::FAILURE;
120 NTuplePtr nt3(
ntupleSvc(),
"FILE1/n3" );
121 if ( nt3 ) m_tuple3 = nt3;
124 m_tuple3 =
ntupleSvc()->book(
"FILE1/n3", CLID_ColumnWiseTuple,
"Field" );
127 status = m_tuple3->addItem(
"x", m_x3 );
128 status = m_tuple3->addItem(
"y", m_y3 );
129 status = m_tuple3->addItem(
"z", m_z3 );
130 status = m_tuple3->addItem(
"r", m_r3 );
131 status = m_tuple3->addItem(
"phi", m_phi3 );
132 status = m_tuple3->addItem(
"Bx", m_Bx3 );
133 status = m_tuple3->addItem(
"By", m_By3 );
134 status = m_tuple3->addItem(
"Bz", m_Bz3 );
138 msg << MSG::ERROR <<
" Cannot book N-tuple:" << long( m_tuple3 ) << endmsg;
139 return StatusCode::FAILURE;
143 NTuplePtr nt4(
ntupleSvc(),
"FILE1/n4" );
144 if ( nt4 ) m_tuple4 = nt4;
147 m_tuple4 =
ntupleSvc()->book(
"FILE1/n4", CLID_ColumnWiseTuple,
"Field" );
148 if ( m_tuple4 ) { status = m_tuple4->addItem(
"time", m_time ); }
151 msg << MSG::ERROR <<
" Cannot book N-tuple:" << long( m_tuple4 ) << endmsg;
152 return StatusCode::FAILURE;
156 status = service(
"BesTimerSvc", m_timersvc );
157 if ( status.isFailure() )
159 msg << MSG::ERROR << name() <<
": Unable to locate BesTimer Service" << endmsg;
160 return StatusCode::FAILURE;
162 m_timer = m_timersvc->addItem(
"Read field Time" );
164 msg << MSG::INFO <<
"MagFieldReader initialized" << endmsg;
165 return StatusCode::SUCCESS;
173 MsgStream msg(
msgSvc(), name() );
175 msg << MSG::DEBUG <<
"==> Execute MagFieldReader" << endmsg;
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,
285 max_z = 2.1 * m, min_z = -2.1 * m;
286 px = min_x + ( max_x - min_x ) * RandFlat::shoot();
287 py = min_y + ( max_y - min_y ) * RandFlat::shoot();
288 pz = min_z + ( max_z - min_z ) * RandFlat::shoot();
289 HepPoint3D r( px, py, pz );
291 m_pIMF->fieldVector( r, b );
297 m_time = m_timer->elapsed();
307 msg << MSG::INFO <<
"MagFieldReader executed" << endmsg;
308 return StatusCode::SUCCESS;
316 MsgStream msg(
msgSvc(), name() );
317 msg << MSG::DEBUG <<
"==> Finalize MagFieldReader" << endmsg;
319 return StatusCode::SUCCESS;
327StatusCode MagFieldReader::readField() {
329 MsgStream msg(
msgSvc(), name() );
330 msg << MSG::DEBUG <<
"m_pIMF = " << m_pIMF << endmsg;
333 msg << MSG::DEBUG <<
"The reference field is " << referfield <<
" tesla" << endmsg;
335 if ( ifrealfield ) msg << MSG::DEBUG <<
"Use the real field" << endmsg;
336 else msg << MSG::DEBUG <<
"Use the fake field" << endmsg;
369 double x1[
n2], bz1[
n2], bz2[
n2], bz3[
n2], bz4[
n2], bz5[
n2], bz6[
n2];
370 double br1[
n2], br2[
n2], br3[
n2], br4[
n2], br5[
n2], br6[
n2];
371 double bp1[
n2], bp2[
n2], bp3[
n2], bp4[
n2], bp5[
n2], bp6[
n2];
372 double bphi1[
n2], bphi2[
n2], bphi3[
n2], bphi4[
n2], bphi5[
n2], bphi6[
n2];
375 double x2[n3], bx1[n3], bx2[n3], bx3[n3], bx4[n3], bx5[n3], bx6[n3];
376 double by1[n3], by2[n3], by3[n3], by4[n3], by5[n3], by6[n3];
379 double globle_x[n4], globle_bz[n4];
381 double px = 0.0, py = 0.0, pz = 0.0;
383 for ( px = -2600; px <= 2600; px += 10 )
389 globle_x[i] = px / m;
390 globle_bz[i] = B.z() / tesla;
394 for ( pz = m_zMin; pz <= m_zMax; pz += 10 )
399 m_pIMF->fieldVector( pos, B );
401 y[i] =
B.z() / tesla;
406 m_pIMF->fieldVector( pos, B );
407 y1[i] =
B.z() / tesla;
412 m_pIMF->fieldVector( pos, B );
413 y2[i] =
B.z() / tesla;
418 m_pIMF->fieldVector( pos, B );
419 y3[i] =
B.z() / tesla;
424 m_pIMF->fieldVector( pos, B );
425 y4[i] =
B.z() / tesla;
429 double tbx, tby, tbz, tbr, tbp, tbphi;
430 for ( pz = 0; pz <= m_zMax; pz += 10 )
435 m_pIMF->fieldVector( pos, B );
440 tbr = tbx * pos[0] / sqrt( pos[0] * pos[0] + pos[1] * pos[1] ) +
441 tby * pos[1] / sqrt( pos[0] * pos[0] + pos[1] * pos[1] );
442 tbphi = tbx * pos[1] / sqrt( pos[0] * pos[0] + pos[1] * pos[1] ) +
443 tby * pos[0] / sqrt( pos[0] * pos[0] + pos[1] * pos[1] );
444 tbp = tbz - tbr * pz / sqrt( pos[0] * pos[0] + pos[1] * pos[1] );
446 br1[j] = tbr * 10000;
448 bphi1[j] = tbphi * 10000;
453 m_pIMF->fieldVector( pos, B );
457 tbr = tbx * pos[0] / sqrt( pos[0] * pos[0] + pos[1] * pos[1] ) +
458 tby * pos[1] / sqrt( pos[0] * pos[0] + pos[1] * pos[1] );
459 tbphi = tbx * pos[1] / sqrt( pos[0] * pos[0] + pos[1] * pos[1] ) +
460 tby * pos[0] / sqrt( pos[0] * pos[0] + pos[1] * pos[1] );
461 tbp = tbz - tbr * pz / sqrt( pos[0] * pos[0] + pos[1] * pos[1] );
463 br2[j] = tbr * 10000;
465 bphi2[j] = tbphi * 10000;
470 m_pIMF->fieldVector( pos, B );
474 tbr = tbx * pos[0] / sqrt( pos[0] * pos[0] + pos[1] * pos[1] ) +
475 tby * pos[1] / sqrt( pos[0] * pos[0] + pos[1] * pos[1] );
476 tbphi = tbx * pos[1] / sqrt( pos[0] * pos[0] + pos[1] * pos[1] ) +
477 tby * pos[0] / sqrt( pos[0] * pos[0] + pos[1] * pos[1] );
478 tbp = tbz - tbr * pz / sqrt( pos[0] * pos[0] + pos[1] * pos[1] );
480 br3[j] = tbr * 10000;
482 bphi3[j] = tbphi * 10000;
487 m_pIMF->fieldVector( pos, B );
491 tbr = tbx * pos[0] / sqrt( pos[0] * pos[0] + pos[1] * pos[1] ) +
492 tby * pos[1] / sqrt( pos[0] * pos[0] + pos[1] * pos[1] );
493 tbphi = tbx * pos[1] / sqrt( pos[0] * pos[0] + pos[1] * pos[1] ) +
494 tby * pos[0] / sqrt( pos[0] * pos[0] + pos[1] * pos[1] );
495 tbp = tbz - tbr * pz / sqrt( pos[0] * pos[0] + pos[1] * pos[1] );
497 br4[j] = tbr * 10000;
499 bphi4[j] = tbphi * 10000;
504 m_pIMF->fieldVector( pos, B );
508 tbr = tbx * pos[0] / sqrt( pos[0] * pos[0] + pos[1] * pos[1] ) +
509 tby * pos[1] / sqrt( pos[0] * pos[0] + pos[1] * pos[1] );
510 tbphi = tbx * pos[1] / sqrt( pos[0] * pos[0] + pos[1] * pos[1] ) +
511 tby * pos[0] / sqrt( pos[0] * pos[0] + pos[1] * pos[1] );
512 tbp = tbz - tbr * pz / sqrt( pos[0] * pos[0] + pos[1] * pos[1] );
514 br5[j] = tbr * 10000;
516 bphi5[j] = tbphi * 10000;
521 m_pIMF->fieldVector( pos, B );
525 tbr = tbx * pos[0] / sqrt( pos[0] * pos[0] + pos[1] * pos[1] ) +
526 tby * pos[1] / sqrt( pos[0] * pos[0] + pos[1] * pos[1] );
527 tbphi = tbx * pos[1] / sqrt( pos[0] * pos[0] + pos[1] * pos[1] ) +
528 tby * pos[0] / sqrt( pos[0] * pos[0] + pos[1] * pos[1] );
529 tbp = tbz - tbr * pz / sqrt( pos[0] * pos[0] + pos[1] * pos[1] );
531 br6[j] = tbr * 10000;
533 bphi6[j] = tbphi * 10000;
537 for ( py = m_yMin; py <= m_yMax; py += 10 )
542 m_pIMF->fieldVector( pos, B );
543 tbx =
B.x() / tesla * 10000;
544 tby =
B.y() / tesla * 10000;
545 tbz =
B.z() / tesla * 10000;
553 m_pIMF->fieldVector( pos, B );
554 tbx =
B.x() / tesla * 10000;
555 tby =
B.y() / tesla * 10000;
556 tbz =
B.z() / tesla * 10000;
563 m_pIMF->fieldVector( pos, B );
564 tbx =
B.x() / tesla * 10000;
565 tby =
B.y() / tesla * 10000;
566 tbz =
B.z() / tesla * 10000;
572 TGraph2D*
dt =
new TGraph2D();
574 for ( pz = -3000; pz <= 3000; pz += 50 )
576 for ( py = -2700; py <= 2700; py += 50 )
581 m_pIMF->fieldVector( pos, B );
585 dt->SetPoint( k, pz / m, py / m, tbz );
589 TGraph2D* dt1 =
new TGraph2D();
591 for ( pz = -3000; pz <= 3000; pz += 50 )
593 for ( py = -3000; py <= 3000; py += 50 )
598 m_pIMF->fieldVector( pos, B );
602 double btot = sqrt( tbx * tbx + tby * tby + tbz * tbz );
603 dt1->SetPoint( k, pz / m, py / m, btot );
607 TGraph2D* dt2 =
new TGraph2D();
609 for ( pz = m_zMin; pz <= m_zMax; pz += 50 )
611 for ( py = m_yMin; py <= m_yMax; py += 50 )
616 m_pIMF->fieldVector( pos, B );
621 dt2->SetPoint( k, pz / m, py / m, tbz );
625 gStyle->SetPalette( 1 );
626 gStyle->SetOptTitle( 1 );
627 gStyle->SetOptStat( 0 );
629 TFile*
f1 =
new TFile(
"graph.root",
"RECREATE" );
630 TGraph* gr1 =
new TGraph(
n1,
x, y );
631 TGraph* gr2 =
new TGraph(
n1,
x, y1 );
632 TGraph* gr3 =
new TGraph(
n1,
x, y2 );
633 TGraph* gr4 =
new TGraph(
n1,
x, y3 );
634 TGraph* gr5 =
new TGraph(
n1,
x, y4 );
636 TGraph*
g1 =
new TGraph(
n2, x1, bz1 );
637 TGraph* g2 =
new TGraph(
n2, x1, bz2 );
638 TGraph* g3 =
new TGraph(
n2, x1, bz3 );
639 TGraph* g4 =
new TGraph(
n2, x1, bz4 );
640 TGraph* g5 =
new TGraph(
n2, x1, bz5 );
641 TGraph* g6 =
new TGraph(
n2, x1, bz6 );
643 TGraph* g7 =
new TGraph(
n2, x1, br1 );
644 TGraph* g8 =
new TGraph(
n2, x1, br2 );
645 TGraph* g9 =
new TGraph(
n2, x1, br3 );
646 TGraph* g10 =
new TGraph(
n2, x1, br4 );
647 TGraph* g11 =
new TGraph(
n2, x1, br5 );
648 TGraph* g12 =
new TGraph(
n2, x1, br6 );
650 TGraph* g13 =
new TGraph(
n2, x1, bp1 );
651 TGraph* g14 =
new TGraph(
n2, x1, bp2 );
652 TGraph* g15 =
new TGraph(
n2, x1, bp3 );
653 TGraph* g16 =
new TGraph(
n2, x1, bp4 );
654 TGraph* g17 =
new TGraph(
n2, x1, bp5 );
655 TGraph* g18 =
new TGraph(
n2, x1, bp6 );
657 TGraph* g19 =
new TGraph(
n2, x1, bphi1 );
658 TGraph* g20 =
new TGraph(
n2, x1, bphi2 );
659 TGraph* g21 =
new TGraph(
n2, x1, bphi3 );
660 TGraph* g22 =
new TGraph(
n2, x1, bphi4 );
661 TGraph* g23 =
new TGraph(
n2, x1, bphi5 );
662 TGraph* g24 =
new TGraph(
n2, x1, bphi6 );
664 TGraph* g25 =
new TGraph( n3, x2, bx1 );
665 TGraph* g26 =
new TGraph( n3, x2, bx2 );
666 TGraph* g27 =
new TGraph( n3, x2, bx3 );
668 TGraph* g28 =
new TGraph( n3, x2, by1 );
669 TGraph* g29 =
new TGraph( n3, x2, by2 );
670 TGraph* g30 =
new TGraph( n3, x2, by3 );
672 TGraph* g31 =
new TGraph( n4, globle_x, globle_bz );
674 TCanvas* c1 =
new TCanvas(
"c1",
"Two Graphs", 200, 10, 600, 400 );
675 TCanvas* c2 =
new TCanvas(
"c2",
"Two Graphs", 200, 10, 600, 400 );
679 gr1->SetLineColor( 2 );
680 gr1->SetLineWidth( 2 );
682 gr1->SetTitle(
"bz vs z (y=0,x=0,200,400,600,800mm)" );
683 gr1->GetXaxis()->SetTitle(
"m" );
684 gr1->GetYaxis()->SetTitle(
"tesla" );
685 gr2->SetLineWidth( 2 );
686 gr2->SetLineColor( 3 );
688 gr3->SetLineWidth( 2 );
689 gr3->SetLineColor( 4 );
691 gr4->SetLineWidth( 2 );
692 gr4->SetLineColor( 5 );
694 gr5->SetLineWidth( 2 );
695 gr5->SetLineColor( 6 );
699 g1->SetLineColor( 2 );
700 g1->SetLineWidth( 2 );
702 g1->SetTitle(
"bz(red),bendp vs z (y=0,x=50,100,200,400,600,800mm)" );
703 g1->GetXaxis()->SetTitle(
"m" );
704 g1->GetYaxis()->SetTitle(
"tesla" );
705 g1->GetYaxis()->SetRangeUser( 0.2, 2 );
706 g2->SetLineWidth( 2 );
707 g2->SetLineColor( 2 );
709 g3->SetLineWidth( 2 );
710 g3->SetLineColor( 2 );
712 g4->SetLineWidth( 2 );
713 g4->SetLineColor( 2 );
715 g5->SetLineWidth( 2 );
716 g5->SetLineColor( 2 );
718 g6->SetLineColor( 2 );
719 g6->SetLineWidth( 2 );
722 g13->SetLineWidth( 2 );
723 g13->SetLineColor( 4 );
725 g14->SetLineWidth( 2 );
726 g14->SetLineColor( 4 );
728 g15->SetLineWidth( 2 );
729 g15->SetLineColor( 4 );
731 g16->SetLineWidth( 2 );
732 g16->SetLineColor( 4 );
734 g17->SetLineWidth( 2 );
735 g17->SetLineColor( 4 );
737 g18->SetLineWidth( 2 );
738 g18->SetLineColor( 4 );
742 g7->SetLineWidth( 2 );
743 g7->SetLineColor( 2 );
745 g7->SetTitle(
"br vs z (y=0,x=50,100,200,400,600,800mm)" );
746 g7->GetXaxis()->SetTitle(
"m" );
747 g7->GetYaxis()->SetTitle(
"gauss" );
748 g7->GetYaxis()->SetRangeUser( -1100, 1000 );
749 g8->SetLineWidth( 2 );
750 g8->SetLineColor( 3 );
752 g9->SetLineWidth( 2 );
753 g9->SetLineColor( 4 );
755 g10->SetLineWidth( 2 );
756 g10->SetLineColor( 5 );
758 g11->SetLineWidth( 2 );
759 g11->SetLineColor( 6 );
761 g12->SetLineWidth( 2 );
762 g12->SetLineColor( 7 );
766 g19->SetLineWidth( 2 );
767 g19->SetLineColor( 2 );
769 g19->SetTitle(
"bphi vs z (y=0,x=50,100,200,400,600,800mm)" );
770 g19->GetXaxis()->SetTitle(
"m" );
771 g19->GetYaxis()->SetTitle(
"gauss" );
772 g19->GetYaxis()->SetRangeUser( -1000, 200 );
773 g20->SetLineWidth( 2 );
774 g20->SetLineColor( 3 );
776 g21->SetLineWidth( 2 );
777 g21->SetLineColor( 4 );
779 g22->SetLineWidth( 2 );
780 g22->SetLineColor( 5 );
782 g23->SetLineWidth( 2 );
783 g23->SetLineColor( 6 );
785 g24->SetLineWidth( 2 );
786 g24->SetLineColor( 7 );
789 TCanvas* c3 =
new TCanvas(
"c3",
"Two Graphs", 200, 10, 600, 400 );
794 dt->Draw(
"z sinusoidal" );
795 dt->SetTitle(
"bz vs y,z (x=0)" );
797 TCanvas* c4 =
new TCanvas(
"c4",
"Two Graphs", 200, 10, 600, 400 );
799 dt1->Draw(
"z sinusoidal" );
800 dt1->SetTitle(
"btot vs y,z (x=0)" );
802 TCanvas* c5 =
new TCanvas(
"c5",
"Two Graphs", 200, 10, 600, 400 );
804 dt2->Draw(
"z sinusoidal" );
805 dt2->SetTitle(
"bz vs y,z (x=0)" );
808 g25->SetLineWidth( 2 );
809 g25->SetLineColor( 2 );
811 g25->SetTitle(
"bx(red),by vs y (x,z=0,100,400mm)" );
812 g25->GetXaxis()->SetTitle(
"m" );
813 g25->GetYaxis()->SetTitle(
"gauss" );
814 g25->GetYaxis()->SetRangeUser( -200, 300 );
815 g26->SetLineWidth( 2 );
816 g26->SetLineColor( 2 );
818 g27->SetLineWidth( 2 );
819 g27->SetLineColor( 2 );
822 g28->SetLineWidth( 2 );
823 g28->SetLineColor( 3 );
825 g29->SetLineWidth( 2 );
826 g29->SetLineColor( 3 );
828 g30->SetLineWidth( 2 );
829 g30->SetLineColor( 3 );
833 g31->SetLineWidth( 2 );
834 g31->SetLineColor( 2 );
836 g31->SetTitle(
"bz vs x (y,z=0)" );
837 g31->GetXaxis()->SetTitle(
"m" );
838 g31->GetYaxis()->SetTitle(
"tesla" );
866 return StatusCode::SUCCESS;
HepGeom::Vector3D< double > HepVector3D
HepGeom::Point3D< double > HepPoint3D
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.