Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 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 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 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 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 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 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
#include<string>
//#include<vector>
#include <sstream>
std::string IntToString(int num)
 {
   std::ostringstream myStream; //creates an ostringstream object
   myStream << num << std::flush;

  /*
   * outputs the number into the string stream and then flushes
   * the buffer (makes sure the output is put into the stream)
   */

   return(myStream.str()); //returns the string form of the stringstream object
 }

// 
// File H4Geom.cxx
//
/*! \class H4Geom
 * \brief A helper class with geometry information of the super module
 *
 * Author: Giovanni Franzoni
 * Crystal numbering schemes during automn 2004 (SM10):
 * - <b>Conventions</b>
 *   SM Crystal Number range is [1, 1700]
 *   SM Tower Number range is [1, 68]
 *   eta range is [0, 84]
 *   phi range is [0, 19]
 * - <b>Crystal Number in Readout-Order</b>
 *   Crystal numbering in readout order is very similar to 2003 with two
 *   different orders according to the LVRB position (right or left) on the
 *   trigger tower.
 *   \verbatim
 *                  <- eta
 *   +--+--+--+--+--+      +--+--+--+--+--+
 *   | 4| 5|14|15|24|      |20|19|10| 9| 0|
 *   +--+--+--+--+--+      +--+--+--+--+--+
 *   | 3| 6|13|16|23|      |21|18|11| 8| 1|
 *   +--+--+--+--+--+      +--+--+--+--+--+  ||
 *   | 2| 7|12|17|22|      |22|17|12| 7| 2|  || phi
 *   +--+--+--+--+--+      +--+--+--+--+--+  \/
 *   | 1| 8|11|18|21|      |23|16|12| 6| 3| 
 *   +--+--+--+--+--+      +--+--+--+--+--+
 *   | 0| 9|10|19|20|      |24|15|14| 5| 4| 
 *   +--+--+--+--+--+      +--+--+--+--+--+
 *      left tower            right tower
 *   \endverbatim
 *   In order to know if a trigger tower (from 1 -> 68) is right or left, the
 *   following logic can be applied:
 *     if ((smTower - 1)/4 < 3 || ((smTower - 1)/4 - 3)%4 >= 2)         
 *       left tower
 *     else
 *       right tower
 * - <b>Super Module Crystal Number</b> 
 *   Organization of crystals in the lines of a supermodule has been changed
 *   in 2004:
 *   \verbatim
 *(i = 84, j = 0)          <- eta               (i = 0, j = 0)
 *      +----+----+     +----+----+----+----+----+----+
 *      |1681|1661| ... | 101|  81|  61|  41|  21|   1|
 *      +----+----+     +----+----+----+----+----+----+
 *      |1682|1662| ... | 102|  82|  62|  42|  22|   2|
 *      +----+----+     +----+----+----+----+----+----+  
 *      ...                                              ||      |
 *      +----+----+     +----+----+----+----+----+----+  || phi  |
 *      |1698|1678| ... | 118|  98|  78|  58|  38|  18|  \/      |t
 *      +----+----+     +----+----+----+----+----+----+          |o
 *      |1699|1679| ... | 119|  99|  79|  59|  39|  19|          |w
 *      +----+----+     +----+----+----+----+----+----+          |e
 *      |1700|1680| ... | 120| 100|  80|  60|  40|  20|          |r
 *      +----+----+     +----+----+----+----+----+----+          |
 *(i = 84, j = 19)                              (i = 0, j = 19)  
 *                           --------- tower ----------
 *   \endverbatim
 *   smCrystalNumber is obtained from i and j:
 *     smCrystalNumber = i*5*4 + j + 1
 *   conversly:
 *     i = (smCrystalNumber - 1)/(5*4)
 *     j = (smCrystalNumber - 1)%(5*4)
 *
 *   the logic to determine i and j from smTower and crystal readout number is:
 *     if (tower is left)
 *       i = (smTower - 1)/4 * 5 + (24 - crystal)/5
 *     else
 *       i = (smTower - 1)/4 * 5 + crystal/5
 *     if (tower is left && (crystal/5)%2 = 0 || 
 *         tower is right && (crystal/5)%2 = 1)
 *       j = (smTower - 1)%4 * 5 + 4 - crystal%5
 *     else
 *       j = (smTower - 1)%4 * 5 + crystal%5
 *   conversly
 *     smTower = i/5 * 4 + j/5 + 1
 *     if (tower is left)
 *       if ((i%5)%2 = 0)
 *         crystal = (4 - i%5)*5 + 4 - j%5
 *       else
 *         crystal = (4 - i%5)*5 + j%5
 *     else
 *       if ((i%5)%2 = 0)
 *         crystal = i%5 * 5 + j%5
 *       else
 *         crystal = i%5 * 5 + 4 - j%5 
 *
 *
 * Crystal numbering schemes in 2003:
 * - <b>Crystal Number in Readout-Order</b>
 *   The data on the RRF files contain the crystal information in
 *   readout order. The physical position of the crystals in readout
 *   order depends then on the VFE-Module and Tower-Number. There
 *   exist two different kinds of towers, one where the low voltage
 *   board sits on the left side, and one where the board sits on the
 *   right side. The following scheme shows the position in eta and
 *   phi of the crystals given their readout crystal number: 
 *   \verbatim
 *                  <- eta
 *   +--+--+--+--+--+      +--+--+--+--+--+
 *   | 4| 5|14|15|24|      |20|19|10| 9| 0|
 *   +--+--+--+--+--+      +--+--+--+--+--+
 *   | 3| 6|13|16|23|      |21|18|11| 8| 1|
 *   +--+--+--+--+--+      +--+--+--+--+--+  ^
 *   | 2| 7|12|17|22|      |22|17|12| 7| 2|  | phi
 *   +--+--+--+--+--+      +--+--+--+--+--+  |
 *   | 1| 8|11|18|21|      |23|16|12| 6| 3| 
 *   +--+--+--+--+--+      +--+--+--+--+--+
 *   | 0| 9|10|19|20|      |24|15|14| 5| 4| 
 *   +--+--+--+--+--+      +--+--+--+--+--+
 *      left tower            right tower
 *   \endverbatim
 *   <b>If in this documentation a crystal number is mentioned, then by
 *   default, the readout number is meant. </b>
 * - <b>Super Module Crystal Number</b> 
 *   Organization of crystals in the lines of a supermodule:
 *   \verbatim
 *                 <- eta
 *      +----+----+     +----+----+----+----+----+----+
 *      |1699|1698| ... |1620|1919|1618|1617|1616|1615|
 *      +----+----+     +----+----+----+----+----+----+
 *      |1614|1613| ... |1535|1534|1533|1532|1531|1530|
 *      +----+----+     +----+----+----+----+----+----+  ^
 *      ...                                              | phi  |
 *      +----+----+     +----+----+----+----+----+----+  |      |
 *      | 254| 253| ... | 175| 174| 173| 172| 171| 170|         |t
 *      +----+----+     +----+----+----+----+----+----+         |o
 *      | 169| 168| ... |  90|  89|  88|  87|  86|  85|         |w
 *      +----+----+     +----+----+----+----+----+----+         |e
 *      |  84|  83| ... |   5|   4|   3|   2|   1|   0|         |r
 *      +----+----+     +----+----+----+----+----+----+         |
 *
 *                           --------- tower ----------
 *   \endverbatim
 *   The above numbering scheme schows the numbers of a crystal in the
 *   super module (SM). The sm-crystal-number allows to uniquely
 *   identify the crystal in the H4 test-beams since only one SM is put
 *   into the beam at a time. The sm-crystal-number allows navigation
 *   across the boarders of the towers and is used on the ERF level by
 *   void H4RecEnergy::getCrystal(H4RecXtal * oneXtal, int smCrystal).
 * - <b>Crystal Number in Geometrical Order</b>
 *   The geometrical crystal number within a tower reflects the
 *   physical position of a crystal and is therefore used on the ERF
 *   level where higher level information is of interest. The
 *   geometrical ordering counts crystals as:
 *   \verbatim
 *        <- eta
 *   +--+--+--+--+--+
 *   |24|19|14| 9| 4|
 *   +--+--+--+--+--+
 *   |23|18|13| 8| 3|  ^
 *   +--+--+--+--+--+  | phi
 *   |22|17|12| 7| 2|  |
 *   +--+--+--+--+--+ 
 *   |21|16|11| 6| 1| 
 *   +--+--+--+--+--+ 
 *   |20|15|10| 5| 0| 
 *   +--+--+--+--+--+ 
 *   \endverbatim
 *   This numbering scheme is offered by 
 *   void H4RecEnergy::getTower(int towerNb, H4RecXtal * tower[])
 *
 * The H4Geom class is providing methods to convert these numbers into
 * each other.
*/

