Collimator.cpp 16.8 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// ------------------------------------------------------------------------
// $RCSfile: Collimator.cpp,v $
// ------------------------------------------------------------------------
// $Revision: 1.1.1.1 $
// ------------------------------------------------------------------------
// Copyright: see Copyright.readme
// ------------------------------------------------------------------------
//
// Class: Collimator
//   Defines the abstract interface for a beam Collimator.
//
// ------------------------------------------------------------------------
// Class category: AbsBeamline
// ------------------------------------------------------------------------
//
// $Date: 2000/03/27 09:32:31 $
// $Author: fci $
//
// ------------------------------------------------------------------------

#include "AbsBeamline/Collimator.h"
kraus's avatar
kraus committed
22
#include "Physics/Physics.h"
23
#include "Algorithms/PartBunch.h"
gsell's avatar
gsell committed
24 25 26
#include "AbsBeamline/BeamlineVisitor.h"
#include "Fields/Fieldmap.hh"
#include "Structure/LossDataSink.h"
kraus's avatar
kraus committed
27
#include "Solvers/SurfacePhysicsHandler.hh"
28
#include <memory>
gsell's avatar
gsell committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

extern Inform *gmsg;

using namespace std;

// Class Collimator
// ------------------------------------------------------------------------

Collimator::Collimator():
    Component(),
    filename_m(""),
    plane_m(OFF),
    position_m(0.0),
    PosX_m(0),
    PosY_m(0),
    PosZ_m(0),
    MomentumX_m(0),
    MomentumY_m(0),
    MomentumZ_m(0),
    time_m(0),
    id_m(0),
    informed_m(false),
    a_m(0.0),
    b_m(0.0),
    x0_m(0.0),
    y0_m(0.0),
55 56 57 58 59
    xstart_m(0.0),
    xend_m(0.0),
    ystart_m(0.0),
    yend_m(0.0),
    width_m(0.0),
gsell's avatar
gsell committed
60 61 62 63 64 65 66 67
    isAPepperPot_m(false),
    isASlit_m(false),
    isARColl_m(false),
    isACColl_m(false),
    isAWire_m(false),
    rHole_m(0.0),
    nHolesX_m(0),
    nHolesY_m(0),
68
    pitch_m(0.0),
adelmann's avatar
adelmann committed
69 70
    sphys_m(NULL),
    lossDs_m(nullptr)
gsell's avatar
gsell committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
{}


Collimator::Collimator(const Collimator &right):
    Component(right),
    filename_m(right.filename_m),
    plane_m(right.plane_m),
    position_m(right.position_m),
    PosX_m(right.PosX_m),
    PosY_m(right.PosY_m),
    PosZ_m(right.PosZ_m),
    MomentumX_m(right.MomentumX_m),
    MomentumY_m(right.MomentumY_m),
    MomentumZ_m(right.MomentumZ_m),
    time_m(right.time_m),
    id_m(right.id_m),
    informed_m(right.informed_m),
    a_m(right.a_m),
    b_m(right.b_m),
    x0_m(right.x0_m),
    y0_m(right.y0_m),
92 93 94 95
    xstart_m(right.xstart_m),
    xend_m(right.xend_m),
    ystart_m(right.ystart_m),
    yend_m(right.yend_m),
96 97
    zstart_m(right.zstart_m),
    zend_m(right.zend_m),
98
    width_m(right.width_m),
gsell's avatar
gsell committed
99 100 101 102 103 104 105 106
    isAPepperPot_m(right.isAPepperPot_m),
    isASlit_m(right.isASlit_m),
    isARColl_m(right.isARColl_m),
    isACColl_m(right.isACColl_m),
    isAWire_m(right.isAWire_m),
    rHole_m(right.rHole_m),
    nHolesX_m(right.nHolesX_m),
    nHolesY_m(right.nHolesY_m),
107
    pitch_m(right.pitch_m),
adelmann's avatar
adelmann committed
108 109
    sphys_m(NULL),
    lossDs_m(nullptr)
gsell's avatar
gsell committed
110 111 112 113 114
{
  setGeom();
}


