#include "CondTools/Ecal/interface/EcalDAQHandler.h"
#include "DataFormats/EcalDetId/interface/EcalSubdetector.h"

#include "FWCore/ParameterSet/interface/ParameterSetfwd.h"

#include <iostream>

//  maps from EcalElectronicsMapping
// iphiEB and ietaEB[FedId - 610][towerId - 1]
// ixx and iyy[FEDid][iTT - 1][3] where  FEDid -= 601 if FEDid < 610, FEDid -= 637 else
// pos = 0/2 filled when needed. ixx/iyy = 0 if unused

const int iphiEB[36][68] = {
    {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2,
     3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4},
    {5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8, 5, 6,
     7, 8, 5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8, 5, 6, 7, 8},
    {9,  10, 11, 12, 9,  10, 11, 12, 9,  10, 11, 12, 9,  10, 11, 12, 9,  10, 11, 12, 9,  10, 11,
     12, 9,  10, 11, 12, 9,  10, 11, 12, 9,  10, 11, 12, 9,  10, 11, 12, 9,  10, 11, 12, 9,  10,
     11, 12, 9,  10, 11, 12, 9,  10, 11, 12, 9,  10, 11, 12, 9,  10, 11, 12, 9,  10, 11, 12},
    {13, 14, 15, 16, 13, 14, 15, 16, 13, 14, 15, 16, 13, 14, 15, 16, 13, 14, 15, 16, 13, 14, 15,
     16, 13, 14, 15, 16, 13, 14, 15, 16, 13, 14, 15, 16, 13, 14, 15, 16, 13, 14, 15, 16, 13, 14,
     15, 16, 13, 14, 15, 16, 13, 14, 15, 16, 13, 14, 15, 16, 13, 14, 15, 16, 13, 14, 15, 16},
    {17, 18, 19, 20, 17, 18, 19, 20, 17, 18, 19, 20, 17, 18, 19, 20, 17, 18, 19, 20, 17, 18, 19,
     20, 17, 18, 19, 20, 17, 18, 19, 20, 17, 18, 19, 20, 17, 18, 19, 20, 17, 18, 19, 20, 17, 18,
     19, 20, 17, 18, 19, 20, 17, 18, 19, 20, 17, 18, 19, 20, 17, 18, 19, 20, 17, 18, 19, 20},
    {21, 22, 23, 24, 21, 22, 23, 24, 21, 22, 23, 24, 21, 22, 23, 24, 21, 22, 23, 24, 21, 22, 23,
     24, 21, 22, 23, 24, 21, 22, 23, 24, 21, 22, 23, 24, 21, 22, 23, 24, 21, 22, 23, 24, 21, 22,
     23, 24, 21, 22, 23, 24, 21, 22, 23, 24, 21, 22, 23, 24, 21, 22, 23, 24, 21, 22, 23, 24},
    {25, 26, 27, 28, 25, 26, 27, 28, 25, 26, 27, 28, 25, 26, 27, 28, 25, 26, 27, 28, 25, 26, 27,
     28, 25, 26, 27, 28, 25, 26, 27, 28, 25, 26, 27, 28, 25, 26, 27, 28, 25, 26, 27, 28, 25, 26,
     27, 28, 25, 26, 27, 28, 25, 26, 27, 28, 25, 26, 27, 28, 25, 26, 27, 28, 25, 26, 27, 28},
    {29, 30, 31, 32, 29, 30, 31, 32, 29, 30, 31, 32, 29, 30, 31, 32, 29, 30, 31, 32, 29, 30, 31,
     32, 29, 30, 31, 32, 29, 30, 31, 32, 29, 30, 31, 32, 29, 30, 31, 32, 29, 30, 31, 32, 29, 30,
     31, 32, 29, 30, 31, 32, 29, 30, 31, 32, 29, 30, 31, 32, 29, 30, 31, 32, 29, 30, 31, 32},
    {33, 34, 35, 36, 33, 34, 35, 36, 33, 34, 35, 36, 33, 34, 35, 36, 33, 34, 35, 36, 33, 34, 35,
     36, 33, 34, 35, 36, 33, 34, 35, 36, 33, 34, 35, 36, 33, 34, 35, 36, 33, 34, 35, 36, 33, 34,
     35, 36, 33, 34, 35, 36, 33, 34, 35, 36, 33, 34, 35, 36, 33, 34, 35, 36, 33, 34, 35, 36},
    {37, 38, 39, 40, 37, 38, 39, 40, 37, 38, 39, 40, 37, 38, 39, 40, 37, 38, 39, 40, 37, 38, 39,
     40, 37, 38, 39, 40, 37, 38, 39, 40, 37, 38, 39, 40, 37, 38, 39, 40, 37, 38, 39, 40, 37, 38,
     39, 40, 37, 38, 39, 40, 37, 38, 39, 40, 37, 38, 39, 40, 37, 38, 39, 40, 37, 38, 39, 40},
    {41, 42, 43, 44, 41, 42, 43, 44, 41, 42, 43, 44, 41, 42, 43, 44, 41, 42, 43, 44, 41, 42, 43,
     44, 41, 42, 43, 44, 41, 42, 43, 44, 41, 42, 43, 44, 41, 42, 43, 44, 41, 42, 43, 44, 41, 42,
     43, 44, 41, 42, 43, 44, 41, 42, 43, 44, 41, 42, 43, 44, 41, 42, 43, 44, 41, 42, 43, 44},
    {45, 46, 47, 48, 45, 46, 47, 48, 45, 46, 47, 48, 45, 46, 47, 48, 45, 46, 47, 48, 45, 46, 47,
     48, 45, 46, 47, 48, 45, 46, 47, 48, 45, 46, 47, 48, 45, 46, 47, 48, 45, 46, 47, 48, 45, 46,
     47, 48, 45, 46, 47, 48, 45, 46, 47, 48, 45, 46, 47, 48, 45, 46, 47, 48, 45, 46, 47, 48},
    {49, 50, 51, 52, 49, 50, 51, 52, 49, 50, 51, 52, 49, 50, 51, 52, 49, 50, 51, 52, 49, 50, 51,
     52, 49, 50, 51, 52, 49, 50, 51, 52, 49, 50, 51, 52, 49, 50, 51, 52, 49, 50, 51, 52, 49, 50,
     51, 52, 49, 50, 51, 52, 49, 50, 51, 52, 49, 50, 51, 52, 49, 50, 51, 52, 49, 50, 51, 52},
    {53, 54, 55, 56, 53, 54, 55, 56, 53, 54, 55, 56, 53, 54, 55, 56, 53, 54, 55, 56, 53, 54, 55,
     56, 53, 54, 55, 56, 53, 54, 55, 56, 53, 54, 55, 56, 53, 54, 55, 56, 53, 54, 55, 56, 53, 54,
     55, 56, 53, 54, 55, 56, 53, 54, 55, 56, 53, 54, 55, 56, 53, 54, 55, 56, 53, 54, 55, 56},
    {57, 58, 59, 60, 57, 58, 59, 60, 57, 58, 59, 60, 57, 58, 59, 60, 57, 58, 59, 60, 57, 58, 59,
     60, 57, 58, 59, 60, 57, 58, 59, 60, 57, 58, 59, 60, 57, 58, 59, 60, 57, 58, 59, 60, 57, 58,
     59, 60, 57, 58, 59, 60, 57, 58, 59, 60, 57, 58, 59, 60, 57, 58, 59, 60, 57, 58, 59, 60},
    {61, 62, 63, 64, 61, 62, 63, 64, 61, 62, 63, 64, 61, 62, 63, 64, 61, 62, 63, 64, 61, 62, 63,
     64, 61, 62, 63, 64, 61, 62, 63, 64, 61, 62, 63, 64, 61, 62, 63, 64, 61, 62, 63, 64, 61, 62,
     63, 64, 61, 62, 63, 64, 61, 62, 63, 64, 61, 62, 63, 64, 61, 62, 63, 64, 61, 62, 63, 64},
    {65, 66, 67, 68, 65, 66, 67, 68, 65, 66, 67, 68, 65, 66, 67, 68, 65, 66, 67, 68, 65, 66, 67,
     68, 65, 66, 67, 68, 65, 66, 67, 68, 65, 66, 67, 68, 65, 66, 67, 68, 65, 66, 67, 68, 65, 66,
     67, 68, 65, 66, 67, 68, 65, 66, 67, 68, 65, 66, 67, 68, 65, 66, 67, 68, 65, 66, 67, 68},
    {69, 70, 71, 72, 69, 70, 71, 72, 69, 70, 71, 72, 69, 70, 71, 72, 69, 70, 71, 72, 69, 70, 71,
     72, 69, 70, 71, 72, 69, 70, 71, 72, 69, 70, 71, 72, 69, 70, 71, 72, 69, 70, 71, 72, 69, 70,
     71, 72, 69, 70, 71, 72, 69, 70, 71, 72, 69, 70, 71, 72, 69, 70, 71, 72, 69, 70, 71, 72},
    {4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3,
     2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1},
    {8, 7, 6, 5, 8, 7, 6, 5, 8, 7, 6, 5, 8, 7, 6, 5, 8, 7, 6, 5, 8, 7, 6, 5, 8, 7, 6, 5, 8, 7, 6, 5, 8, 7,
     6, 5, 8, 7, 6, 5, 8, 7, 6, 5, 8, 7, 6, 5, 8, 7, 6, 5, 8, 7, 6, 5, 8, 7, 6, 5, 8, 7, 6, 5, 8, 7, 6, 5},
    {12, 11, 10, 9,  12, 11, 10, 9,  12, 11, 10, 9,  12, 11, 10, 9,  12, 11, 10, 9,  12, 11, 10,
     9,  12, 11, 10, 9,  12, 11, 10, 9,  12, 11, 10, 9,  12, 11, 10, 9,  12, 11, 10, 9,  12, 11,
     10, 9,  12, 11, 10, 9,  12, 11, 10, 9,  12, 11, 10, 9,  12, 11, 10, 9,  12, 11, 10, 9},
    {16, 15, 14, 13, 16, 15, 14, 13, 16, 15, 14, 13, 16, 15, 14, 13, 16, 15, 14, 13, 16, 15, 14,
     13, 16, 15, 14, 13, 16, 15, 14, 13, 16, 15, 14, 13, 16, 15, 14, 13, 16, 15, 14, 13, 16, 15,
     14, 13, 16, 15, 14, 13, 16, 15, 14, 13, 16, 15, 14, 13, 16, 15, 14, 13, 16, 15, 14, 13},
    {20, 19, 18, 17, 20, 19, 18, 17, 20, 19, 18, 17, 20, 19, 18, 17, 20, 19, 18, 17, 20, 19, 18,
     17, 20, 19, 18, 17, 20, 19, 18, 17, 20, 19, 18, 17, 20, 19, 18, 17, 20, 19, 18, 17, 20, 19,
     18, 17, 20, 19, 18, 17, 20, 19, 18, 17, 20, 19, 18, 17, 20, 19, 18, 17, 20, 19, 18, 17},
    {24, 23, 22, 21, 24, 23, 22, 21, 24, 23, 22, 21, 24, 23, 22, 21, 24, 23, 22, 21, 24, 23, 22,
     21, 24, 23, 22, 21, 24, 23, 22, 21, 24, 23, 22, 21, 24, 23, 22, 21, 24, 23, 22, 21, 24, 23,
     22, 21, 24, 23, 22, 21, 24, 23, 22, 21, 24, 23, 22, 21, 24, 23, 22, 21, 24, 23, 22, 21},
    {28, 27, 26, 25, 28, 27, 26, 25, 28, 27, 26, 25, 28, 27, 26, 25, 28, 27, 26, 25, 28, 27, 26,
     25, 28, 27, 26, 25, 28, 27, 26, 25, 28, 27, 26, 25, 28, 27, 26, 25, 28, 27, 26, 25, 28, 27,
     26, 25, 28, 27, 26, 25, 28, 27, 26, 25, 28, 27, 26, 25, 28, 27, 26, 25, 28, 27, 26, 25},
    {32, 31, 30, 29, 32, 31, 30, 29, 32, 31, 30, 29, 32, 31, 30, 29, 32, 31, 30, 29, 32, 31, 30,
     29, 32, 31, 30, 29, 32, 31, 30, 29, 32, 31, 30, 29, 32, 31, 30, 29, 32, 31, 30, 29, 32, 31,
     30, 29, 32, 31, 30, 29, 32, 31, 30, 29, 32, 31, 30, 29, 32, 31, 30, 29, 32, 31, 30, 29},
    {36, 35, 34, 33, 36, 35, 34, 33, 36, 35, 34, 33, 36, 35, 34, 33, 36, 35, 34, 33, 36, 35, 34,
     33, 36, 35, 34, 33, 36, 35, 34, 33, 36, 35, 34, 33, 36, 35, 34, 33, 36, 35, 34, 33, 36, 35,
     34, 33, 36, 35, 34, 33, 36, 35, 34, 33, 36, 35, 34, 33, 36, 35, 34, 33, 36, 35, 34, 33},
    {40, 39, 38, 37, 40, 39, 38, 37, 40, 39, 38, 37, 40, 39, 38, 37, 40, 39, 38, 37, 40, 39, 38,
     37, 40, 39, 38, 37, 40, 39, 38, 37, 40, 39, 38, 37, 40, 39, 38, 37, 40, 39, 38, 37, 40, 39,
     38, 37, 40, 39, 38, 37, 40, 39, 38, 37, 40, 39, 38, 37, 40, 39, 38, 37, 40, 39, 38, 37},
    {44, 43, 42, 41, 44, 43, 42, 41, 44, 43, 42, 41, 44, 43, 42, 41, 44, 43, 42, 41, 44, 43, 42,
     41, 44, 43, 42, 41, 44, 43, 42, 41, 44, 43, 42, 41, 44, 43, 42, 41, 44, 43, 42, 41, 44, 43,
     42, 41, 44, 43, 42, 41, 44, 43, 42, 41, 44, 43, 42, 41, 44, 43, 42, 41, 44, 43, 42, 41},
    {48, 47, 46, 45, 48, 47, 46, 45, 48, 47, 46, 45, 48, 47, 46, 45, 48, 47, 46, 45, 48, 47, 46,
     45, 48, 47, 46, 45, 48, 47, 46, 45, 48, 47, 46, 45, 48, 47, 46, 45, 48, 47, 46, 45, 48, 47,
     46, 45, 48, 47, 46, 45, 48, 47, 46, 45, 48, 47, 46, 45, 48, 47, 46, 45, 48, 47, 46, 45},
    {52, 51, 50, 49, 52, 51, 50, 49, 52, 51, 50, 49, 52, 51, 50, 49, 52, 51, 50, 49, 52, 51, 50,
     49, 52, 51, 50, 49, 52, 51, 50, 49, 52, 51, 50, 49, 52, 51, 50, 49, 52, 51, 50, 49, 52, 51,
     50, 49, 52, 51, 50, 49, 52, 51, 50, 49, 52, 51, 50, 49, 52, 51, 50, 49, 52, 51, 50, 49},
    {56, 55, 54, 53, 56, 55, 54, 53, 56, 55, 54, 53, 56, 55, 54, 53, 56, 55, 54, 53, 56, 55, 54,
     53, 56, 55, 54, 53, 56, 55, 54, 53, 56, 55, 54, 53, 56, 55, 54, 53, 56, 55, 54, 53, 56, 55,
     54, 53, 56, 55, 54, 53, 56, 55, 54, 53, 56, 55, 54, 53, 56, 55, 54, 53, 56, 55, 54, 53},
    {60, 59, 58, 57, 60, 59, 58, 57, 60, 59, 58, 57, 60, 59, 58, 57, 60, 59, 58, 57, 60, 59, 58,
     57, 60, 59, 58, 57, 60, 59, 58, 57, 60, 59, 58, 57, 60, 59, 58, 57, 60, 59, 58, 57, 60, 59,
     58, 57, 60, 59, 58, 57, 60, 59, 58, 57, 60, 59, 58, 57, 60, 59, 58, 57, 60, 59, 58, 57},
    {64, 63, 62, 61, 64, 63, 62, 61, 64, 63, 62, 61, 64, 63, 62, 61, 64, 63, 62, 61, 64, 63, 62,
     61, 64, 63, 62, 61, 64, 63, 62, 61, 64, 63, 62, 61, 64, 63, 62, 61, 64, 63, 62, 61, 64, 63,
     62, 61, 64, 63, 62, 61, 64, 63, 62, 61, 64, 63, 62, 61, 64, 63, 62, 61, 64, 63, 62, 61},
    {68, 67, 66, 65, 68, 67, 66, 65, 68, 67, 66, 65, 68, 67, 66, 65, 68, 67, 66, 65, 68, 67, 66,
     65, 68, 67, 66, 65, 68, 67, 66, 65, 68, 67, 66, 65, 68, 67, 66, 65, 68, 67, 66, 65, 68, 67,
     66, 65, 68, 67, 66, 65, 68, 67, 66, 65, 68, 67, 66, 65, 68, 67, 66, 65, 68, 67, 66, 65},
    {72, 71, 70, 69, 72, 71, 70, 69, 72, 71, 70, 69, 72, 71, 70, 69, 72, 71, 70, 69, 72, 71, 70,
     69, 72, 71, 70, 69, 72, 71, 70, 69, 72, 71, 70, 69, 72, 71, 70, 69, 72, 71, 70, 69, 72, 71,
     70, 69, 72, 71, 70, 69, 72, 71, 70, 69, 72, 71, 70, 69, 72, 71, 70, 69, 72, 71, 70, 69}};