#include "./Geom.h"
#include <iostream>

using namespace std;

const int H4Geom::crystalChannelMap[5][5] = {
  { 4, 5, 14, 15, 24},
  { 3, 6, 13, 16, 23},
  { 2, 7, 12, 17, 22},
  { 1, 8, 11, 18, 21},
  { 0, 9, 10, 19, 20}
};

const int H4Geom::crystalMap[25] = {
  340, 255, 170,  85,   0,
    1,  86, 171, 256, 341,
  342, 257, 172,  87,   2,
    3,  88, 173, 258, 343,
  344, 259, 174,  89,   4
};
const int H4Geom::WhichHalf[69] = {
  0, // TT 0 does not exist 
  1,1,1,1,
  2,2,1,1,
  2,2,1,1,
  2,2,1,1,
  2,2,1,1,
  2,2,1,1,
  2,2,1,1,
  2,2,1,1,
  2,2,1,1,
  2,2,1,1,
  2,2,1,1,
  2,2,1,1,
  2,2,1,1,
  2,2,1,1,
  2,2,1,1,
  2,2,1,1,
  2,2,1,1};


H4Geom::GeomPeriod_t H4Geom::geometry_ = H4Geom::Undef;

// initializes geometry since we always use Automn2004
// (previously done in init() )
H4Geom::H4Geom()
{ 
  GeomPeriod_t ForEcalMonitoring;    ForEcalMonitoring = Automn2004;
  H4Geom::SetGeomPeriod(ForEcalMonitoring);
}