115
Collimator::Collimator(const std::string &name):
gsell's avatar
gsell committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    Component(name),
    filename_m(""),
    plane_m(OFF),
    position_m(0.0),
    PosX_m(0),
    PosY_m(0),
    PosZ_m(0),
    MomentumX_m(0),
    MomentumY_m(0),
    MomentumZ_m(0),
    time_m(0),
    id_m(0),
    informed_m(false),
    a_m(0.0),
    b_m(0.0),
    x0_m(0.0),
    y0_m(0.0),
133 134 135 136
    xstart_m(0.0),
    xend_m(0.0),
    ystart_m(0.0),
    yend_m(0.0),
137 138
    zstart_m(0.0),
    zend_m(0.0),
139
    width_m(0.0),
gsell's avatar
gsell committed
140 141 142 143 144 145 146 147
    isAPepperPot_m(false),
    isASlit_m(false),
    isARColl_m(false),
    isACColl_m(false),
    isAWire_m(false),
    rHole_m(0.0),
    nHolesX_m(0),
    nHolesY_m(0),
148
    pitch_m(0.0),
adelmann's avatar
adelmann committed
149 150
    sphys_m(NULL),
    lossDs_m(nullptr)
gsell's avatar
gsell committed
151 152 153 154
{}


Collimator::~Collimator() {
adelmann's avatar
adelmann committed
155 156
  if(online_m)
    goOffline();
gsell's avatar
gsell committed
157 158 159 160 161 162 163
}


void Collimator::accept(BeamlineVisitor &visitor) const {
    visitor.visitCollimator(*this);
}

164 165 166 167 168 169 170

inline bool Collimator::isInColl(Vector_t R, Vector_t P, double recpgamma) {
  /**
     check if we are in the longitudinal
     range of the collimator
  */
  const double z = R(2) + P(2) * recpgamma;
kraus's avatar
kraus committed
171

172 173
  if((z > position_m) && (z <= position_m + getElementLength())) {
    if(isAPepperPot_m) {
kraus's avatar
kraus committed
174

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    /**
       ------------
       |(0)|  |(0)|
       ----   -----
       |    a)    |
       |          |
       ----   -----
       |(0)|  |(0)|
       yL------------
       xL
       |---| d
       |--| pitch
       Observation: the area in a) is much larger than the
       area(s) (0). In a) particles are lost in (0)
       particles they are not lost.
kraus's avatar
kraus committed
190

191 192 193 194 195
    */
      const double h  =   pitch_m;
      const double xL = - 0.5 * h * (nHolesX_m - 1);
      const double yL = - 0.5 * h * (nHolesY_m - 1);
      bool alive = false;
kraus's avatar
kraus committed
196

197 198 199 200 201 202 203 204 205 206 207
      for(unsigned int m = 0; (m < nHolesX_m && (!alive)); m++) {
	for(unsigned int n = 0; (n < nHolesY_m && (!alive)); n++) {
	  double x_m = xL  + (m * h);
	  double y_m = yL  + (n * h);
	  /** are we in a) ? */
	  double rr = std::pow((R(0) - x_m) / rHole_m, 2) + std::pow((R(1) - y_m) / rHole_m, 2);
	  alive = (rr < 1.0);
	}
      }
      return !alive;
    } else if(isASlit_m) {
208 209 210 211 212
        return (R(0) <= -getXsize() || R(1) <= -getYsize() || R(0) >= getXsize() || R(1) >= getYsize());
    } else if (isARColl_m) {
        return (R(0) <= -getXsize() || R(1) <= -getYsize() || R(0) >= getXsize() || R(1) >= getYsize());
    } else if(isAWire_m) {
        ERRORMSG("Not yet implemented");
kraus's avatar
kraus committed
213
    } else {
214 215
        // case of an elliptic collimator
        const double trm1 = ((R(0)*R(0))/(getXsize()*getXsize()));
kraus's avatar
kraus committed
216
        const double trm2 = ((R(1)*R(1))/(getYsize()*getYsize()));
217
        return (trm1 + trm2) > 1.0;
218 219 220 221 222
    }
  }
  return false;
}

223
bool Collimator::apply(const size_t &i, const double &t, double E[], double B[]) {
gsell's avatar
gsell committed
224 225 226 227
    Vector_t Ev(0, 0, 0), Bv(0, 0, 0);
    return apply(i, t, Ev, Bv);
}

