mazurov
9/14/2016 - 2:55 PM

CMX Et Sums

CMX Et Sums

DAT key=1 xAOD::CMXEtSums crate=0 sourceComponent=1 peak=1 et=3 ex=1 ey=2 etError=0 exError=0, 'eyError=0
DAT key=2 xAOD::CMXEtSums crate=0 sourceComponent=2 peak=1 et=0 ex=0 ey=0 etError=0 exError=0, 'eyError=0
DAT key=3 xAOD::CMXEtSums crate=0 sourceComponent=3 peak=1 et=3 ex=2 ey=0 etError=0 exError=0, 'eyError=0
DAT key=4 xAOD::CMXEtSums crate=0 sourceComponent=4 peak=1 et=4 ex=1 ey=3 etError=0 exError=0, 'eyError=0
DAT key=5 xAOD::CMXEtSums crate=0 sourceComponent=5 peak=1 et=2 ex=1 ey=0 etError=0 exError=0, 'eyError=0
DAT key=6 xAOD::CMXEtSums crate=0 sourceComponent=6 peak=1 et=2 ex=1 ey=0 etError=0 exError=0, 'eyError=0
DAT key=8 xAOD::CMXEtSums crate=0 sourceComponent=8 peak=1 et=0 ex=0 ey=0 etError=0 exError=0, 'eyError=0
DAT key=9 xAOD::CMXEtSums crate=0 sourceComponent=9 peak=1 et=2 ex=0 ey=1 etError=0 exError=0, 'eyError=0
DAT key=10 xAOD::CMXEtSums crate=0 sourceComponent=10 peak=1 et=4 ex=2 ey=1 etError=0 exError=0, 'eyError=0
DAT key=11 xAOD::CMXEtSums crate=0 sourceComponent=11 peak=1 et=7 ex=1 ey=6 etError=0 exError=0, 'eyError=0
DAT key=12 xAOD::CMXEtSums crate=0 sourceComponent=12 peak=1 et=58 ex=15 ey=54 etError=0 exError=0, 'eyError=0
DAT key=13 xAOD::CMXEtSums crate=0 sourceComponent=13 peak=1 et=1 ex=0 ey=0 etError=0 exError=0, 'eyError=0
DAT key=14 xAOD::CMXEtSums crate=0 sourceComponent=14 peak=1 et=1 ex=0 ey=0 etError=0 exError=0, 'eyError=0

DAT key=18 xAOD::CMXEtSums crate=0 sourceComponent=LOCAL_STANDARD peak=1 et=87 ex=32756 ey=32711 etError=0 exError=0, 'eyError=0
SIM key=18 xAOD::CMXEtSums crate=0 sourceComponent=LOCAL_STANDARD peak=1 et=87 ex=32756 ey=32711 etError=0 exError=0, 'eyError=0

DAT key=19 xAOD::CMXEtSums crate=0 sourceComponent=LOCAL_RESTRICTED peak=1 et=87 ex=32756 ey=32711 etError=0 exError=0, 'eyError=0
SIM key=19 xAOD::CMXEtSums crate=0 sourceComponent=LOCAL_RESTRICTED peak=1 et=87 ex=32756 ey=32711 etError=0 exError=0, 'eyError=0

DAT key=100 xAOD::CMXEtSums crate=1 sourceComponent=0 peak=1 et=0 ex=0 ey=0 etError=0 exError=0, 'eyError=0
DAT key=102 xAOD::CMXEtSums crate=1 sourceComponent=2 peak=1 et=2 ex=1 ey=1 etError=0 exError=0, 'eyError=0
DAT key=103 xAOD::CMXEtSums crate=1 sourceComponent=3 peak=1 et=2 ex=0 ey=1 etError=0 exError=0, 'eyError=0
DAT key=104 xAOD::CMXEtSums crate=1 sourceComponent=4 peak=1 et=0 ex=0 ey=0 etError=0 exError=0, 'eyError=0
DAT key=105 xAOD::CMXEtSums crate=1 sourceComponent=5 peak=1 et=4 ex=3 ey=2 etError=0 exError=0, 'eyError=0
DAT key=106 xAOD::CMXEtSums crate=1 sourceComponent=6 peak=1 et=0 ex=0 ey=0 etError=0 exError=0, 'eyError=0
DAT key=107 xAOD::CMXEtSums crate=1 sourceComponent=7 peak=1 et=22 ex=8 ey=19 etError=0 exError=0, 'eyError=0
DAT key=109 xAOD::CMXEtSums crate=1 sourceComponent=9 peak=1 et=1 ex=0 ey=0 etError=0 exError=0, 'eyError=0
DAT key=110 xAOD::CMXEtSums crate=1 sourceComponent=10 peak=1 et=1 ex=0 ey=0 etError=0 exError=0, 'eyError=0
DAT key=111 xAOD::CMXEtSums crate=1 sourceComponent=11 peak=1 et=4 ex=3 ey=2 etError=0 exError=0, 'eyError=0
DAT key=112 xAOD::CMXEtSums crate=1 sourceComponent=12 peak=1 et=4 ex=0 ey=3 etError=0 exError=0, 'eyError=0
DAT key=113 xAOD::CMXEtSums crate=1 sourceComponent=13 peak=1 et=0 ex=0 ey=0 etError=0 exError=0, 'eyError=0
DAT key=114 xAOD::CMXEtSums crate=1 sourceComponent=14 peak=1 et=1 ex=0 ey=0 etError=0 exError=0, 'eyError=0
DAT key=115 xAOD::CMXEtSums crate=1 sourceComponent=15 peak=1 et=0 ex=0 ey=0 etError=0 exError=0, 'eyError=0