//! does nothing
H4Geom::~H4Geom()
{ }

//! Initialize geometry with config file
bool H4Geom::init()
{
  //   if (gConfigParser->isDefined("Geometry::Period"))
  //     geometry_ = H4Geom::GeomPeriod_t(gConfigParser->
  // 				     readIntOption("Geometry::Period"));
  //   else
  //     return false;
  
  // initialization of period moved to constructor, since we always use Automn2004
  //   GeomPeriod_t ForEcalMonitoring;    ForEcalMonitoring = Automn2004;
  //   H4Geom::SetGeomPeriod(ForEcalMonitoring);
  return true;
}

//! Retuns the crystal number in the super module for a given
//! tower number in the super module and crystal number in the tower
int H4Geom::getSMCrystalNumber(int tower, int crystal) const 
{
  if (!IsGeomPeriodDefined()) 
    {cout << "[H4Geom] geometry period not defined, aborting.\t (should be  ForEcalMonitoring = Automn2004;)" << endl;
      abort();}

  if (geometry_ == H4Geom::Spring2004) {
    int smCrystalNbr = 0;
    if ((crystal/5)%2 == 0)
      smCrystalNbr = kCrystalsPerTower - 
	int(crystal/kCardsPerTower)*kChannelsPerCard - 
	crystal%kChannelsPerCard + (tower - 1)*kCrystalsPerTower;
    else
      smCrystalNbr = kCrystalsPerTower + 1 - 
	(int(crystal/kCardsPerTower) + 1)*kChannelsPerCard + 
	crystal%kChannelsPerCard + (tower - 1)*kCrystalsPerTower;
    return smCrystalNbr;
  } else if (geometry_ == H4Geom::Automn2004) {
    int eta = (tower - 1)/kTowersInPhi*kCardsPerTower;
    int phi = (tower - 1)%kTowersInPhi*kChannelsPerCard;
    if (rightTower(tower))
      eta += crystal/kCardsPerTower;
    else
      eta += (kCrystalsPerTower - 1 - crystal)/kCardsPerTower;
    if (rightTower(tower) && (crystal/kCardsPerTower)%2 == 1 ||
	!rightTower(tower) && (crystal/kCardsPerTower)%2 == 0)
      phi += (kChannelsPerCard - 1 - crystal%kChannelsPerCard);
    else
      phi += crystal%kChannelsPerCard;
    return eta*kChannelsPerCard*kTowersInPhi + phi + 1;
  } else {
    int towerId = tower - 1;
    int line = towerId%4;
    int column = towerId/4;
    int lowerRight = 5*line*85 + column*5;
    if (rightTower(tower))
      return lowerRight+crystalMap[crystal];
    else
      return lowerRight+crystalMap[24-crystal];
  }
}