228
bool Collimator::apply(const size_t &i, const double &t, Vector_t &E, Vector_t &B) {
gsell's avatar
gsell committed
229 230 231 232
    const Vector_t &R = RefPartBunch_m->R[i] - Vector_t(dx_m, dy_m, ds_m); // including the missaligment
    const Vector_t &P = RefPartBunch_m->P[i];
    const double recpgamma = Physics::c * RefPartBunch_m->getdT() / sqrt(1.0  + dot(P, P));
    bool pdead = false;
233 234 235 236
    pdead = isInColl(R,P,recpgamma);

    if(pdead) {
      double frac = (R(2) - position_m) / P(2) * recpgamma;
adelmann's avatar
adelmann committed
237
      lossDs_m->addParticle(R,P, RefPartBunch_m->ID[i], t + frac * RefPartBunch_m->getdT(), 0);
kraus's avatar
kraus committed
238
    }
gsell's avatar
gsell committed
239 240 241 242 243 244 245
    return pdead;
}

bool Collimator::apply(const Vector_t &R, const Vector_t &centroid, const double &t, Vector_t &E, Vector_t &B) {
    return false;
}

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
bool Collimator::checkCollimator(Vector_t r, Vector_t rmin, Vector_t rmax) {

  double r_start = sqrt(xstart_m * xstart_m + ystart_m * ystart_m);
  double r_end = sqrt(xend_m * xend_m + yend_m * yend_m);
  double r1 = sqrt(rmax(0) * rmax(0) + rmax(1) * rmax(1));
  bool isDead = false;
  if(rmax(2) >= zstart_m && rmin(2) <= zend_m) {
    if( r1 > r_start - 10.0 && r1 < r_end + 10.0 ){
      if(r(2) < zend_m && r(2) > zstart_m ) {
	int pflag = checkPoint(r(0), r(1));
	isDead = (pflag != 0);
      }
    }
  }
  return isDead;
}

gsell's avatar
gsell committed
263 264

// rectangle collimators in cyclotron cyclindral coordiantes
265
// without surfacephysics, the particle hitting collimator is deleted directly
gsell's avatar
gsell committed
266 267 268 269
bool Collimator::checkCollimator(PartBunch &bunch, const int turnnumber, const double t, const double tstep) {

    bool flagNeedUpdate = false;
    Vector_t rmin, rmax;
270

gsell's avatar
gsell committed
271
    bunch.get_bounds(rmin, rmax);
272 273
    double r_start = sqrt(xstart_m * xstart_m + ystart_m * ystart_m);
    double r_end = sqrt(xend_m * xend_m + yend_m * yend_m);
gsell's avatar
gsell committed
274
    double r1 = sqrt(rmax(0) * rmax(0) + rmax(1) * rmax(1));
275 276

    if(rmax(2) >= zstart_m && rmin(2) <= zend_m) {
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
      if( r1 > r_start - 10.0 && r1 < r_end + 10.0 ){
	size_t tempnum = bunch.getLocalNum();
	int pflag = 0;
	for(unsigned int i = 0; i < tempnum; ++i) {
	  if(bunch.PType[i] == 0 && bunch.R[i](2) < zend_m && bunch.R[i](2) > zstart_m ) {
	    pflag = checkPoint(bunch.R[i](0), bunch.R[i](1));
	    if(pflag != 0) {
	      if (!sphys_m)
		lossDs_m->addParticle(bunch.R[i], bunch.P[i], bunch.ID[i]);
	      bunch.Bin[i] = -1;
	      flagNeedUpdate = true;
	    }
	  }
	}
      }
    }
    reduce(&flagNeedUpdate, &flagNeedUpdate + 1, &flagNeedUpdate, OpBitwiseOrAssign());
    if (flagNeedUpdate && sphys_m) {
      sphys_m->apply(bunch);
gsell's avatar
gsell committed
296
    }
297
    return flagNeedUpdate;
gsell's avatar
gsell committed
298 299 300 301 302 303
}

