#include "TH2F.h"
#include "TLine.h"

inline void DrawES(int plane, int side) {
  int ixF[172] = {
      2,  2,  3,  3,  4,  4,  5,  5,  6,  6,  7,  7,  8,  8,  10, 10, 11, 11, 14, 14,     // external upper crown (44)
      16, 16, 26, 26, 28, 28, 31, 31, 32, 32, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38,     // external upper crown
      39, 39, 40, 40,                                                                     // external upper crown
      28, 28, 27, 27, 26, 26, 25, 25, 23, 23, 19, 19, 17, 17, 16, 16, 15, 15, 14, 14, 2,  // internal upper crown (21)
      0,  6,  6,  8,  8,  0,  16, 16, 14, 14, 0,  20, 20, 0,  26, 26, 0,  32, 32, 30, 30,
      0,                                                                                  // upper FEDs           (22)
      2,  3,  3,  4,  4,  5,  5,  6,  6,  7,  7,  8,  8,  10, 10, 11, 11, 14, 14, 16,     // external lower crown (44)
      16, 26, 26, 28, 28, 31, 31, 32, 32, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38, 39,     // external lower crown
      39, 40, 40, 0,                                                                      // external lower crown
      14, 14, 15, 15, 16, 16, 17, 17, 19, 19, 23, 23, 25, 25, 26, 26, 27, 27, 28, 28, 0,  // internal lower crown (21)
      10, 10, 12, 12, 0,  16, 16, 0,  22, 22, 0,  26, 26, 28, 28, 0,  36, 36, 34, 34};    // lower FEDs           (20)
  int iyF[172] = {
      14, 26, 26, 28, 28, 31, 31, 32, 32, 33, 33, 34, 34, 36, 36, 37, 37, 38, 38, 39,         // external upper crown
      39, 40, 40, 39, 39, 38, 38, 37, 37, 36, 36, 34, 34, 33, 33, 32, 32, 31, 31, 28,         // external upper crown
      28, 26, 26, 20,                                                                         // external upper crown
      20, 22, 22, 24, 24, 25, 25, 26, 26, 27, 27, 26, 26, 25, 25, 24, 24, 22, 22, 20, 20,     // internal upper crown
      0,  20, 30, 30, 36, 0,  24, 26, 26, 38, 0,  27, 40, 0,  24, 40, 0,  20, 24, 24, 38, 0,  // upper FEDs
      14, 14, 12, 12, 9,  9,  8,  8,  7,  7,  6,  6,  4,  4,  3,  3,  2,  2,  1,  1,          // external lower crown
      0,  0,  1,  1,  2,  2,  3,  3,  4,  4,  6,  6,  7,  7,  8,  8,  9,  9,  12, 12,         // external lower crown
      14, 14, 20, 0,                                                                          // external lower crown
      20, 18, 18, 16, 16, 15, 15, 14, 14, 13, 13, 14, 14, 15, 15, 16, 16, 18, 18, 20, 0,      // internal lower crown
      20, 16, 16, 2,  0,  16, 1,  0,  13, 0,  0,  16, 14, 14, 1,  0,  20, 10, 10, 6};         // lower FEDs
  int ixR[171] = {
      0,  0,  1,  1,  2,  2,  3,  3,  4,  4,  6,  6,  7,  7,  8,  8,  9,  9,  12, 12,  // external crown (86)
      14, 14, 26, 26, 28, 28, 31, 31, 32, 32, 33, 33, 34, 34, 36, 36, 37, 37, 38, 38,  // external crown
      39, 39, 40, 40, 39, 39, 38, 38, 37, 37, 36, 36, 34, 34, 33, 33, 32, 32, 31, 31,  // external crown
      28, 28, 26, 26, 14, 14, 12, 12, 9,  9,  8,  8,  7,  7,  6,  6,  4,  4,  3,  3,   // external crown
      2,  2,  1,  1,  0,  0,                                                           // external crown
      13, 13, 14, 14, 15, 15, 16, 16, 18, 18, 22, 22, 24, 24, 25, 25, 26, 26, 27, 27,  // internal crown (38)
      26, 26, 25, 25, 24, 24, 22, 22, 18, 18, 16, 16, 15, 15, 14, 14, 13, 0,           // internal crown
      20, 20, 0,  20, 20, 0,  2,  16, 16, 20, 0,  0,  16, 0,  0,  13, 0,  1,  14, 14,  // FEDs (47)
      16, 0,  4,  10, 10, 20, 0,  20, 24, 24, 38, 0,  24, 40, 0,  27, 40, 0,  24, 26,  // FEDs
      26, 39, 0,  20, 30, 30, 36};                                                     // FEDs
  int iyR[171] = {15, 25, 25, 27, 27, 30, 30, 31, 31, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38,  // external crown
                  38, 39, 39, 38, 38, 37, 37, 36, 36, 35, 35, 34, 34, 33, 33, 31, 31, 30, 30, 27,  // external crown
                  27, 25, 25, 15, 15, 13, 13, 10, 10, 9,  9,  7,  7,  6,  6,  5,  5,  4,  4,  3,   // external crown
                  3,  2,  2,  1,  1,  2,  2,  3,  3,  4,  4,  5,  5,  6,  6,  7,  7,  9,  9,  10,  // external crown
                  10, 13, 13, 15, 15, 0,                                                           // external crown
                  18, 22, 22, 24, 24, 25, 25, 26, 26, 27, 27, 26, 26, 25, 25, 24, 24, 22, 22, 18,  // internal crown
                  18, 16, 16, 15, 15, 14, 14, 13, 13, 14, 14, 15, 15, 16, 16, 18, 18, 0,           // internal crown
                  39, 27, 0,  13, 1,  0,  11, 11, 9,  9,  0,  15, 15, 0,  21, 21, 0,  27, 27, 25,  // FEDs
                  25, 0,  33, 33, 35, 35, 0,  31, 31, 29, 29, 0,  25, 25, 0,  19, 19, 0,  15, 15,  // FEDs
                  13, 13, 0,  5,  5,  7,  7};                                                      // FEDs

  //  std::cout << " plane " << plane << " side " << side << std::endl;
  TLine* l = new TLine;
  l->SetLineWidth(1);

  if (plane == 0 && side == 0)
    for (int i = 0; i < 171; i = i + 1) {
      if ((ixF[i] != 0 || iyF[i] != 0) && (ixF[i + 1] != 0 || iyF[i + 1] != 0)) {
        l->DrawLine(ixF[i] - 1, iyF[i], ixF[i + 1] - 1, iyF[i + 1]);
      }
    }
  else if (plane == 0 && side == 1)
    for (int i = 0; i < 171; i = i + 1) {
      if ((ixF[i] != 0 || iyF[i] != 0) && (ixF[i + 1] != 0 || iyF[i + 1] != 0)) {
        l->DrawLine(41 - ixF[i], iyF[i], 41 - ixF[i + 1], iyF[i + 1]);
      }
    }
  else if (plane == 1 && side == 1) {
    for (int i = 0; i < 170; i = i + 1) {
      if ((ixR[i] != 0 || iyR[i] != 0) && (ixR[i + 1] != 0 || iyR[i + 1] != 0)) {
        l->DrawLine(ixR[i], iyR[i], ixR[i + 1], iyR[i + 1]);
      }
    }
  } else if (plane == 1 && side == 0) {
    for (int i = 0; i < 170; i = i + 1) {
      if ((ixR[i] != 0 || iyR[i] != 0) && (ixR[i + 1] != 0 || iyR[i + 1] != 0)) {
        l->DrawLine(40 - ixR[i], iyR[i], 40 - ixR[i + 1], iyR[i + 1]);
      }
    }
  }
}