//! Retuns the crystal number in the super module for a given
//! tower number in the super module and crystal number in the tower
int H4Geom::getSMCrystalNumber(int tower, int strip_id, int crystal_id) const {
  if (
      !(0< strip_id && strip_id <6
	&& 0< crystal_id && crystal_id <6)
      )    {
    cout << "[H4geom] invalid strip_id or crystal_id; ";
    cout << "strip= "<<strip_id << " crystal_id= " << crystal_id << endl ;
    //    abort();
    return -1;
  }
  else
    {
      //      cout << "[H4Geom][getSMCrystalNumber] doing nothing for now" << endl;
      //       strip_id and crystal_id --> {1,2,3,4,5}
      //       crystal_id = 5 - crystal_id + 1;
      //       int  crystalInTower = 5*(strip_id-1) + crystal_id-1 ;
      int  crystalInTower = 5*(strip_id-1) + crystal_id -1;
      //      cout << "[H4geom][debug] crystalInTower is: " << crystalInTower << endl;
      return 
	H4Geom::getSMCrystalNumber(tower, crystalInTower); // should be ok for TT right
    }
}


//! Retuns the crystal number in the super module for a given
//! tower number in the super module and crystal number in the tower
void H4Geom::getTowerStripChannelNumber(int& tower, int& strip_id, int& crystal_id, int sm_num) const {
  int CryNum;
  getTowerCrystalNumber(tower,CryNum,sm_num);
  strip_id   = CryNum/5 + 1;
  crystal_id = CryNum%5 + 1;
}




//! Returns the crystal number in a tower for a given
//! crystal number in the super module
void H4Geom::getTowerCrystalNumber(int &tower, int &crystal, int smCrystal) const 
{
  if (!IsGeomPeriodDefined()) 
    {cout << "[H4Geom] geometry period not defined, aborting.\t (should be  ForEcalMonitoring = Automn2004;)" << endl;
      abort();}
  if (geometry_ == H4Geom::Spring2004) {
    // We do not know where the supercrystal is...
    tower = (smCrystal - 1)/kCrystalsPerTower + 1; 
    smCrystal -= (tower - 1)*kCrystalsPerTower;
    if (((smCrystal - 1)/kCardsPerTower)%2 == 0)
      crystal = (kCardsPerTower - 
		 int((smCrystal - 1)/kCardsPerTower))*kCardsPerTower - 
                (smCrystal - 1)%kCardsPerTower - 1;
    else
      crystal = (kCardsPerTower - 1 - 
		 int((smCrystal - 1)/kCardsPerTower))*kCardsPerTower + 
                (smCrystal - 1)%kCardsPerTower;
    return;
  } else if (geometry_ == H4Geom::Automn2004) {
    int eta = (smCrystal - 1)/(kChannelsPerCard*kTowersInPhi);
    int phi = (smCrystal - 1)%(kChannelsPerCard*kTowersInPhi);
    tower = eta/kCardsPerTower*kTowersInPhi + phi/kChannelsPerCard + 1;
    if (rightTower(tower)) {
      crystal = (eta%kCardsPerTower)*kChannelsPerCard;
      if ((eta%kChannelsPerCard)%2 == 0)
	crystal += phi%kChannelsPerCard;
      else
	crystal += kChannelsPerCard - 1 - phi%kChannelsPerCard;
    } else {
      crystal = (kCardsPerTower - 1 - eta%kCardsPerTower)*kChannelsPerCard;
      if ((eta%kChannelsPerCard)%2 == 0)
	crystal += kChannelsPerCard - 1 - phi%kChannelsPerCard;
      else
	crystal += phi%kChannelsPerCard;
    }
  } else {
    int line = smCrystal/85;
    int cInLine = smCrystal%85;
    
    tower= 1 + (cInLine/5) * 4 + line/5; // tower number in SM
    crystal = crystalChannelMap[line%5][cInLine%5];
    if (leftTower(tower)) crystal = 24-crystal ;
  }
  return;
}