const int ietaEB[36][68] = {{1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17},
                            {1,  1,  1,  1,  2,  2,  2,  2,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,  6,  6,  6,
                             6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9,  10, 10, 10, 10, 11, 11, 11, 11, 12, 12,
                             12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17}};
const int ixx[18][41][3] = {
    {{19, 0, 0}, {20, 0, 0}, {14, 0, 0}, {15, 0, 0}, {16, 0, 0}, {17, 0, 0}, {18, 0, 0}, {19, 0, 0}, {20, 0, 0},
     {13, 0, 0}, {14, 0, 0}, {15, 0, 0}, {16, 0, 0}, {17, 0, 0}, {18, 0, 0}, {19, 0, 0}, {20, 0, 0}, {14, 0, 0},
     {15, 0, 0}, {16, 0, 0}, {17, 0, 0}, {18, 0, 0}, {19, 0, 0}, {20, 0, 0}, {15, 0, 0}, {16, 0, 0}, {17, 0, 0},
     {18, 0, 0}, {19, 0, 0}, {19, 0, 0}, {17, 0, 0}, {18, 0, 0}, {19, 0, 0}, {20, 0, 0}, {0, 0, 0},  {0, 0, 0},
     {0, 0, 0},  {0, 0, 0},  {0, 0, 0},  {0, 0, 0},  {0, 0, 0}},
    {{16, 0, 0}, {17, 0, 0}, {18, 17, 0}, {16, 0, 0}, {17, 0, 0}, {18, 0, 0}, {15, 0, 0},   {16, 0, 0}, {17, 0, 0},
     {18, 0, 0}, {19, 0, 0}, {14, 0, 0},  {15, 0, 0}, {16, 0, 0}, {17, 0, 0}, {18, 0, 0},   {19, 0, 0}, {13, 0, 0},
     {14, 0, 0}, {15, 0, 0}, {16, 0, 0},  {17, 0, 0}, {18, 0, 0}, {19, 0, 0}, {12, 20, 19}, {13, 0, 0}, {14, 0, 0},
     {15, 0, 0}, {16, 0, 0}, {17, 0, 0},  {18, 0, 0}, {13, 0, 0}, {0, 0, 0},  {0, 0, 0},    {0, 0, 0},  {0, 0, 0},
     {0, 0, 0},  {0, 0, 0},  {0, 0, 0},   {0, 0, 0},  {0, 0, 0}},
    {{11, 0, 0}, {11, 0, 0}, {11, 0, 0},  {11, 0, 0}, {12, 0, 0}, {12, 0, 0}, {12, 0, 0}, {12, 0, 0}, {11, 0, 0},
     {11, 0, 0}, {11, 0, 0}, {11, 0, 0},  {12, 0, 0}, {12, 0, 0}, {12, 0, 0}, {12, 0, 0}, {13, 0, 0}, {14, 0, 0},
     {15, 0, 0}, {13, 0, 0}, {14, 0, 0},  {15, 0, 0}, {16, 0, 0}, {13, 0, 0}, {14, 0, 0}, {15, 0, 0}, {13, 0, 0},
     {14, 0, 0}, {15, 0, 0}, {16, 13, 0}, {13, 0, 0}, {14, 0, 0}, {13, 0, 0}, {0, 0, 0},  {0, 0, 0},  {0, 0, 0},
     {0, 0, 0},  {0, 0, 0},  {0, 0, 0},   {0, 0, 0},  {0, 0, 0}},
    {{10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {9, 0, 0}, {9, 0, 0}, {9, 0, 0}, {9, 0, 0}, {10, 0, 0},
     {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {9, 0, 0},  {9, 0, 0}, {9, 0, 0}, {9, 0, 0}, {8, 0, 0}, {7, 0, 0},
     {6, 0, 0},  {8, 0, 0},  {7, 0, 0},  {6, 0, 0},  {5, 0, 0}, {8, 0, 0}, {7, 0, 0}, {6, 0, 0}, {8, 0, 0},
     {7, 0, 0},  {6, 0, 0},  {5, 8, 0},  {8, 0, 0},  {7, 0, 0}, {8, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0},
     {0, 0, 0},  {0, 0, 0},  {0, 0, 0},  {0, 0, 0},  {0, 0, 0}},
    {{5, 0, 0}, {4, 0, 0}, {3, 4, 0}, {5, 0, 0}, {4, 0, 0}, {3, 0, 0}, {6, 0, 0}, {5, 0, 0}, {4, 0, 0},
     {3, 0, 0}, {2, 0, 0}, {7, 0, 0}, {6, 0, 0}, {5, 0, 0}, {4, 0, 0}, {3, 0, 0}, {2, 0, 0}, {8, 0, 0},
     {7, 0, 0}, {6, 0, 0}, {5, 0, 0}, {4, 0, 0}, {3, 0, 0}, {2, 0, 0}, {9, 1, 2}, {8, 0, 0}, {7, 0, 0},
     {6, 0, 0}, {5, 0, 0}, {4, 0, 0}, {3, 0, 0}, {8, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0},
     {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}},
    {{2, 0, 0}, {1, 0, 0}, {7, 0, 0}, {6, 0, 0}, {5, 0, 0}, {4, 0, 0}, {3, 0, 0}, {2, 0, 0}, {1, 0, 0},
     {8, 0, 0}, {7, 0, 0}, {6, 0, 0}, {5, 0, 0}, {4, 0, 0}, {3, 0, 0}, {2, 0, 0}, {1, 0, 0}, {7, 0, 0},
     {6, 0, 0}, {5, 0, 0}, {4, 0, 0}, {3, 0, 0}, {2, 0, 0}, {1, 0, 0}, {6, 0, 0}, {5, 0, 0}, {4, 0, 0},
     {3, 0, 0}, {2, 0, 0}, {2, 0, 0}, {4, 0, 0}, {3, 0, 0}, {2, 0, 0}, {1, 0, 0}, {0, 0, 0}, {0, 0, 0},
     {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}},
    {{8, 0, 0}, {8, 0, 0}, {7, 0, 0}, {8, 0, 0}, {7, 0, 0}, {6, 0, 0}, {5, 0, 0}, {8, 0, 0}, {7, 0, 0},
     {6, 0, 0}, {5, 0, 0}, {4, 0, 0}, {3, 0, 0}, {3, 2, 9}, {8, 0, 0}, {7, 0, 0}, {6, 0, 0}, {5, 0, 0},
     {4, 0, 0}, {3, 0, 0}, {5, 4, 0}, {8, 0, 0}, {7, 0, 0}, {6, 0, 0}, {5, 0, 0}, {4, 0, 0}, {5, 0, 0},
     {7, 0, 0}, {7, 0, 0}, {6, 0, 0}, {6, 0, 0}, {9, 0, 0}, {9, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0},
     {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}},
    {{8, 0, 0},  {8, 0, 0},  {8, 0, 0},  {9, 0, 0},  {9, 0, 0},  {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0},
     {9, 0, 0},  {9, 0, 0},  {9, 0, 0},  {9, 0, 0},  {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {0, 0, 0},
     {0, 0, 0},  {0, 0, 0},  {0, 0, 0},  {0, 0, 0},  {0, 0, 0},  {0, 0, 0},  {13, 0, 0}, {13, 0, 0}, {13, 0, 0},
     {12, 0, 0}, {12, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0}, {12, 0, 0}, {12, 0, 0}, {12, 0, 0},
     {12, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0}},
    {{13, 0, 0}, {13, 0, 0}, {14, 0, 0},  {13, 0, 0}, {14, 0, 0},   {15, 0, 0}, {16, 0, 0}, {13, 0, 0}, {14, 0, 0},
     {15, 0, 0}, {16, 0, 0}, {17, 0, 0},  {18, 0, 0}, {18, 19, 12}, {13, 0, 0}, {14, 0, 0}, {15, 0, 0}, {16, 0, 0},
     {17, 0, 0}, {18, 0, 0}, {16, 17, 0}, {13, 0, 0}, {14, 0, 0},   {15, 0, 0}, {16, 0, 0}, {17, 0, 0}, {16, 0, 0},
     {14, 0, 0}, {14, 0, 0}, {15, 0, 0},  {15, 0, 0}, {12, 0, 0},   {12, 0, 0}, {0, 0, 0},  {0, 0, 0},  {0, 0, 0},
     {0, 0, 0},  {0, 0, 0},  {0, 0, 0},   {0, 0, 0},  {0, 0, 0}},
    {{19, 0, 0}, {20, 0, 0}, {14, 0, 0}, {15, 0, 0}, {16, 0, 0}, {17, 0, 0}, {18, 0, 0}, {19, 0, 0}, {20, 0, 0},
     {13, 0, 0}, {14, 0, 0}, {15, 0, 0}, {16, 0, 0}, {17, 0, 0}, {18, 0, 0}, {19, 0, 0}, {20, 0, 0}, {14, 0, 0},
     {15, 0, 0}, {16, 0, 0}, {17, 0, 0}, {18, 0, 0}, {19, 0, 0}, {20, 0, 0}, {15, 0, 0}, {16, 0, 0}, {17, 0, 0},
     {18, 0, 0}, {19, 0, 0}, {19, 0, 0}, {17, 0, 0}, {18, 0, 0}, {19, 0, 0}, {20, 0, 0}, {0, 0, 0},  {0, 0, 0},
     {0, 0, 0},  {0, 0, 0},  {0, 0, 0},  {0, 0, 0},  {0, 0, 0}},
    {{16, 0, 0}, {17, 0, 0}, {18, 17, 0}, {16, 0, 0}, {17, 0, 0}, {18, 0, 0}, {15, 0, 0},   {16, 0, 0}, {17, 0, 0},
     {18, 0, 0}, {19, 0, 0}, {14, 0, 0},  {15, 0, 0}, {16, 0, 0}, {17, 0, 0}, {18, 0, 0},   {19, 0, 0}, {13, 0, 0},
     {14, 0, 0}, {15, 0, 0}, {16, 0, 0},  {17, 0, 0}, {18, 0, 0}, {19, 0, 0}, {12, 20, 19}, {13, 0, 0}, {14, 0, 0},
     {15, 0, 0}, {16, 0, 0}, {17, 0, 0},  {18, 0, 0}, {13, 0, 0}, {0, 0, 0},  {0, 0, 0},    {0, 0, 0},  {0, 0, 0},
     {0, 0, 0},  {0, 0, 0},  {0, 0, 0},   {0, 0, 0},  {0, 0, 0}},
    {{11, 0, 0}, {11, 0, 0}, {11, 0, 0},  {11, 0, 0}, {12, 0, 0}, {12, 0, 0}, {12, 0, 0}, {12, 0, 0}, {11, 0, 0},
     {11, 0, 0}, {11, 0, 0}, {11, 0, 0},  {12, 0, 0}, {12, 0, 0}, {12, 0, 0}, {12, 0, 0}, {13, 0, 0}, {14, 0, 0},
     {15, 0, 0}, {13, 0, 0}, {14, 0, 0},  {15, 0, 0}, {16, 0, 0}, {13, 0, 0}, {14, 0, 0}, {15, 0, 0}, {13, 0, 0},
     {14, 0, 0}, {15, 0, 0}, {16, 13, 0}, {13, 0, 0}, {14, 0, 0}, {13, 0, 0}, {0, 0, 0},  {0, 0, 0},  {0, 0, 0},
     {0, 0, 0},  {0, 0, 0},  {0, 0, 0},   {0, 0, 0},  {0, 0, 0}},
    {{10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {9, 0, 0}, {9, 0, 0}, {9, 0, 0}, {9, 0, 0}, {10, 0, 0},
     {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {9, 0, 0},  {9, 0, 0}, {9, 0, 0}, {9, 0, 0}, {8, 0, 0}, {7, 0, 0},
     {6, 0, 0},  {8, 0, 0},  {7, 0, 0},  {6, 0, 0},  {5, 0, 0}, {8, 0, 0}, {7, 0, 0}, {6, 0, 0}, {8, 0, 0},
     {7, 0, 0},  {6, 0, 0},  {5, 8, 0},  {8, 0, 0},  {7, 0, 0}, {8, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0},
     {0, 0, 0},  {0, 0, 0},  {0, 0, 0},  {0, 0, 0},  {0, 0, 0}},
    {{5, 0, 0}, {4, 0, 0}, {3, 4, 0}, {5, 0, 0}, {4, 0, 0}, {3, 0, 0}, {6, 0, 0}, {5, 0, 0}, {4, 0, 0},
     {3, 0, 0}, {2, 0, 0}, {7, 0, 0}, {6, 0, 0}, {5, 0, 0}, {4, 0, 0}, {3, 0, 0}, {2, 0, 0}, {8, 0, 0},
     {7, 0, 0}, {6, 0, 0}, {5, 0, 0}, {4, 0, 0}, {3, 0, 0}, {2, 0, 0}, {9, 1, 2}, {8, 0, 0}, {7, 0, 0},
     {6, 0, 0}, {5, 0, 0}, {4, 0, 0}, {3, 0, 0}, {8, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0},
     {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}},
    {{2, 0, 0}, {1, 0, 0}, {7, 0, 0}, {6, 0, 0}, {5, 0, 0}, {4, 0, 0}, {3, 0, 0}, {2, 0, 0}, {1, 0, 0},
     {8, 0, 0}, {7, 0, 0}, {6, 0, 0}, {5, 0, 0}, {4, 0, 0}, {3, 0, 0}, {2, 0, 0}, {1, 0, 0}, {7, 0, 0},
     {6, 0, 0}, {5, 0, 0}, {4, 0, 0}, {3, 0, 0}, {2, 0, 0}, {1, 0, 0}, {6, 0, 0}, {5, 0, 0}, {4, 0, 0},
     {3, 0, 0}, {2, 0, 0}, {2, 0, 0}, {4, 0, 0}, {3, 0, 0}, {2, 0, 0}, {1, 0, 0}, {0, 0, 0}, {0, 0, 0},
     {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}},
    {{8, 0, 0}, {8, 0, 0}, {7, 0, 0}, {8, 0, 0}, {7, 0, 0}, {6, 0, 0}, {5, 0, 0}, {8, 0, 0}, {7, 0, 0},
     {6, 0, 0}, {5, 0, 0}, {4, 0, 0}, {3, 0, 0}, {3, 2, 9}, {8, 0, 0}, {7, 0, 0}, {6, 0, 0}, {5, 0, 0},
     {4, 0, 0}, {3, 0, 0}, {5, 4, 0}, {8, 0, 0}, {7, 0, 0}, {6, 0, 0}, {5, 0, 0}, {4, 0, 0}, {5, 0, 0},
     {7, 0, 0}, {7, 0, 0}, {6, 0, 0}, {6, 0, 0}, {9, 0, 0}, {9, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0},
     {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}},
    {{13, 0, 0}, {13, 0, 0}, {13, 0, 0}, {12, 0, 0}, {12, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0},
     {12, 0, 0}, {12, 0, 0}, {12, 0, 0}, {12, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0}, {0, 0, 0},
     {0, 0, 0},  {0, 0, 0},  {0, 0, 0},  {0, 0, 0},  {0, 0, 0},  {0, 0, 0},  {8, 0, 0},  {8, 0, 0},  {8, 0, 0},
     {9, 0, 0},  {9, 0, 0},  {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {9, 0, 0},  {9, 0, 0},  {9, 0, 0},
     {9, 0, 0},  {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}},
    {{13, 0, 0}, {13, 0, 0}, {14, 0, 0},  {13, 0, 0}, {14, 0, 0},   {15, 0, 0}, {16, 0, 0}, {13, 0, 0}, {14, 0, 0},
     {15, 0, 0}, {16, 0, 0}, {17, 0, 0},  {18, 0, 0}, {18, 19, 12}, {13, 0, 0}, {14, 0, 0}, {15, 0, 0}, {16, 0, 0},
     {17, 0, 0}, {18, 0, 0}, {16, 17, 0}, {13, 0, 0}, {14, 0, 0},   {15, 0, 0}, {16, 0, 0}, {17, 0, 0}, {16, 0, 0},
     {14, 0, 0}, {14, 0, 0}, {15, 0, 0},  {15, 0, 0}, {12, 0, 0},   {12, 0, 0}, {0, 0, 0},  {0, 0, 0},  {0, 0, 0},
     {0, 0, 0},  {0, 0, 0},  {0, 0, 0},   {0, 0, 0},  {0, 0, 0}}};
const int iyy[18][41][3] = {
    {{12, 0, 0}, {12, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0},
     {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {9, 0, 0},
     {9, 0, 0},  {9, 0, 0},  {9, 0, 0},  {9, 0, 0},  {9, 0, 0},  {9, 0, 0},  {8, 0, 0},  {8, 0, 0},  {8, 0, 0},
     {8, 0, 0},  {8, 0, 0},  {7, 0, 0},  {7, 0, 0},  {7, 0, 0},  {6, 0, 0},  {8, 0, 0},  {0, 0, 0},  {0, 0, 0},
     {0, 0, 0},  {0, 0, 0},  {0, 0, 0},  {0, 0, 0},  {0, 0, 0}},
    {{17, 0, 0}, {17, 0, 0}, {17, 18, 0}, {16, 0, 0}, {16, 0, 0}, {16, 0, 0}, {15, 0, 0},   {15, 0, 0}, {15, 0, 0},
     {15, 0, 0}, {15, 0, 0}, {14, 0, 0},  {14, 0, 0}, {14, 0, 0}, {14, 0, 0}, {14, 0, 0},   {14, 0, 0}, {13, 0, 0},
     {13, 0, 0}, {13, 0, 0}, {13, 0, 0},  {13, 0, 0}, {13, 0, 0}, {13, 0, 0}, {12, 13, 16}, {12, 0, 0}, {12, 0, 0},
     {12, 0, 0}, {12, 0, 0}, {12, 0, 0},  {12, 0, 0}, {11, 0, 0}, {0, 0, 0},  {0, 0, 0},    {0, 0, 0},  {0, 0, 0},
     {0, 0, 0},  {0, 0, 0},  {0, 0, 0},   {0, 0, 0},  {0, 0, 0}},
    {{20, 0, 0}, {19, 0, 0}, {18, 0, 0},  {17, 0, 0}, {20, 0, 0}, {19, 0, 0}, {18, 0, 0}, {17, 0, 0}, {16, 0, 0},
     {15, 0, 0}, {14, 0, 0}, {13, 0, 0},  {16, 0, 0}, {15, 0, 0}, {14, 0, 0}, {13, 0, 0}, {19, 0, 0}, {19, 0, 0},
     {19, 0, 0}, {18, 0, 0}, {18, 0, 0},  {18, 0, 0}, {18, 0, 0}, {17, 0, 0}, {17, 0, 0}, {17, 0, 0}, {16, 0, 0},
     {16, 0, 0}, {16, 0, 0}, {19, 20, 0}, {15, 0, 0}, {15, 0, 0}, {14, 0, 0}, {0, 0, 0},  {0, 0, 0},  {0, 0, 0},
     {0, 0, 0},  {0, 0, 0},  {0, 0, 0},   {0, 0, 0},  {0, 0, 0}},
    {{20, 0, 0}, {19, 0, 0}, {18, 0, 0},  {17, 0, 0}, {20, 0, 0}, {19, 0, 0}, {18, 0, 0}, {17, 0, 0}, {16, 0, 0},
     {15, 0, 0}, {14, 0, 0}, {13, 0, 0},  {16, 0, 0}, {15, 0, 0}, {14, 0, 0}, {13, 0, 0}, {19, 0, 0}, {19, 0, 0},
     {19, 0, 0}, {18, 0, 0}, {18, 0, 0},  {18, 0, 0}, {18, 0, 0}, {17, 0, 0}, {17, 0, 0}, {17, 0, 0}, {16, 0, 0},
     {16, 0, 0}, {16, 0, 0}, {19, 20, 0}, {15, 0, 0}, {15, 0, 0}, {14, 0, 0}, {0, 0, 0},  {0, 0, 0},  {0, 0, 0},
     {0, 0, 0},  {0, 0, 0},  {0, 0, 0},   {0, 0, 0},  {0, 0, 0}},
    {{17, 0, 0}, {17, 0, 0}, {17, 18, 0}, {16, 0, 0}, {16, 0, 0}, {16, 0, 0}, {15, 0, 0},   {15, 0, 0}, {15, 0, 0},
     {15, 0, 0}, {15, 0, 0}, {14, 0, 0},  {14, 0, 0}, {14, 0, 0}, {14, 0, 0}, {14, 0, 0},   {14, 0, 0}, {13, 0, 0},
     {13, 0, 0}, {13, 0, 0}, {13, 0, 0},  {13, 0, 0}, {13, 0, 0}, {13, 0, 0}, {12, 13, 16}, {12, 0, 0}, {12, 0, 0},
     {12, 0, 0}, {12, 0, 0}, {12, 0, 0},  {12, 0, 0}, {11, 0, 0}, {0, 0, 0},  {0, 0, 0},    {0, 0, 0},  {0, 0, 0},
     {0, 0, 0},  {0, 0, 0},  {0, 0, 0},   {0, 0, 0},  {0, 0, 0}},
    {{12, 0, 0}, {12, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0},
     {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {9, 0, 0},
     {9, 0, 0},  {9, 0, 0},  {9, 0, 0},  {9, 0, 0},  {9, 0, 0},  {9, 0, 0},  {8, 0, 0},  {8, 0, 0},  {8, 0, 0},
     {8, 0, 0},  {8, 0, 0},  {7, 0, 0},  {7, 0, 0},  {7, 0, 0},  {6, 0, 0},  {8, 0, 0},  {0, 0, 0},  {0, 0, 0},
     {0, 0, 0},  {0, 0, 0},  {0, 0, 0},  {0, 0, 0},  {0, 0, 0}},
    {{9, 0, 0}, {8, 0, 0}, {8, 0, 0}, {7, 0, 0}, {7, 0, 0}, {7, 0, 0}, {7, 0, 0}, {6, 0, 0}, {6, 0, 0},
     {6, 0, 0}, {6, 0, 0}, {6, 0, 0}, {6, 0, 0}, {4, 5, 9}, {5, 0, 0}, {5, 0, 0}, {5, 0, 0}, {5, 0, 0},
     {5, 0, 0}, {5, 0, 0}, {2, 3, 0}, {4, 0, 0}, {4, 0, 0}, {4, 0, 0}, {4, 0, 0}, {4, 0, 0}, {3, 0, 0},
     {3, 0, 0}, {2, 0, 0}, {3, 0, 0}, {2, 0, 0}, {8, 0, 0}, {7, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0},
     {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}},
    {{3, 0, 0}, {2, 0, 0}, {1, 0, 0}, {6, 0, 0}, {5, 0, 0}, {8, 0, 0}, {7, 0, 0}, {6, 0, 0}, {5, 0, 0},
     {4, 0, 0}, {3, 0, 0}, {2, 0, 0}, {1, 0, 0}, {4, 0, 0}, {3, 0, 0}, {2, 0, 0}, {1, 0, 0}, {0, 0, 0},
     {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {3, 0, 0}, {2, 0, 0}, {1, 0, 0},
     {6, 0, 0}, {5, 0, 0}, {8, 0, 0}, {7, 0, 0}, {6, 0, 0}, {5, 0, 0}, {4, 0, 0}, {3, 0, 0}, {2, 0, 0},
     {1, 0, 0}, {4, 0, 0}, {3, 0, 0}, {2, 0, 0}, {1, 0, 0}},
    {{9, 0, 0}, {8, 0, 0}, {8, 0, 0}, {7, 0, 0}, {7, 0, 0}, {7, 0, 0}, {7, 0, 0}, {6, 0, 0}, {6, 0, 0},
     {6, 0, 0}, {6, 0, 0}, {6, 0, 0}, {6, 0, 0}, {4, 5, 9}, {5, 0, 0}, {5, 0, 0}, {5, 0, 0}, {5, 0, 0},
     {5, 0, 0}, {5, 0, 0}, {2, 3, 0}, {4, 0, 0}, {4, 0, 0}, {4, 0, 0}, {4, 0, 0}, {4, 0, 0}, {3, 0, 0},
     {3, 0, 0}, {2, 0, 0}, {3, 0, 0}, {2, 0, 0}, {8, 0, 0}, {7, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0},
     {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}},
    {{12, 0, 0}, {12, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0},
     {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {9, 0, 0},
     {9, 0, 0},  {9, 0, 0},  {9, 0, 0},  {9, 0, 0},  {9, 0, 0},  {9, 0, 0},  {8, 0, 0},  {8, 0, 0},  {8, 0, 0},
     {8, 0, 0},  {8, 0, 0},  {7, 0, 0},  {7, 0, 0},  {7, 0, 0},  {6, 0, 0},  {8, 0, 0},  {0, 0, 0},  {0, 0, 0},
     {0, 0, 0},  {0, 0, 0},  {0, 0, 0},  {0, 0, 0},  {0, 0, 0}},
    {{17, 0, 0}, {17, 0, 0}, {17, 18, 0}, {16, 0, 0}, {16, 0, 0}, {16, 0, 0}, {15, 0, 0},   {15, 0, 0}, {15, 0, 0},
     {15, 0, 0}, {15, 0, 0}, {14, 0, 0},  {14, 0, 0}, {14, 0, 0}, {14, 0, 0}, {14, 0, 0},   {14, 0, 0}, {13, 0, 0},
     {13, 0, 0}, {13, 0, 0}, {13, 0, 0},  {13, 0, 0}, {13, 0, 0}, {13, 0, 0}, {12, 13, 16}, {12, 0, 0}, {12, 0, 0},
     {12, 0, 0}, {12, 0, 0}, {12, 0, 0},  {12, 0, 0}, {11, 0, 0}, {0, 0, 0},  {0, 0, 0},    {0, 0, 0},  {0, 0, 0},
     {0, 0, 0},  {0, 0, 0},  {0, 0, 0},   {0, 0, 0},  {0, 0, 0}},
    {{20, 0, 0}, {19, 0, 0}, {18, 0, 0},  {17, 0, 0}, {20, 0, 0}, {19, 0, 0}, {18, 0, 0}, {17, 0, 0}, {16, 0, 0},
     {15, 0, 0}, {14, 0, 0}, {13, 0, 0},  {16, 0, 0}, {15, 0, 0}, {14, 0, 0}, {13, 0, 0}, {19, 0, 0}, {19, 0, 0},
     {19, 0, 0}, {18, 0, 0}, {18, 0, 0},  {18, 0, 0}, {18, 0, 0}, {17, 0, 0}, {17, 0, 0}, {17, 0, 0}, {16, 0, 0},
     {16, 0, 0}, {16, 0, 0}, {19, 20, 0}, {15, 0, 0}, {15, 0, 0}, {14, 0, 0}, {0, 0, 0},  {0, 0, 0},  {0, 0, 0},
     {0, 0, 0},  {0, 0, 0},  {0, 0, 0},   {0, 0, 0},  {0, 0, 0}},
    {{20, 0, 0}, {19, 0, 0}, {18, 0, 0},  {17, 0, 0}, {20, 0, 0}, {19, 0, 0}, {18, 0, 0}, {17, 0, 0}, {16, 0, 0},
     {15, 0, 0}, {14, 0, 0}, {13, 0, 0},  {16, 0, 0}, {15, 0, 0}, {14, 0, 0}, {13, 0, 0}, {19, 0, 0}, {19, 0, 0},
     {19, 0, 0}, {18, 0, 0}, {18, 0, 0},  {18, 0, 0}, {18, 0, 0}, {17, 0, 0}, {17, 0, 0}, {17, 0, 0}, {16, 0, 0},
     {16, 0, 0}, {16, 0, 0}, {19, 20, 0}, {15, 0, 0}, {15, 0, 0}, {14, 0, 0}, {0, 0, 0},  {0, 0, 0},  {0, 0, 0},
     {0, 0, 0},  {0, 0, 0},  {0, 0, 0},   {0, 0, 0},  {0, 0, 0}},
    {{17, 0, 0}, {17, 0, 0}, {17, 18, 0}, {16, 0, 0}, {16, 0, 0}, {16, 0, 0}, {15, 0, 0},   {15, 0, 0}, {15, 0, 0},
     {15, 0, 0}, {15, 0, 0}, {14, 0, 0},  {14, 0, 0}, {14, 0, 0}, {14, 0, 0}, {14, 0, 0},   {14, 0, 0}, {13, 0, 0},
     {13, 0, 0}, {13, 0, 0}, {13, 0, 0},  {13, 0, 0}, {13, 0, 0}, {13, 0, 0}, {12, 13, 16}, {12, 0, 0}, {12, 0, 0},
     {12, 0, 0}, {12, 0, 0}, {12, 0, 0},  {12, 0, 0}, {11, 0, 0}, {0, 0, 0},  {0, 0, 0},    {0, 0, 0},  {0, 0, 0},
     {0, 0, 0},  {0, 0, 0},  {0, 0, 0},   {0, 0, 0},  {0, 0, 0}},
    {{12, 0, 0}, {12, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0}, {11, 0, 0},
     {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {10, 0, 0}, {9, 0, 0},
     {9, 0, 0},  {9, 0, 0},  {9, 0, 0},  {9, 0, 0},  {9, 0, 0},  {9, 0, 0},  {8, 0, 0},  {8, 0, 0},  {8, 0, 0},
     {8, 0, 0},  {8, 0, 0},  {7, 0, 0},  {7, 0, 0},  {7, 0, 0},  {6, 0, 0},  {8, 0, 0},  {0, 0, 0},  {0, 0, 0},
     {0, 0, 0},  {0, 0, 0},  {0, 0, 0},  {0, 0, 0},  {0, 0, 0}},
    {{9, 0, 0}, {8, 0, 0}, {8, 0, 0}, {7, 0, 0}, {7, 0, 0}, {7, 0, 0}, {7, 0, 0}, {6, 0, 0}, {6, 0, 0},
     {6, 0, 0}, {6, 0, 0}, {6, 0, 0}, {6, 0, 0}, {4, 5, 9}, {5, 0, 0}, {5, 0, 0}, {5, 0, 0}, {5, 0, 0},
     {5, 0, 0}, {5, 0, 0}, {2, 3, 0}, {4, 0, 0}, {4, 0, 0}, {4, 0, 0}, {4, 0, 0}, {4, 0, 0}, {3, 0, 0},
     {3, 0, 0}, {2, 0, 0}, {3, 0, 0}, {2, 0, 0}, {8, 0, 0}, {7, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0},
     {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}},
    {{3, 0, 0}, {2, 0, 0}, {1, 0, 0}, {6, 0, 0}, {5, 0, 0}, {8, 0, 0}, {7, 0, 0}, {6, 0, 0}, {5, 0, 0},
     {4, 0, 0}, {3, 0, 0}, {2, 0, 0}, {1, 0, 0}, {4, 0, 0}, {3, 0, 0}, {2, 0, 0}, {1, 0, 0}, {0, 0, 0},
     {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {3, 0, 0}, {2, 0, 0}, {1, 0, 0},
     {6, 0, 0}, {5, 0, 0}, {8, 0, 0}, {7, 0, 0}, {6, 0, 0}, {5, 0, 0}, {4, 0, 0}, {3, 0, 0}, {2, 0, 0},
     {1, 0, 0}, {4, 0, 0}, {3, 0, 0}, {2, 0, 0}, {1, 0, 0}},
    {{9, 0, 0}, {8, 0, 0}, {8, 0, 0}, {7, 0, 0}, {7, 0, 0}, {7, 0, 0}, {7, 0, 0}, {6, 0, 0}, {6, 0, 0},
     {6, 0, 0}, {6, 0, 0}, {6, 0, 0}, {6, 0, 0}, {4, 5, 9}, {5, 0, 0}, {5, 0, 0}, {5, 0, 0}, {5, 0, 0},
     {5, 0, 0}, {5, 0, 0}, {2, 3, 0}, {4, 0, 0}, {4, 0, 0}, {4, 0, 0}, {4, 0, 0}, {4, 0, 0}, {3, 0, 0},
     {3, 0, 0}, {2, 0, 0}, {3, 0, 0}, {2, 0, 0}, {8, 0, 0}, {7, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0},
     {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}}};

popcon::EcalDAQHandler::EcalDAQHandler(const edm::ParameterSet& ps)
    : m_name(ps.getUntrackedParameter<std::string>("name", "EcalDAQHandler")) {
  std::cout << "EcalDAQ Source handler constructor\n" << std::endl;
  m_firstRun = (unsigned long)atoi(ps.getParameter<std::string>("firstRun").c_str());
  m_lastRun = (unsigned long)atoi(ps.getParameter<std::string>("lastRun").c_str());
  m_sid = ps.getParameter<std::string>("OnlineDBSID");
  m_user = ps.getParameter<std::string>("OnlineDBUser");
  m_pass = ps.getParameter<std::string>("OnlineDBPassword");
  m_location = ps.getParameter<std::string>("location");
  m_runtype = ps.getParameter<std::string>("runtype");
  m_gentag = ps.getParameter<std::string>("gentag");
  m_debug = ps.getParameter<bool>("debug");

  std::cout << m_sid << "/" << m_user << std::endl;
}

popcon::EcalDAQHandler::~EcalDAQHandler() {}

uint16_t popcon::EcalDAQHandler::OffDBStatus(uint16_t dbStatus, int pos) {
  uint16_t hv_off_dbstatus = (dbStatus & (1 << pos));
  if (hv_off_dbstatus > 0)
    hv_off_dbstatus = 1;
  return hv_off_dbstatus;
}

void popcon::EcalDAQHandler::getNewObjects() {
  if (m_debug)
    std::cout << "------- Ecal DAQ - > getNewObjects\n";

  std::ostringstream ss;
  ss << "ECAL ";

  unsigned long long max_since = 1;

  // this is the last inserted run
  max_since = tagInfo().lastInterval.since;

  // this is the last object in the DB
  std::cout << " max_since : " << max_since << std::endl;

  uint16_t oldEBStatus[72][17][2];
  uint16_t newEBStatus[72][17][2];
  uint16_t oldEEStatus[20][20][2];
  uint16_t newEEStatus[20][20][2];

  // we copy the last valid record to a temporary object peds
  EcalDAQTowerStatus* daq_temp = new EcalDAQTowerStatus();

  if (tagInfo().size) {
    Ref daq_db = lastPayload();
    if (m_debug)
      std::cout << "retrieved last payload " << std::endl;

    // barrel
    int iz = -1;
    for (int k = 0; k < 2; k++) {
      if (k == 1)
        iz = 1;
      for (int iphi = 1; iphi < 73; iphi++) {
        for (int ieta = 1; ieta < 18; ieta++) {
          if (EcalTrigTowerDetId::validDetId(iz, EcalBarrel, ieta, iphi)) {
            EcalTrigTowerDetId ebid(iz, EcalBarrel, ieta, iphi);

            uint16_t dbStatus = 0;
            dbStatus = (daq_db->barrel(ebid.hashedIndex())).getStatusCode();
            oldEBStatus[iphi - 1][ieta - 1][k] = dbStatus;

            EcalDAQTowerStatus::const_iterator it = daq_db->find(ebid.rawId());
            if (it != daq_db->end()) {
            } else {
              std::cout << "*** error channel not found: eta/phi =" << ieta << "/" << iphi << std::endl;
            }
            daq_temp->setValue(ebid, dbStatus);
            if (m_debug && dbStatus != 0)
              std::cout << "barrel side " << k << " phi " << iphi << " eta " << ieta << " status " << dbStatus
                        << std::endl;
          }
        }  // end loop over ieta
      }    // end loop over iphi

      // endcap
      for (int ix = 1; ix < 21; ix++) {
        for (int iy = 1; iy < 21; iy++) {
          if (EcalScDetId::validDetId(ix, iy, iz)) {
            EcalScDetId eeid(ix, iy, iz);

            EcalDAQTowerStatus::const_iterator it = daq_db->find(eeid.rawId());

            uint16_t dbStatus = 0;
            if (it != daq_db->end()) {
              dbStatus = it->getStatusCode();
            }
            oldEEStatus[ix - 1][iy - 1][k] = dbStatus;
            daq_temp->setValue(eeid, dbStatus);
            if (m_debug && dbStatus != 0)
              std::cout << "endcap side " << k << " x " << ix << " y " << iy << " status " << dbStatus << std::endl;
          }
        }  // end loop over iy
      }    // end loop over ix
    }      // end loop over k (side)
  }        // check if there is already a payload
  else {
    if (m_debug)
      std::cout << " No db found : set default values " << std::endl;
    // barrel
    int iz = -1;
    for (int k = 0; k < 2; k++) {
      if (k == 1)
        iz = 1;
      for (int iphi = 1; iphi < 73; iphi++) {
        for (int ieta = 1; ieta < 18; ieta++) {
          if (EcalTrigTowerDetId::validDetId(iz, EcalBarrel, ieta, iphi)) {
            EcalTrigTowerDetId ebid(iz, EcalBarrel, ieta, iphi);

            uint16_t dbStatus = 0;
            oldEBStatus[iphi - 1][ieta - 1][k] = dbStatus;
            daq_temp->setValue(ebid, dbStatus);
          }  // valid Id
        }    // end loop over ieta
      }      // end loop over iphi
      // endcap
      for (int ix = 1; ix < 21; ix++) {
        for (int iy = 1; iy < 21; iy++) {
          if (EcalScDetId::validDetId(ix, iy, iz)) {
            EcalScDetId eeid(ix, iy, iz);
            uint16_t dbStatus = 0;
            oldEEStatus[ix - 1][iy - 1][k] = dbStatus;
            daq_temp->setValue(eeid, dbStatus);
          }  // valid Id
        }    // end loop over iy
      }      // end loop over ix
    }        // end loop over k (side)
  }          //  no payload set default values

  // now read the actual status from the online DB

  if (m_debug)
    std::cout << "Retrieving DAQ status from OMDS DB ... " << std::endl;
  econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
  if (m_debug)
    std::cout << "Connection done" << std::endl;

  if (!econn) {
    std::cout << " Problem with OMDS: connection parameters " << m_sid << "/" << m_user << "/" << m_pass << std::endl;
    throw cms::Exception("OMDS not available");
  }

  // code from EcalTPGBadTTHandler.cc
  LocationDef my_locdef;
  my_locdef.setLocation(m_location);

  RunTypeDef my_rundef;
  my_rundef.setRunType(m_runtype);

  RunTag my_runtag;
  my_runtag.setLocationDef(my_locdef);
  my_runtag.setRunTypeDef(my_rundef);
  my_runtag.setGeneralTag(m_gentag);

  // range of validity
  int min_run = 0;
  if (m_firstRun < (unsigned long)max_since) {
    min_run = (int)max_since + 1;  // we have to add 1 to the last transferred one
  } else {
    min_run = (int)m_firstRun;
  }
  int max_run = (int)m_lastRun;
  std::cout << "min_run " << min_run << " max_run " << max_run << std::endl;

  RunList my_list;
  my_list = econn->fetchRunListByLocation(my_runtag, min_run, max_run, my_locdef);

  std::vector<RunIOV> run_vec = my_list.getRuns();
  int num_runs = run_vec.size();

  std::cout << " number of runs is : " << num_runs << std::endl;

  unsigned long irun = 0;
  if (num_runs > 0) {
    //    char outfile[800];
    //sprintf(outfile,"BadChannelsEB_run%d.txt",min_run);
    //ofstream *daqFile;
    //daqFile = new ofstream(outfile,ios::out);

    //hlt    for(int kr = 0; kr < num_runs; kr++){
    for (int kr = num_runs - 1; kr < num_runs; kr++) {
      //    int krmax = std::min(num_runs, 1000);
      //    for(int kr = 0; kr < krmax; kr++){
      //hlt    if(m_to_transfer.size() < 20 ) {

      if (run_vec[kr].getRunTag().getGeneralTag() != "GLOBAL")
        continue;
      bool somediff = false;
      // initialize this run status to all OK
      irun = (unsigned long)run_vec[kr].getRunNumber();
      for (int k = 0; k < 2; k++) {
        for (int iphi = 0; iphi < 72; iphi++) {
          for (int ieta = 0; ieta < 17; ieta++) {
            newEBStatus[iphi][ieta][k] = 0;
          }
        }
        for (int ix = 0; ix < 20; ix++) {
          for (int iy = 0; iy < 20; iy++) {
            newEEStatus[ix][iy][k] = 0;
          }
        }
      }  // loop over side

      // these are the online conditions DB classes        readout FEDs
      std::map<EcalLogicID, RunDat> fed_dat;
      econn->fetchDataSet(&fed_dat, &run_vec[kr]);

      // these are the online conditions DB classes        unread FEs
      typedef std::map<EcalLogicID, RunDat>::const_iterator fedIter;
      //      EcalLogicID ecid_xt;
      RunDat rdat_fe;
      EcalLogicID idfed;

      std::map<EcalLogicID, RunFEConfigDat> feconfig;
      econn->fetchDataSet(&feconfig, &run_vec[kr]);

      if (fed_dat.empty() || feconfig.empty()) {
        std::cout << " run " << irun << " tag " << run_vec[kr].getRunTag().getGeneralTag() << " Run type "
                  << run_vec[kr].getRunTag().getRunTypeDef().getRunType()
                  << " feconfig and/or read FED size = 0, leaving..." << std::endl;
        continue;
      }

      if (fed_dat.size() != 54) {
        int SM[36] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        int Sect[2][9] = {{0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}};
        for (fedIter p = fed_dat.begin(); p != fed_dat.end(); p++) {
          idfed = p->first;
          int sm = 0;
          int z = 0;
          if (idfed.getLogicID() < 2000000000) {  // eb
            sm = idfed.getID1();
            //	    std::cout << " EB " << sm;
            SM[sm - 1] = 1;
          } else {  // ee
            z = idfed.getID1();
            sm = idfed.getID2();
            //	    std::cout << " EE " << z << " " << sm;
            int izz = z;
            if (z == -1)
              izz = 0;
            Sect[izz][sm - 1] = 1;
          }
        }  // loop over all FED
        //////////////////////////  test ////// do not keep
        /*	if(irun == 124089) {
	  Sect[0][2] = 0;
	  Sect[1][7] = 0;
	  } */
        //////////////////////////  test ////// do not keep
        // mark all TT/SC in the not read out FEDs
        for (int sm = 0; sm < 36; sm++)  // barrel
          if (SM[sm] != 1) {
            //std::cout << " missing EB " << sm + 1 << std::endl;
            if (sm < 18)
              for (int tt = 0; tt < 68; tt++)
                newEBStatus[iphiEB[sm][tt] - 1][ietaEB[sm][tt] - 1][0] = 1;
            else
              for (int tt = 0; tt < 68; tt++)
                newEBStatus[iphiEB[sm - 18][tt] - 1][ietaEB[sm - 18][tt] - 1][1] = 1;
          }
        for (int z = 0; z < 2; z++)  // endcaps
          for (int sec = 0; sec < 9; sec++)
            if (Sect[z][sec] != 1) {
              //std::cout << " missing EE " << z << " " << sec + 1 << std::endl;
              int sec18 = sec;
              if (z == 1)
                sec18 = sec + 9;
              for (int sc = 0; sc < 41; sc++) {
                if (ixx[sec18][sc][0] != 0) {
                  newEEStatus[ixx[sec18][sc][0] - 1][iyy[sec18][sc][0] - 1][z] = 1;
                  if (ixx[sec18][sc][1] != 0) {
                    newEEStatus[ixx[sec18][sc][1] - 1][iyy[sec18][sc][1] - 1][z] = 1;
                    if (ixx[sec18][sc][2] != 0) {
                      newEEStatus[ixx[sec18][sc][2] - 1][iyy[sec18][sc][2] - 1][z] = 1;
                    }
                  }
                }
              }  // loop over all possible SC in a sector
            }    // not read out sector
      }          // not all FED are read out

      typedef std::map<EcalLogicID, RunFEConfigDat>::const_iterator feConfIter;
      //      EcalLogicID ecid_xt;
      RunFEConfigDat rd_fe;

      int fe_conf_id = 0;
      for (feConfIter p = feconfig.begin(); p != feconfig.end(); p++) {
        //	ecid_xt = p->first;
        rd_fe = p->second;
        fe_conf_id = rd_fe.getConfigId();
      }

      // reading this configuration
      ODFEDAQConfig myconfig;
      myconfig.setId(fe_conf_id);
      econn->fetchConfigSet(&myconfig);

      // list of bad towers
      int myTT = myconfig.getBadTTId();
      ODBadTTInfo mybadTT;
      std::vector<ODBadTTDat> badTT_dat;
      unsigned NbadTT = 0;

      /*      if(myTT == 0) {
		std::cout << " myTT = 0" << std::endl;
		continue;
      }
      else {
      */
      if (myTT != 0) {
        mybadTT.setId(myTT);
        econn->fetchConfigSet(&mybadTT);

        econn->fetchConfigDataSet(&badTT_dat, &mybadTT);
        NbadTT = badTT_dat.size();
      }

      /*      std::cout << " run " << irun << " tag " << run_vec[kr].getRunTag().getGeneralTag()
	   << " Run type " << run_vec[kr].getRunTag().getRunTypeDef().getRunType()
	   << " number of read FEDs " << fed_dat.size()
	   << " number of bad FEs " << NbadTT
	   << " fe_conf_id " << fe_conf_id << std::endl;
      */

      //      *daqFile  << " run " << irun << " bad TT number " << NbadTT << "\n" << "Towers : ";
      for (size_t iTT = 0; iTT < NbadTT; iTT++) {
        int fed_id = badTT_dat[iTT].getFedId();
        int tt_id = badTT_dat[iTT].getTTId();
        //	int SM = badTT_dat[iTT].getSMId();  // SM always 0!
        int side = 0;  // EB/EE-
        //*daqFile << fed_id << "/" << tt_id << " ";
        //	std::cout << fed_id << "/" << tt_id << "/" << SM << "/" ;
        // std::cout << fed_id << "/" << tt_id << "/";
        if (fed_id >= 610 && fed_id <= 645) {  // barrel
          if (tt_id > 68) {
            //std::cout << " Problem in Fed " << fed_id << " TT " << tt_id << " Give up " << std::endl;
            continue;
          }
          if (fed_id > 627)
            side = +1;  // EB+
          if (m_debug)
            std::cout << " phi " << iphiEB[fed_id - 610][tt_id - 1] << " eta " << ietaEB[fed_id - 610][tt_id - 1]
                      << " side " << side << std::endl;
          newEBStatus[iphiEB[fed_id - 610][tt_id - 1] - 1][ietaEB[fed_id - 610][tt_id - 1] - 1][side] = 1;
        } else if (fed_id <= 609 || (fed_id >= 646 && fed_id <= 654)) {  // endcap
          if (tt_id > 41) {
            //std::cout << " Problem in Fed " << fed_id << " SC " << tt_id << " Give up " << std::endl;
            continue;
          }
          if (fed_id < 610) {  // EE-
            if (m_debug)
              std::cout << " x " << ixx[fed_id - 601][tt_id - 1][0] << " y " << iyy[fed_id - 601][tt_id - 1][0];
            newEEStatus[ixx[fed_id - 601][tt_id - 1][0] - 1][iyy[fed_id - 601][tt_id - 1][0] - 1][side] = 1;
            if (ixx[fed_id - 601][tt_id - 1][1] != 0) {  // partial SC
              if (m_debug)
                std::cout << " x2 " << ixx[fed_id - 601][tt_id - 1][1] << " y2 " << iyy[fed_id - 601][tt_id - 1][1];
              newEEStatus[ixx[fed_id - 601][tt_id - 1][1] - 1][iyy[fed_id - 601][tt_id - 1][1] - 1][side] = 1;
              if (ixx[fed_id - 601][tt_id - 1][2] != 0) {  // partial SC
                if (m_debug)
                  std::cout << " x3 " << ixx[fed_id - 601][tt_id - 1][2] << " y3 " << iyy[fed_id - 601][tt_id - 1][2];
                newEEStatus[ixx[fed_id - 601][tt_id - 1][2] - 1][iyy[fed_id - 601][tt_id - 1][2] - 1][side] = 1;
              }
            }
            if (m_debug)
              std::cout << " side " << side << std::endl;
          } else {  // EE+
            side = +1;
            if (m_debug)
              std::cout << " x " << ixx[fed_id - 637][tt_id - 1][0] << " y " << iyy[fed_id - 637][tt_id - 1][0];
            newEEStatus[ixx[fed_id - 637][tt_id - 1][0] - 1][iyy[fed_id - 637][tt_id - 1][0] - 1][side] = 1;
            if (ixx[fed_id - 637][tt_id - 1][1] != 0) {  // partial SC
              if (m_debug)
                std::cout << " x2 " << ixx[fed_id - 637][tt_id - 1][1] << " y2 " << iyy[fed_id - 637][tt_id - 1][1];
              newEEStatus[ixx[fed_id - 637][tt_id - 1][1] - 1][iyy[fed_id - 637][tt_id - 1][1] - 1][side] = 1;
              if (ixx[fed_id - 637][tt_id - 1][2] != 0) {  // partial SC
                if (m_debug)
                  std::cout << " x3 " << ixx[fed_id - 637][tt_id - 1][2] << " y3 " << iyy[fed_id - 637][tt_id - 1][2];
                newEEStatus[ixx[fed_id - 637][tt_id - 1][2] - 1][iyy[fed_id - 637][tt_id - 1][2] - 1][side] = 1;
              }
            }
            if (m_debug)
              std::cout << " side " << side << std::endl;
          }
        } else {
          std::cout << " Strange Fed " << fed_id << " TT/SC " << tt_id << " Give up " << std::endl;
          continue;
        }
      }  // end loop over iTT
      if (m_debug)
        std::cout << std::endl;
      //*daqFile  << std::endl;
      for (int k = 0; k < 2; k++) {
        int iz = -1;
        if (k == 1)
          iz = 1;
        if (m_debug)
          std::cout << " Side : " << k << " barrel " << std::endl;  // barrel
        for (int iphi = 0; iphi < 72; iphi++) {
          for (int ieta = 0; ieta < 17; ieta++) {
            if (newEBStatus[iphi][ieta][k] != oldEBStatus[iphi][ieta][k]) {
              somediff = true;
              EcalTrigTowerDetId ebid(iz, EcalBarrel, ieta + 1, iphi + 1);
              daq_temp->setValue(ebid, newEBStatus[iphi][ieta][k]);
              if (m_debug)
                std::cout << " change in EB side " << iz << " phi " << iphi + 1 << " eta " << ieta + 1 << std::endl;
            }  // new status
            if (m_debug)
              std::cout << newEBStatus[iphi][ieta][k] << " ";
            oldEBStatus[iphi][ieta][k] = newEBStatus[iphi][ieta][k];
          }
          if (m_debug)
            std::cout << std::endl;
        }
        if (m_debug)
          std::cout << " endcaps " << std::endl;  // endcap
        for (int iy = 0; iy < 20; iy++) {
          for (int ix = 0; ix < 20; ix++) {
            if (EcalScDetId::validDetId(ix + 1, iy + 1, iz)) {
              if (newEEStatus[ix][iy][k] != oldEEStatus[ix][iy][k]) {
                somediff = true;
                EcalScDetId eeid(ix + 1, iy + 1, iz);
                daq_temp->setValue(eeid, newEEStatus[ix][iy][k]);
                if (m_debug)
                  std::cout << " change in EE side " << iz << " x " << ix + 1 << " y " << iy + 1 << std::endl;
              }  // new status
              if (m_debug)
                std::cout << newEEStatus[ix][iy][k] << " ";
              oldEEStatus[ix][iy][k] = newEEStatus[ix][iy][k];
            }  // valid SC
            else {
              if (m_debug)
                std::cout << ". ";
            }
          }
          if (m_debug)
            std::cout << std::endl;
        }
      }  // loop over side

      if (somediff) {
        // we copy the last valid record to the object that is sent to the DB
        EcalDAQTowerStatus* daq_pop = new EcalDAQTowerStatus();

        int iz = -1;
        for (int k = 0; k < 2; k++) {
          if (k == 1)
            iz = 1;
          for (int iphi = 1; iphi < 73; iphi++) {
            for (int ieta = 1; ieta < 18; ieta++) {
              if (EcalTrigTowerDetId::validDetId(iz, EcalBarrel, ieta, iphi)) {
                EcalTrigTowerDetId ebid(iz, EcalBarrel, ieta, iphi);
                uint16_t dbStatus = 0;
                dbStatus = (daq_temp->barrel(ebid.hashedIndex())).getStatusCode();
                daq_pop->setValue(ebid, dbStatus);
              }
            }  // end loop over ieta
          }    // end loop over iphi

          // endcap
          for (int ix = 1; ix < 21; ix++) {
            for (int iy = 1; iy < 21; iy++) {
              if (EcalScDetId::validDetId(ix, iy, iz)) {
                EcalScDetId eeid(ix, iy, iz);

                EcalDAQTowerStatus::const_iterator it = daq_temp->find(eeid.rawId());
                uint16_t dbStatus = 0;
                if (it != daq_temp->end()) {
                  dbStatus = it->getStatusCode();
                }
                daq_pop->setValue(eeid, dbStatus);
              }
            }  // end loop over iy
          }    // end loop over ix
        }      // end loop over k (side)

        std::cout << "Generating popcon record for run " << irun << std::endl;

        m_to_transfer.push_back(std::make_pair((EcalDAQTowerStatus*)daq_pop, irun));

        ss << "Run=" << irun << "_DAQchanged_" << std::endl;
        m_userTextLog = ss.str() + ";";
      }  // some change found
      else {
        std::cout << "Run" << irun << " DAQ record was the same as previous run " << std::endl;
        ss << "Run=" << irun << "_DAQunchanged_" << std::endl;
        m_userTextLog = ss.str() + ";";

      }  // no change

      // hlt    } // check on number of already transferred runs
    }  // loop over runs
  }    // check on run number > 0

  // disconnect from DB
  delete econn;
  delete daq_temp;
  if (m_debug)
    std::cout << "Ecal - > end of getNewObjects -----------\n";
}