void Collimator::initialise(PartBunch *bunch, double &startField, double &endField, const double &scaleFactor) {
    RefPartBunch_m = bunch;
    position_m = startField;
    endField = position_m + getElementLength();
304

kraus's avatar
kraus committed
305 306
    sphys_m = getSurfacePhysics();

adelmann's avatar
adelmann committed
307 308 309 310 311 312
    if (!sphys_m) {
      if (filename_m == std::string(""))
        lossDs_m = std::unique_ptr<LossDataSink>(new LossDataSink(getName(), !Options::asciidump));
      else
        lossDs_m = std::unique_ptr<LossDataSink>(new LossDataSink(filename_m.substr(0, filename_m.rfind(".")), !Options::asciidump));
    }
kraus's avatar
kraus committed
313

adelmann's avatar
adelmann committed
314
    goOnline();
gsell's avatar
gsell committed
315 316 317 318
}

void Collimator::initialise(PartBunch *bunch, const double &scaleFactor) {
    RefPartBunch_m = bunch;
319 320

    sphys_m = getSurfacePhysics();
adelmann's avatar
adelmann committed
321 322 323 324 325 326 327

    if (!sphys_m) {
      if (filename_m == std::string(""))
        lossDs_m = std::unique_ptr<LossDataSink>(new LossDataSink(getName(), !Options::asciidump));
      else
        lossDs_m = std::unique_ptr<LossDataSink>(new LossDataSink(filename_m.substr(0, filename_m.rfind(".")), !Options::asciidump));
    }
kraus's avatar
kraus committed
328

adelmann's avatar
adelmann committed
329
    goOnline();
gsell's avatar
gsell committed
330 331 332 333
}

void Collimator::finalise()
{
adelmann's avatar
adelmann committed
334 335
  if(online_m)
    goOffline();
adelmann's avatar
Cleanup  
adelmann committed
336
  *gmsg << "* Finalize probe" << endl;
gsell's avatar
gsell committed
337 338 339 340 341
}

void Collimator::goOnline() {
    if(RefPartBunch_m == NULL) {
        if(!informed_m) {
342
            std::string errormsg = Fieldmap::typeset_msg("BUNCH SIZE NOT SET", "warning");
343
            ERRORMSG(errormsg << endl);
gsell's avatar
gsell committed
344 345 346 347 348 349 350 351 352 353 354
            if(Ippl::myNode() == 0) {
                ofstream omsg("errormsg.txt", ios_base::app);
                omsg << errormsg << endl;
                omsg.close();
            }
            informed_m = true;
        }
        return;
    }

    if(isAPepperPot_m)
355
        *gmsg << "* Pepperpot x= " << a_m << " y= " << b_m << " r= " << rHole_m << " nx= " << nHolesX_m << " ny= " << nHolesY_m << " pitch= " << pitch_m << endl;
gsell's avatar
gsell committed
356
    else if(isASlit_m)
357
        *gmsg << "* Slit x= " << getXsize() << " Slit y= " << getYsize() << " start= " << position_m << " fn= " << filename_m << endl;
gsell's avatar
gsell committed
358
    else if(isARColl_m)
359
        *gmsg << "* RCollimator a= " << getXsize() << " b= " << b_m << " start= " << position_m << " fn= " << filename_m << " ny= " << nHolesY_m << " pitch= " << pitch_m << endl;
gsell's avatar
gsell committed
360
    else if(isACColl_m)
361
        *gmsg << "* CCollimator angle start " << xstart_m << " (Deg) angle end " << ystart_m << " (Deg) R start " << xend_m << " (mm) R rend " << yend_m << " (mm)" << endl;
gsell's avatar
gsell committed
362
    else if(isAWire_m)
363
        *gmsg << "* Wire x= " << x0_m << " y= " << y0_m << endl;
gsell's avatar
gsell committed
364
    else
365
        *gmsg << "* ECollimator a= " << getXsize() << " b= " << b_m << " start= " << position_m << " fn= " << filename_m << " ny= " << nHolesY_m << " pitch= " << pitch_m << endl;
gsell's avatar
gsell committed
366 367 368 369 370 371 372 373 374 375 376 377

    PosX_m.reserve((int)(1.1 * RefPartBunch_m->getLocalNum()));
    PosY_m.reserve((int)(1.1 * RefPartBunch_m->getLocalNum()));
    PosZ_m.reserve((int)(1.1 * RefPartBunch_m->getLocalNum()));
    MomentumX_m.reserve((int)(1.1 * RefPartBunch_m->getLocalNum()));
    MomentumY_m.reserve((int)(1.1 * RefPartBunch_m->getLocalNum()));
    MomentumZ_m.reserve((int)(1.1 * RefPartBunch_m->getLocalNum()));
    time_m.reserve((int)(1.1 * RefPartBunch_m->getLocalNum()));
    id_m.reserve((int)(1.1 * RefPartBunch_m->getLocalNum()));
    online_m = true;
}