//! Returns the crystal number (readout order) in a tower 
//! for a given position in the tower (crystalNbGeom=0 is the 
//! lower-right corner and crystalNbGeom=24 is the upper-left corner,
//! see scheme in H4Geom::getTower)
int H4Geom::getTowerCrystalNumber(int smTowerNb, int crystalNbGeom) const
{
  if (crystalNbGeom < 0 || crystalNbGeom >= kCrystalsPerTower) return -1 ;
  int column = crystalNbGeom/kCardsPerTower;
  int line = crystalNbGeom%kChannelsPerCard;
  int crystal = crystalChannelMap[line][column];
  if (leftTower(smTowerNb)) crystal = kCrystalsPerTower - 1 - crystal;
  return crystal;
}


//! Returns the crystal coordinates (eta, phi index) for a given
//! crystal number in the super module
void H4Geom::getCrystalCoord(int &eta, int &phi, int smCrystal) const
{
  if (geometry_ == H4Geom::Spring2004) {
    eta = (smCrystal - 1)/kChannelsPerCard; // arbitrary units
    phi = kChannelsPerCard - 1 - 
      ((smCrystal - 1)%kCrystalsPerTower)%kChannelsPerCard;
  } else if (geometry_ == H4Geom::Automn2004) {
    eta = (smCrystal - 1)/(kChannelsPerCard*kTowersInPhi);
    phi = (smCrystal - 1)%(kChannelsPerCard*kTowersInPhi);
  } else {
    eta = smCrystal%kCrystalsInEta;
    phi = smCrystal/kCrystalsInEta;
  }
}


//! Retuns the crystal number in the super module for given coordinates
int H4Geom::getSMCrystalFromCoord(int eta, int phi) const
{
  if (geometry_ == H4Geom::Spring2004) {
    if (eta >= 9 || eta < 0 || phi >= 4 || phi < 0) 
      return -1; // Only two supercrystals
    return eta*kCardsPerTower + (kChannelsPerCard - phi);
  }
  if (eta < 0 || eta >= kCrystalsInEta || phi < 0 || phi >= kCrystalsInPhi) 
    return -1; // non real smCrystal
  else {
    if (geometry_ == H4Geom::Automn2004)
      return eta*kChannelsPerCard*kTowersInPhi + phi + 1;
    else
      return eta + kCrystalsInEta*phi;
  }
}


//! Returns left neighbour of a sm crystal.
//! Input and output are crystal numbers in the super module.
//! A negative output means outside of the supermodule.
int H4Geom::getLeft(int smCrystal) const
{
  if (!IsGeomPeriodDefined()) 
    {cout << "[H4Geom] geometry period not defined, aborting.\t (should be  ForEcalMonitoring = Automn2004;)" << endl;
      abort();}
  if (geometry_ == H4Geom::Spring2004) {
    smCrystal += kChannelsPerCard;
    if (smCrystal > 2*kCrystalsPerTower || smCrystal < 1) return -1;
    return smCrystal;
  } else if (geometry_ == H4Geom::Automn2004) {
    smCrystal += kCrystalsInPhi;
    if (smCrystal > kCrystals) return -1;
    return smCrystal;
  } else {
    smCrystal++;
    if (!(smCrystal % kCrystalsInEta)) return -1;
    if (smCrystal >= kCrystals || smCrystal < 0) return -1;
    return smCrystal;
  }
}