DAT key=116 xAOD::CMXEtSums crate=1 sourceComponent=REMOTE_STANDARD peak=1 et=87 ex=32756 ey=32711 etError=0 exError=0, 'eyError=0
SIM key=116 xAOD::CMXEtSums crate=1 sourceComponent=REMOTE_STANDARD peak=1 et=87 ex=32756 ey=32711 etError=0 exError=0, 'eyError=0

DAT key=117 xAOD::CMXEtSums crate=1 sourceComponent=REMOTE_RESTRICTED peak=1 et=87 ex=32756 ey=32711 etError=0 exError=0, 'eyError=0
SIM key=117 xAOD::CMXEtSums crate=1 sourceComponent=REMOTE_RESTRICTED peak=1 et=19 ex=9 ey=18 etError=0 exError=0, 'eyError=0

DAT key=118 xAOD::CMXEtSums crate=1 sourceComponent=LOCAL_STANDARD peak=1 et=41 ex=9 ey=18 etError=0 exError=0, 'eyError=0
SIM key=118 xAOD::CMXEtSums crate=1 sourceComponent=LOCAL_STANDARD peak=1 et=41 ex=9 ey=18 etError=0 exError=0, 'eyError=0

DAT key=119 xAOD::CMXEtSums crate=1 sourceComponent=LOCAL_RESTRICTED peak=1 et=19 ex=9 ey=18 etError=0 exError=0, 'eyError=0
SIM key=119 xAOD::CMXEtSums crate=1 sourceComponent=LOCAL_RESTRICTED peak=1 et=19 ex=9 ey=18 etError=0 exError=0, 'eyError=0