adelmann's avatar
adelmann committed
378 379 380
void Collimator::print() {
    if(RefPartBunch_m == NULL) {
        if(!informed_m) {
381
            std::string errormsg = Fieldmap::typeset_msg("BUNCH SIZE NOT SET", "warning");
adelmann's avatar
adelmann committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
            *gmsg << errormsg << "\n"
                << endl;
            if(Ippl::myNode() == 0) {
                ofstream omsg("errormsg.txt", ios_base::app);
                omsg << errormsg << endl;
                omsg.close();
            }
            informed_m = true;
        }
        return;
    }

    if(isAPepperPot_m)
        *gmsg << "Pepperpot x= " << a_m << " y= " << b_m << " r= " << rHole_m << " nx= " << nHolesX_m << " ny= " << nHolesY_m << " pitch= " << pitch_m << endl;
    else if(isASlit_m)
        *gmsg << "Slit x= " << getXsize() << " Slit y= " << getYsize() << " start= " << position_m << " fn= " << filename_m << endl;
    else if(isARColl_m)
        *gmsg << "RCollimator a= " << getXsize() << " b= " << b_m << " start= " << position_m << " fn= " << filename_m << " ny= " << nHolesY_m << " pitch= " << pitch_m << endl;
    else if(isACColl_m)
        *gmsg << "CCollimator angstart= " << xstart_m << " angend " << ystart_m << " rstart " << xend_m << " rend " << yend_m << endl;
    else if(isAWire_m)
        *gmsg << "Wire x= " << x0_m << " y= " << y0_m << endl;
    else
        *gmsg << "ECollimator a= " << getXsize() << " b= " << b_m << " start= " << position_m << " fn= " << filename_m << " ny= " << nHolesY_m << " pitch= " << pitch_m << endl;

}

gsell's avatar
gsell committed
409
void Collimator::goOffline() {
adelmann's avatar
adelmann committed
410
  if (online_m && lossDs_m)
adelmann's avatar
adelmann committed
411
    lossDs_m->save();
adelmann's avatar
adelmann committed
412
  online_m = false;
gsell's avatar
gsell committed
413 414 415 416 417 418
}

bool Collimator::bends() const {
    return false;
}

419
void Collimator::setOutputFN(std::string fn) {
gsell's avatar
gsell committed
420 421 422 423
    filename_m = fn;
}

string Collimator::getOutputFN() {
adelmann's avatar
adelmann committed
424 425
    if (filename_m == std::string(""))
        return getName();
kraus's avatar
kraus committed
426
    else
adelmann's avatar
adelmann committed
427
        return filename_m.substr(0, filename_m.rfind("."));
gsell's avatar
gsell committed
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
}

void Collimator::setXsize(double a) {
    a_m = a;
}

void Collimator::setYsize(double b) {
    b_m = b;
}

void Collimator::setXpos(double x0) {
    x0_m = x0;
}

void Collimator::setYpos(double y0) {
    y0_m = y0;
}


double Collimator::getXsize(double a) {
    return a_m;
}

double Collimator::getYsize(double b) {
    return b_m;
}

double Collimator::getXpos() {
    return x0_m;
}

double Collimator::getYpos() {
    return y0_m;

    // --------Cyclotron collimator
}
464 465
void Collimator::setXStart(double xstart) {
    xstart_m = xstart;
gsell's avatar
gsell committed
466 467
}

468 469
void Collimator::setXEnd(double xend) {
    xend_m = xend;
gsell's avatar
gsell committed
470 471
}

472 473
void Collimator::setYStart(double ystart) {
    ystart_m = ystart;
gsell's avatar
gsell committed
474 475
}

476 477
void Collimator::setYEnd(double yend) {
    yend_m = yend;
gsell's avatar
gsell committed
478 479
}

480 481 482 483 484 485 486 487
void Collimator::setZStart(double zstart) {
    zstart_m = zstart;
}

void Collimator::setZEnd(double zend) {
    zend_m = zend;
}

488 489
void Collimator::setWidth(double width) {
    width_m = width;
gsell's avatar
gsell committed
490 491
}