//! Returns right neighbour of a sm crystal.
//! Input and output are crystal numbers in the super module.
//! A negative output means outside of the supermodule.
int H4Geom::getRight(int smCrystal) const
{
  if (!IsGeomPeriodDefined()) 
    {cout << "[H4Geom] geometry period not defined, aborting.\t (should be  ForEcalMonitoring = Automn2004;)" << endl;
      abort();}
  if (geometry_ == H4Geom::Spring2004) {
    smCrystal -= kChannelsPerCard;
    if (smCrystal > 2*kCrystalsPerTower || smCrystal < 1) return -1;
    return smCrystal;
  } else if (geometry_ == H4Geom::Automn2004) {
    smCrystal -= kCrystalsInPhi;
    if (smCrystal < 0) return -1;
    return smCrystal;
  } else {
    smCrystal--;
    if (smCrystal % kCrystalsInEta == kCrystalsInEta - 1) return -1;
    if (smCrystal >= kCrystals || smCrystal < 0) return -1;
    return smCrystal;
  }
}


//! Returns upper neighbour of a sm crystal.
//! Input and output are crystal numbers in the super module.
//! A negative output means outside of the supermodule.
int H4Geom::getUpper(int smCrystal) const{
  if (!IsGeomPeriodDefined()) 
    {cout << "[H4Geom] geometry period not defined, aborting.\t (should be  ForEcalMonitoring = Automn2004;)" << endl;
      abort();}
  if (geometry_ == H4Geom::Spring2004) {
    smCrystal--;
    if (smCrystal > 2*kCrystalsPerTower || smCrystal < 1) return -1;
    return smCrystal;
  } else if (geometry_ == H4Geom::Automn2004) {
    if ((smCrystal - 1)%kCrystalsInPhi == 0) return -1; // phi = 0
    smCrystal--;
    return smCrystal;
  } else {
    smCrystal += kCrystalsInEta;
    if (smCrystal >= kCrystals || smCrystal < 0) return -1;
    return smCrystal;
  }
}


//! Returns lower neighbour of a sm crystal.
//! Input and output are crystal numbers in the super module.
//! A negative output means outside of the supermodule.
int H4Geom::getLower(int smCrystal) const
{
  if (!IsGeomPeriodDefined()) 
    {cout << "[H4Geom] geometry period not defined, aborting.\t (should be  ForEcalMonitoring = Automn2004;)" << endl;
      abort();}
  if (geometry_ == H4Geom::Spring2004) {
    smCrystal ++;
    if (smCrystal > 2*kCrystalsPerTower || smCrystal < 1) return -1;
    return smCrystal;
  } else if (geometry_ == H4Geom::Automn2004) {
    if (smCrystal%kCrystalsInPhi == 0) return -1; // phi = 19
    smCrystal++;
    return smCrystal;
  } else {
    smCrystal -= kCrystalsInEta;
    if (smCrystal >= kCrystals || smCrystal < 0) return -1;
    return smCrystal;
  }
}


//! Returns left neighbour of a crystal referenced by its coordinates.
//! New coordonates overwrite the old ones. No check is done to see
//! if it corresponds to a real crystal. To be used with caution. 
void H4Geom::mvLeft(int &eta, int &phi) const
{
  if (eta >= kCrystalsInEta)
    std::cout << "H4Geom::mvLeft: eta is too large " << eta << std::endl;
  eta++;
}

//! Returns right neighbour of a crystal referenced by its coordinates.
//! New coordonates overwrite the old ones. No check is done to see
//! if it corresponds to a real crystal. To be used with caution. 
void H4Geom::mvRight(int &eta, int &phi) const
{
  if (eta < 0)
    std::cout << "H4Geom::mvRight: eta is too small " << eta << std::endl;
  eta--;
}

//! Returns upper neighbour of a crystal referenced by its coordinates.
//! New coordonates overwrite the old ones. No check is done to see
//! if it corresponds to a real crystal. To be used with caution. 
void H4Geom::mvUp(int &eta, int &phi) const
{
  if (phi >= kCrystalsInPhi)
    std::cout << "H4Geom::mvUp: phi is too large " << phi << std::endl;
  phi++;
}


//! Returns lower neighbour of a crystal referenced by its coordinates.
//! New coordonates overwrite the old ones. No check is done to see
//! if it corresponds to a real crystal. To be used with caution. 
void H4Geom::mvDown(int &eta, int &phi) const
{
  if (phi < 0)
    std::cout << "H4Geom::mvDown: phi is too small " << phi << std::endl;
  phi--;
}