DAT key=120 xAOD::CMXEtSums crate=1 sourceComponent=TOTAL_STANDARD peak=1 et=128 ex=32747 ey=32729 etError=0 exError=0, 'eyError=0
DAT key=121 xAOD::CMXEtSums crate=1 sourceComponent=TOTAL_RESTRICTED peak=1 et=106 ex=32747 ey=32729 etError=0 exError=0, 'eyError=0
DAT key=122 xAOD::CMXEtSums crate=1 sourceComponent=SUM_ET_STANDARD peak=1 et=255 ex=255 ey=255 etError=0 exError=0, 'eyError=0
DAT key=123 xAOD::CMXEtSums crate=1 sourceComponent=SUM_ET_RESTRICTED peak=1 et=255 ex=255 ey=255 etError=0 exError=0, 'eyError=0
DAT key=124 xAOD::CMXEtSums crate=1 sourceComponent=MISSING_ET_STANDARD peak=1 et=63 ex=63 ey=63 etError=0 exError=0, 'eyError=0
DAT key=126 xAOD::CMXEtSums crate=1 sourceComponent=MISSING_ET_SIG_STANDARD peak=1 et=3 ex=3 ey=3 etError=0 exError=0, 'eyError=0
void L1EnergyCMXTools::formCMXEtSumsCrate(
    const xAOD::CMXEtSumsContainer *cmxEtSumsMod,
    xAOD::CMXEtSumsContainer *cmxEtSumsCrate) const
{
    float etaTruncXE, etaTruncTE;
    // From menu
    findRestrictedEta(etaTruncXE, etaTruncTE);
    ATH_MSG_INFO("NEW Restricted eta ranges etaTruncXE=" << etaTruncXE << " etaTruncTE=" << etaTruncTE);
    // Convert to internal containers
    int peak = 0;
    MultiSliceModuleEnergy modulesVec;
    MultiSliceCrateEnergy cratesVecFull;
    MultiSliceCrateEnergy cratesVecRestricted;
    etSumsToModuleEnergy(cmxEtSumsMod, modulesVec, peak);
    // Process each slice
    for (auto iter : modulesVec)
    {
        DataVector<ModuleEnergy> *modules = iter;
        DataVector<CrateEnergy> *cratesFull = new DataVector<CrateEnergy>;
        DataVector<CrateEnergy> *cratesRestricted = new DataVector<CrateEnergy>;
        cratesVecFull.push_back(cratesFull);
        cratesVecRestricted.push_back(cratesRestricted);
        m_etTool->crateSums(modules, cratesFull);
        m_etTool->crateSums(modules, cratesRestricted, etaTruncXE, etaTruncTE, true);
        delete modules;
    }

    // Convert back to CMXEtSums
    crateEnergyToEtSums(cratesVecFull, cratesVecRestricted, cmxEtSumsCrate, peak);
    // crateEnergyToEtSums(cratesVecRestricted, cmxEtSumsCrate, peak);

    for (auto crate : cratesVecFull)
    {
        delete crate;
    }
    for (auto crate : cratesVecRestricted)
    {
        delete crate;
    }
}
void L1EnergyCMXTools::crateEnergyToEtSums(
    const MultiSliceCrateEnergy &cratesVec,
    xAOD::CMXEtSumsContainer *cmxEtSumsVec, int peak) const
{
    std::map<int, xAOD::CMXEtSums *> etMap;
    unsigned int nslices = cratesVec.size();
    std::vector<uint16_t> dummy(nslices);
    std::vector<uint32_t> error(nslices);
    for (unsigned int slice = 0; slice < nslices; ++slice)
    {
        const DataVector<CrateEnergy> *crates = cratesVec[slice];
        DataVector<CrateEnergy>::const_iterator pos = crates->begin();
        DataVector<CrateEnergy>::const_iterator pose = crates->end();
        for (; pos != pose; ++pos)
        {
            CrateEnergy *energy = *pos;
            unsigned int ex = energy->exTC();
            unsigned int ey = energy->eyTC();
            unsigned int et = energy->et();
            int exOverflow = energy->exOverflow();
            int eyOverflow = energy->eyOverflow();
            int etOverflow = energy->etOverflow();
            if (ex == 0 && ey == 0 && et == 0 &&
                exOverflow == 0 && eyOverflow == 0 && etOverflow == 0)
                continue;
            int crate = energy->crate();
            xAOD::CMXEtSums *sums = 0;
            int source = xAOD::CMXEtSums::LOCAL_STANDARD;
            for (int i = 0; i < 2; ++i)
            {
                for (int j = 0; j < 2; ++j)
                {
                    int key = crate * 100 + source;
                    std::map<int, xAOD::CMXEtSums *>::iterator iter = etMap.find(key);
                    if (iter == etMap.end())
                    {
                        sums = new xAOD::CMXEtSums;
                        sums->makePrivateStore();
                        sums->initialize(crate, source, dummy, dummy, dummy,
                                         error, error, error, peak);
                        etMap.insert(std::make_pair(key, sums));
                        cmxEtSumsVec->push_back(sums);
                    }
                    else
                        sums = iter->second;
                    std::vector<uint16_t> exVec(sums->exVec());
                    std::vector<uint16_t> eyVec(sums->eyVec());
                    std::vector<uint16_t> etVec(sums->etVec());
                    std::vector<uint32_t> exErr(sums->exErrorVec());
                    std::vector<uint32_t> eyErr(sums->eyErrorVec());
                    std::vector<uint32_t> etErr(sums->etErrorVec());
                    exVec[slice] = ex;
                    eyVec[slice] = ey;
                    etVec[slice] = et;
                    if (exOverflow)
                    {
                        DataError dEx(exErr[slice]);
                        dEx.set(DataError::Overflow);
                        exErr[slice] = dEx.error();
                    }
                    if (eyOverflow)
                    {
                        DataError dEy(eyErr[slice]);
                        dEy.set(DataError::Overflow);
                        eyErr[slice] = dEy.error();
                    }
                    if (etOverflow)
                    {
                        DataError dEt(etErr[slice]);
                        dEt.set(DataError::Overflow);
                        etErr[slice] = dEt.error();
                    }
                    sums->addEx(exVec, exErr);
                    sums->addEy(eyVec, eyErr);
                    sums->addEt(etVec, etErr);
                    if (source == xAOD::CMXEtSums::LOCAL_STANDARD)
                    {
                        source = xAOD::CMXEtSums::LOCAL_RESTRICTED;
                    }
                    else
                    {
                        source = xAOD::CMXEtSums::REMOTE_RESTRICTED;
                    }
                }
                if (crate == 1)
                    break;
                crate = 1;
                source = xAOD::CMXEtSums::REMOTE_STANDARD;
            }
        }
    }
}
void L1EnergyCMXTools::crateEnergyToEtSums(
    const MultiSliceCrateEnergy &cratesVecFull, const MultiSliceCrateEnergy &cratesVecRestricted,
    xAOD::CMXEtSumsContainer *cmxEtSumsVec, int peak) const
{
    std::map<int, xAOD::CMXEtSums *> etMap;
    unsigned int nslices = cratesVecFull.size();
    std::vector<uint16_t> dummy(nslices);
    std::vector<uint32_t> error(nslices);

    for (unsigned int slice = 0; slice < nslices; ++slice)
    {
        for (unsigned int i = 0; i < 2; i++)
        {
            const auto *crates = (i == 0 ? cratesVecFull[slice] : cratesVecRestricted[slice]);
            int origSource = i == 0 ? xAOD::CMXEtSums::LOCAL_STANDARD : xAOD::CMXEtSums::LOCAL_RESTRICTED;
            
            for (auto pos : *crates)
            {
                int source = origSource;
                CrateEnergy *energy = pos;
                unsigned int ex = energy->exTC();
                unsigned int ey = energy->eyTC();
                unsigned int et = energy->et();
                int exOverflow = energy->exOverflow();
                int eyOverflow = energy->eyOverflow();
                int etOverflow = energy->etOverflow();
                if (ex == 0 && ey == 0 && et == 0 &&
                    exOverflow == 0 && eyOverflow == 0 && etOverflow == 0)
                    continue;
                int crate = energy->crate();
                xAOD::CMXEtSums *sums = 0;

                while (true) {
                    int key = crate * 100 + source;
                    std::map<int, xAOD::CMXEtSums *>::iterator iter = etMap.find(key);
                    if (iter == etMap.end())
                    {
                        sums = new xAOD::CMXEtSums;
                        sums->makePrivateStore();
                        sums->initialize(crate, source, dummy, dummy, dummy,
                                         error, error, error, peak);
                        etMap.insert(std::make_pair(key, sums));
                        cmxEtSumsVec->push_back(sums);
                    }
                    else
                        sums = iter->second;
                    std::vector<uint16_t> exVec(sums->exVec());
                    std::vector<uint16_t> eyVec(sums->eyVec());
                    std::vector<uint16_t> etVec(sums->etVec());
                    std::vector<uint32_t> exErr(sums->exErrorVec());
                    std::vector<uint32_t> eyErr(sums->eyErrorVec());
                    std::vector<uint32_t> etErr(sums->etErrorVec());
                    exVec[slice] = ex;
                    eyVec[slice] = ey;
                    etVec[slice] = et;
                    if (exOverflow)
                    {
                        DataError dEx(exErr[slice]);
                        dEx.set(DataError::Overflow);
                        exErr[slice] = dEx.error();
                    }
                    if (eyOverflow)
                    {
                        DataError dEy(eyErr[slice]);
                        dEy.set(DataError::Overflow);
                        eyErr[slice] = dEy.error();
                    }
                    if (etOverflow)
                    {
                        DataError dEt(etErr[slice]);
                        dEt.set(DataError::Overflow);
                        etErr[slice] = dEt.error();
                    }
                    sums->addEx(exVec, exErr);
                    sums->addEy(eyVec, eyErr);
                    sums->addEt(etVec, etErr);

                    ATH_MSG_INFO("SASHA o=" << origSource << " s=" << source << " crate=" << crate);
                    if (origSource == xAOD::CMXEtSums::LOCAL_STANDARD){
                        if (crate == 1){
                            break;
                        }else{
                            crate = 1;
                            source =  xAOD::CMXEtSums::REMOTE_STANDARD;
                        }
                    }else if (source == xAOD::CMXEtSums::LOCAL_RESTRICTED){
                         if (crate == 1){
                            break;
                        }else{
                            crate = 1;
                            source =  xAOD::CMXEtSums::REMOTE_RESTRICTED;
                        }
                    }else{
                        break;
                    }
                }
            } // crates
        }     //for i
    }
}