492 493
double Collimator::getXStart() {
    return xstart_m;
gsell's avatar
gsell committed
494 495
}

496 497
double Collimator::getXEnd() {
    return xend_m;
gsell's avatar
gsell committed
498 499
}

500 501
double Collimator::getYStart() {
    return ystart_m;
gsell's avatar
gsell committed
502 503
}

504 505
double Collimator::getYEnd() {
    return yend_m;
gsell's avatar
gsell committed
506 507
}

508 509 510 511 512 513 514 515
double Collimator::getZStart() {
    return zstart_m;
}

double Collimator::getZEnd() {
    return zend_m;
}

gsell's avatar
gsell committed
516
double Collimator::getWidth() {
517
    return width_m;
gsell's avatar
gsell committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
}

//-------------------------------

void Collimator::setRHole(double r) {
    rHole_m = r;
}
void Collimator::setNHoles(unsigned int nx, unsigned int ny) {
    nHolesX_m = nx;
    nHolesY_m = ny;
}
void Collimator::setPitch(double p) {
    pitch_m = p;
}


void Collimator::setPepperPot() {
    isAPepperPot_m = true;
}
void Collimator::setSlit() {
    isASlit_m = true;
}

void Collimator::setRColl() {
    isARColl_m = true;
}

void Collimator::setCColl() {
    isACColl_m = true;
}

void Collimator::setWire() {
    isAWire_m = true;
}
void Collimator::getDimensions(double &zBegin, double &zEnd) const {
    zBegin = position_m;
    zEnd = position_m + getElementLength();

    // zBegin = position_m - 0.005;
    //  zEnd = position_m + 0.005;

}

561 562
const std::string &Collimator::getType() const {
    static const std::string type("Collimator");
gsell's avatar
gsell committed
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
    return type;
}

string Collimator::getCollimatorShape() {
    if(isAPepperPot_m)
        return "PeperPot";
    else if(isASlit_m)
        return "Slit";
    else if(isARColl_m)
        return "RCollimator";
    else if(isACColl_m)
        return "CCollimator";
    else if(isAWire_m)
        return "Wire";
    else
        return "ECollimator";
}

void Collimator::setGeom() {

583 584 585 586 587 588 589 590 591 592 593
    double slope;
    if (xend_m == xstart_m)
      slope = 1.0e12;
    else
      slope = (yend_m - ystart_m) / (xend_m - xstart_m);

    double coeff2 = sqrt(1 + slope * slope);
    double coeff1 = slope / coeff2;
    double halfdist = width_m / 2.0;
    geom_m[0].x = xstart_m - halfdist * coeff1;
    geom_m[0].y = ystart_m + halfdist / coeff2;
gsell's avatar
gsell committed
594

595 596
    geom_m[1].x = xstart_m + halfdist * coeff1;
    geom_m[1].y = ystart_m - halfdist / coeff2;
gsell's avatar
gsell committed
597

598 599
    geom_m[2].x = xend_m + halfdist * coeff1;
    geom_m[2].y = yend_m - halfdist  / coeff2;
gsell's avatar
gsell committed
600

601 602
    geom_m[3].x = xend_m - halfdist * coeff1;
    geom_m[3].y = yend_m + halfdist / coeff2;
gsell's avatar
gsell committed
603 604 605 606

    geom_m[4].x = geom_m[0].x;
    geom_m[4].y = geom_m[0].y;

607 608 609 610 611 612
    if (zstart_m > zend_m){
      double tempz = 0.0;
      tempz = zstart_m;
      zstart_m = zend_m;
      zend_m = tempz;
    }
gsell's avatar
gsell committed
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
}


int Collimator::checkPoint(const double &x, const double &y) {
    int    cn = 0;

    for(int i = 0; i < 4; i++) {
        if(((geom_m[i].y <= y) && (geom_m[i+1].y > y))
           || ((geom_m[i].y > y) && (geom_m[i+1].y <= y))) {

            float vt = (float)(y - geom_m[i].y) / (geom_m[i+1].y - geom_m[i].y);
            if(x < geom_m[i].x + vt * (geom_m[i+1].x - geom_m[i].x))
                ++cn;
        }
    }
    return (cn & 1);  // 0 if even (out), and 1 if odd (in)
}