//! Returns the 25 crystals of tower towerNb in the super module.
//! Output are crystal numbers in the super module.
//! By default, the order in the output array (tower) corresponds to
//! geometric order (index 0 is lower-right corner).
//! if order=readout, the order in the output array (tower) 
//! corresponds to the readout scheme (depends on the kind of tower)
/**
 * the geometric order is defined by:
 * \verbatim
 *  +--+--+--+--+--+
 *  |24|19|14| 9| 4|
 *  +--+--+--+--+--+
 *  |23|18|13| 8| 3|
 *  +--+--+--+--+--+  
 *  |22|17|12| 7| 2| 
 *  +--+--+--+--+--+ 
 *  |21|16|11| 6| 1| 
 *  +--+--+--+--+--+ 
 *  |20|15|10| 5| 0| 
 *  +--+--+--+--+--+ 
 * \endverbatim
 */ 
void H4Geom::getTower(int * tower, int towerNb, std::string order) const 
{
  if (!IsGeomPeriodDefined()) 
    {cout << "[H4Geom] geometry period not defined, aborting.\t (should be  ForEcalMonitoring = Automn2004;)" << endl;
      abort();}
  if (order == "readout") {
    for (int crystalNb = 0; crystalNb < kCrystalsPerTower; crystalNb++)
      tower[crystalNb] = getSMCrystalNumber(towerNb, crystalNb);
  } else {
    int towerId = towerNb - 1;
    int line = towerId%kTowersInPhi;
    int column = towerId/kTowersInPhi;
    if (geometry_ == H4Geom::Automn2004) {
      int smLowerRight = column*kCardsPerTower*kCrystalsInPhi + 
	                 (line + 1)*kChannelsPerCard;
      for (int i = 0; i < kCrystalsPerTower; i++)
	tower[i] = smLowerRight - i%kChannelsPerCard + 
                   i/kChannelsPerCard*kCrystalsInPhi;
    } else {
      int smLowerRight = kChannelsPerCard*line*kCrystalsInEta + 
	                 column*kCardsPerTower;
      for (int i = 0; i < kCrystalsPerTower; i++)
	tower[i] = i%kChannelsPerCard * kCrystalsInEta + 
	           i/kChannelsPerCard + smLowerRight;
    }
  }
  return;
}


//! Returns the 5 crystals belonging to the same VFE board as smCrystal.
//! Input and output are crystal numbers in the super module.
//! By default, the order in the output array (VFE) corresponds to
//! The geometric order (index 0 is lower-right corner).
//! if order=readout, the order in the output array (VFE) 
//! corresponds to the readout scheme (depends on the kind of tower)
/**
 * the geometric order is defined by:
 * \verbatim
 *  +-+
 *  |4|
 *  +-+
 *  |3|
 *  +-+  
 *  |2| 
 *  +-+ 
 *  |1| 
 *  +-+ 
 *  |0| 
 *  +-+ 
 * \endverbatim
 */ 
void H4Geom::getVFE(int * VFE, int smCrystal, std::string order) const 
{
  int towerNb, crystalNb;
  getTowerCrystalNumber(towerNb, crystalNb, smCrystal);
  int VFEnb = crystalNb/kChannelsPerCard;
  if (order == "readout") {
    for (crystalNb = 0; crystalNb < kChannelsPerCard; crystalNb++) 
      VFE[crystalNb] = getSMCrystalNumber(towerNb, 
					  kChannelsPerCard*VFEnb+crystalNb);
  } else {
    int eta, phi;
    getCrystalCoord(eta, phi, smCrystal);
    int smLower = eta+85*5*(phi/5);	
    for (int i = 0; i < 5; i++)
      VFE[i] =  i*85 + smLower;
  }  
}

