ATLAS Offline Software
magnetSet.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "FPTracker/magnetSet.h"
7 #include "FPTracker/ConfigData.h"
8 #include "FPTracker/STLHelpers.h"
11 #include "openFile.tpl"
12 #include <istream>
13 #include <fstream>
14 #include <sstream>
15 #include <stdexcept>
16 #include <cmath>
17 #include <cassert>
18 #include <algorithm>
19 //#include <iostream>
20 namespace FPTracker
21 {
22 
23  double findIPCentre(int magver, Side side, int ip)
24  {
25  if ( magver == 1 )
26  {
27  if ( (side == beam2) && (ip == 1) ) { return 26658.88320;}
28  if ( (side == beam2) && (ip == 5) ) { return 13329.593967;}
29  if ( (side == beam1) && (ip == 1) ) { return 0.;}
30  if ( (side == beam1) && (ip == 5) ) { return 13329.289233;}
31  assert(false);
32  }
33 
34 
35 
36  if ( magver == 2 )
37  {
38  if ( (side == beam2) && (ip == 1) ) { return 13329.28923;}
39  if ( (side == beam2) && (ip == 5) ) { return 13329.59397;}
40  if ( (side == beam1) && (ip == 1) ) { return 13329.59397;}
41  if ( (side == beam1) && (ip == 5) ) { return 13329.28923;}
42  assert(false);
43  }
44 
45  if ( magver == 3 ) //ALFA, from D Pelikan
46  {
47  if ( (side == beam2) && (ip == 1) ) { return 0.;}
48  if ( (side == beam2) && (ip == 5) ) { return 13329.593967;}
49  if ( (side == beam1) && (ip == 1) ) { return 0.;}
50  if ( (side == beam1) && (ip == 5) ) { return 13329.289233;}
51  assert(false);
52  }
53  // check that the argument values are in have expected values
54  assert(false);
55 
56  return 0;
57  }
58 
59  // Magnet::Container_t magnetSet(const ConfigData& cData, int sideSelect, const std::vector< std::istream& >& magfiles){
60  Magnet::Container_t magnetSet(const ConfigData& cData, const Side& side, int magver, std::shared_ptr< std::ifstream> magfile)
61  {
62  // Coordinate system: take z = forwards, x= to left , y = up.
63  // For quadrupoles, input the quantity K1L. type = 1,2 if K1L is +,-ve.
64  // Input type = 3 here to let the program decide.
65  // HKICK and VKICK are read and treated as bending magnets.
66 
67 
68 
69  //-------- Arrange to read the magnet data from CERN files.
70  // twiss_b1.txt and twiss_b2.txt for beam 1 and beam 2
71  // beam 2 is on side=0 here and beam 1 is on side=1
72  // Read through the files to pick up the requested magnets.
73 
74 
75  const int& IP = cData.IP;
76  const double& apermb = cData.apermb;
77  const double& brho = cData.brho;
78  const float& absZMagMax = cData.absZMagMax;
79 
80  Magnet::Container_t magnets;
81 
82  double centreIP = findIPCentre(magver, side, IP);
83 
84  std::ifstream& myMags = *magfile;
85 
86 
87  //std::cout<<"Open magnets file for beam "<<ifile<<std::endl;
88  //--- Skip introductory material, indicated by the 1st character of a record.
89  while (myMags.peek() =='@' || myMags.peek() =='*' || myMags.peek() =='$')
90  {
91  myMags.ignore(1000,'\n');
92  }
93 
94  // side 1 == direction to +ve s of IP == beam 1.
95  // NB "dist" is -Delta(s) but magcen, i.e. z, is then -dist.
96 
97  while(true)
98  {
99 
100  double endpos=0, maglength=0, K0L=0, K1L=0, K2L=0, K3L=0, HKICK=0, VKICK=0;
101  float BETX=0,ALFX=0,MUX=0,DX=0,DPX=0,X=0,PX=0,BETY=0,ALFY=0,MUY=0,DY=0,DPY=0,Y=0,PY=0,A1=0,A2=0,A3=0,A4=0;
102 
103  std::string magname,magsort, basicmag ;
104 
105  std::string aperture;
106 
107  if( magver==1 )
108  {
109  myMags>>magname>>magsort>>endpos>>maglength>>K0L>>K1L>>K2L>>K3L>>HKICK>>VKICK
110  >>BETX>>ALFX>>MUX>>DX>>DPX>>X>>PX>>BETY>>ALFY>>MUY>>DY>>DPY>>Y>>PY
111  >>aperture>>A1>>A2>>A3>>A4;
112  }
113  else if( magver==2 or magver==3 )
114  {
115  myMags>>magname>>magsort>>basicmag>>endpos>>maglength>>HKICK>>VKICK>>K0L
116  >>K1L>>K2L>>K3L>>X>>Y>>PX>>PY>>BETX>>BETY>>ALFX>>ALFY
117  >>MUX>>MUY>>DX>>DY>>DPX>>DPY>>aperture>>A1>>A2>>A3>>A4;
118  }
119  else
120  {
121  assert(false);
122  }
123 
124  if(magver==1)
125  { // Some more fudges to give more precision
126  if(std::fabs(K0L)==0.000188) K0L=K0L*188.175/188.0 ;
127  if(std::fabs(K0L)==0.001129) K0L=K0L*1129.049/1129. ;
128  if(std::fabs(K1L)==0.055577) K0L=K0L*55576.561/55577. ;
129  if(std::fabs(K1L)==0.047986) K0L=K0L*47986.041/47986. ;
130  if(std::fabs(VKICK)==0.00020) VKICK=VKICK*20.171/20. ;
131  if(std::fabs(VKICK)==0.00019) VKICK=VKICK*19.17/19. ;
132  }
133 
134  double dist = centreIP - endpos + 0.5*maglength ;
135 
136  float adist=std::fabs(dist) ;
137 
138  // decide how to progress down the magnet files
139  // the file formats are not constant. first
140  // consider the magver=1 or 2 (these are two formats used by AFP.
141  if ( magver == 1 or magver == 2 )
142  {
143  if (side == beam1)
144  {
145  if( dist > 0. ) {continue;}
146  if( adist > absZMagMax ) {break;}
147  }
148  else
149  {
150  if( adist > absZMagMax ) {continue;}
151  if ( dist <= 0. ) {break;}
152  }
153  }
154  else if( magver == 3 ) //ALFA. from D. Pelikan
155  {
156  if( dist > 0. ) {continue;}
157  if( adist > absZMagMax ) {break;}
158  }
159 
160  Magnet::Type mmagtype = Magnet::notAMagnet;
161  double magstrength = 0;
162  if (K0L != 0.)
163  {
164  mmagtype = Magnet::hbDipole; //type = 0
165  magstrength = K0L;
166  if(side == beam2)
167  {
168  magstrength = -magstrength;
169  }
170 
171  }
172  else if(K1L != 0.)
173  {
174  magstrength = K1L;
175  mmagtype = magstrength >= 0 ? Magnet::hfQuadrupole:Magnet::vfQuadrupole; // type = 3 ->1,2
176  assert(maglength > 0.);
177  magstrength = std::fabs(brho*magstrength/maglength);
178  }
179  else if(HKICK != 0.)
180  {
181  mmagtype = Magnet::hbDipole; // type = 0
182  magstrength = -HKICK;
183  }
184  else if(VKICK != 0.)
185  {
186  mmagtype = Magnet::vbDipole; // type = 4
187  magstrength = VKICK;
188  }
189 
190  if(mmagtype == Magnet::notAMagnet) {continue;} // --(not a magnet)
191 
192 
193  double mmagcen = -dist;
194 
195 
196  // more differences between AFP and ALFA files (from D. Pelikan)
197  if (magver == 3)
198  {
199  if (side == beam2 ){ mmagcen = dist; }
200 
201  }
202 
203 
204  int mmagapertype = 0;
205  // CLassify apertures.
206  if(aperture == "\"NONE\"")
207  {
208  mmagapertype = 0;
209  }
210  else if(aperture == "\"CIRCLE\"" && A1 == 0.)
211  {
212  mmagapertype =0;
213  }
214  else if(aperture == "\"CIRCLE\"" && A1 > 0.)
215  {
216  mmagapertype =1;
217  }
218  else if(aperture == "\"RECTELLIPSE\"")
219  {
220  mmagapertype =2;
221  }
222  else if(aperture == "\"OCTAGON\"")
223  {
224  mmagapertype =3;
225  }
226  else
227  {
228  std::stringstream sstr;
229  sstr<<" Unknown magnet aperture type "<<aperture<<'\n' ;
230  throw std::runtime_error(sstr.str().c_str());
231  }
232 
233  /*
234  std::cout<<magname<<" "<<endpos<<" "<<maglength<<" "<<K0L<<" "<<K1L<<" "
235  <<" --> "<<mmagcen<<" "<<maglength
236  <<" "<<magstrength
237  <<" A1 "<<A1
238  <<" A2 "<<A2
239  <<" A3 "<<A3
240  <<" A4 "<<A4
241  <<std::endl;
242  */
243 
244 
245  if(apermb > 0)
246  { // -------alter the MB apertures
247  int pos = magname.find("MB.");
248  if(pos==1)
249  {
250  A3=apermb; A4=apermb;
251  }
252  }
253 
254  // note the sign convention for xdis. This shift of the magnet
255  // x location accounts for the splitting of the beams somewhere
256  // around 155. m. The x coordinate is wrt to beamline position,
257  // and not the magnet center.
258  double mmagnet_x = beamlineXPosition(mmagcen)[0];
259  double mmagnet_y = beamlineXPosition(mmagcen)[1];
260 
261  Magnet::Ptr_t mptr = magnetFactory(mmagnet_x,
262  mmagnet_y,
263  mmagcen,
264  magstrength,
265  maglength,
266  mmagapertype,
267  A1,
268  A2,
269  A3,
270  A4,
271  X,
272  cData.pbeam0,
273  side,
274  mmagtype);
275  // std::cout<<*mptr<<std::endl;
276  magnets.push_back(mptr);
277  }
278  std::sort(magnets.begin(), magnets.end(), absZGreater< Magnet::ConstPtr_t >);
279  return magnets;
280  }
281 }
282 
FPTracker::findIPCentre
double findIPCentre(int magver, Side side, int ip)
Definition: magnetSet.cxx:23
FPTracker::Magnet::vbDipole
@ vbDipole
Definition: FPTracker/FPTracker/Magnet.h:64
ConfigData.h
FPTracker::magnetSet
Magnet::Container_t magnetSet(const ConfigData &, const Side &side, int magversion, std::shared_ptr< std::ifstream > magfile)
Definition: magnetSet.cxx:60
FPTracker::Magnet::hfQuadrupole
@ hfQuadrupole
Definition: FPTracker/FPTracker/Magnet.h:64
TransversePoint.h
STLHelpers.h
FPTracker::Magnet::hbDipole
@ hbDipole
Definition: FPTracker/FPTracker/Magnet.h:64
Monitored::X
@ X
Definition: HistogramFillerUtils.h:24
magnetSet.h
TRT::Hit::side
@ side
Definition: HitInfo.h:83
FPTracker::beam1
@ beam1
Definition: FPTrackerConstants.h:12
FPTracker::ConfigData::absZMagMax
float absZMagMax
Definition: FPTracker/FPTracker/ConfigData.h:23
FPTracker::beam2
@ beam2
Definition: FPTrackerConstants.h:12
magnetFactory.h
FPTracker::beamlineXPosition
TransversePoint beamlineXPosition(double z)
Definition: beamlineXPosition.cxx:11
FPTracker::Magnet::Container_t
std::vector< Ptr_t > Container_t
Definition: FPTracker/FPTracker/Magnet.h:62
Side
Definition: WaferTree.h:36
FPTracker::Magnet::notAMagnet
@ notAMagnet
Definition: FPTracker/FPTracker/Magnet.h:64
find_tgc_unfilled_channelids.ip
ip
Definition: find_tgc_unfilled_channelids.py:3
Monitored::Y
@ Y
Definition: HistogramFillerUtils.h:24
FPTracker::ConfigData::pbeam0
double pbeam0
Definition: FPTracker/FPTracker/ConfigData.h:18
FPTracker::ConfigData::brho
double brho
Definition: FPTracker/FPTracker/ConfigData.h:19
FPTracker::Magnet::vfQuadrupole
@ vfQuadrupole
Definition: FPTracker/FPTracker/Magnet.h:64
FPTracker::magnetFactory
Magnet::Ptr_t magnetFactory(double x, double y, double center, double strength, double length, int apertype, double A1, double A2, double A3, double A4, double X, double pbeam0, Side side, Magnet::Type type)
Definition: magnetFactory.cxx:87
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
FPTracker::Magnet::Ptr_t
std::shared_ptr< Magnet > Ptr_t
Definition: FPTracker/FPTracker/Magnet.h:61
beamlineXPosition.h
FPTracker::ConfigData
Definition: FPTracker/FPTracker/ConfigData.h:9
FPTracker
Definition: FPTracker/FPTracker/Beamline.h:12
FPTracker::ConfigData::apermb
double apermb
Definition: FPTracker/FPTracker/ConfigData.h:15
FPTracker::ConfigData::IP
int IP
Definition: FPTracker/FPTracker/ConfigData.h:12
FPTracker::Magnet::Type
Type
Definition: FPTracker/FPTracker/Magnet.h:64