//! Returns sm crystal numbers for crystals in a window of
//! size widthxheight centered around a given smCrystal.
//! width and height must be odd.
//! The order in the output array (window) is defined 
//! by the geometric order (index 0 is lower-right corner).
/**
 * the geometric order is defined by:
 * \verbatim
 *         width (w)
 * <--------------------->
 * +---------------------+   -
 * | h*w . . . . .  .   h|   |
 * | .              .   .|   |
 * | .              .   .|   | h
 * | .              .   .|   | e
 * | . . . . x . .  .   .|   | i (h)
 * | .              .   .|   | g
 * | .              .   .|   | h
 * | .              .   .|   | t
 * | .              .   1|   | 
 * | . . . . . . . h+1  0|   |
 * +---------------------+   -
 *
 *    x: central crystal     
 * \endverbatim
 */
void H4Geom::getWindow(int * window, int smCrystal, int width, int height) const
{
  if (width <= 0 || width%2 == 0) {
    std::cout << "H4Geom::getWindow, width should be >0 and odd!" << std::endl;
    return;
  }
  if (height <= 0 || height%2 == 0) {
    std::cout << "H4Geom::getWindow, height should be >0 and odd!" 
	      << std::endl;
    return;
  }
  int eta, phi;
  getCrystalCoord(eta, phi, smCrystal);
  // get lower-right corner
  int eta0 = eta - (width - 1)/2;
  int phi0 = phi - (height - 1)/2;
  int index = 0;
  eta = eta0;
  phi = phi0;
  for (int i = 0; i < width; i++) {
    for (int j = 0; j < height; j++) {
      window[index] = getSMCrystalFromCoord(eta, phi);
      phi++;
      index++;
    }
    eta++;
    phi = phi0;
  }
}


//! Tests if low voltage board is on the right size of the tower.
//! Readout scheme depends on that.
/**
 * the readout order for a "right-tower" is defined by:
 * \verbatim
 *  +--+--+--+--+--+
 *  |20|19|10| 9| 0|
 *  +--+--+--+--+--+
 *  |21|18|11| 8| 1|
 *  +--+--+--+--+--+  
 *  |22|17|12| 7| 2| 
 *  +--+--+--+--+--+ 
 *  |23|16|12| 6| 3| 
 *  +--+--+--+--+--+ 
 *  |24|15|14| 5| 4| 
 *  +--+--+--+--+--+ 
 * \endverbatim
 */ 
bool H4Geom::rightTower(int tower) const 
{
  if (!IsGeomPeriodDefined()) 
    {cout << "[H4Geom] geometry period not defined, aborting.\t (should be  ForEcalMonitoring = Automn2004;)" << endl;
      abort();}
  if (geometry_ == H4Geom::Automn2004) {
    if ((tower - 1)/kTowersInPhi < 3 ||
	((tower - 1)/kTowersInPhi - 3)%4 >= 2)
      return false;
    else
      return true;
  }
  if ((tower>12 && tower<21) || (tower>28 && tower<37) ||
      (tower>44 && tower<53) || (tower>60 && tower<69))
    return true;
  else
    return false;
}

//! Tests if low voltage board is on the left size of the tower.
//! Readout scheme depends on that.
/**
 * the readout order for a "left-tower" is defined by:
 * \verbatim
 *  +--+--+--+--+--+
 *  | 4| 5|14|15|24|
 *  +--+--+--+--+--+
 *  | 3| 6|13|16|23|
 *  +--+--+--+--+--+  
 *  | 2| 7|12|17|22| 
 *  +--+--+--+--+--+ 
 *  | 1| 8|11|18|21| 
 *  +--+--+--+--+--+ 
 *  | 0| 9|10|19|20| 
 *  +--+--+--+--+--+ 
 * \endverbatim
 */ 
bool H4Geom::leftTower(int tower) const
{
  return !rightTower(tower);
}

void H4Geom::SetGeomPeriod(GeomPeriod_t geometry)
{
  geometry_ = geometry;
}

bool H4Geom::IsGeomPeriodDefined() const {
  if (geometry_ == H4Geom::Undef) {
    std::cout << "Class H4Geom : geometry is not defined. You should call in"
	      << " your program H4Geom::init (and fill the [Geometry] section"
	      << " of your config file) or call H4Geom::SetGeomPeriod." 
	      << std::endl;
    return false;
  }
  return true;
}

int H4Geom::getHalf (int TT){
  if(TT < 1 || TT >68){cout <<"TT num out of range: "<<TT<<endl; return 0;}
  return WhichHalf[TT];
}