#include <iostream>
#include <map>

#include <TFile.h>

#include <PndEmcMapper.h>
#include <PndEmcNeighbourStore.h>
#include <PndEmcTwoCoordIndex.h>
#include <PndEmcStructure.h>

using namespace std;

void neighbour_test()
{
    TFile file("emc.root");

    PndEmcMapper *mapper = PndEmcMapper::Instance(1);
   
    cout << "total: " << mapper->GetTciMap().size() << " tci's" << endl;

    map<Int_t, Int_t> errors;  // Indexed by module.

    for (map<Int_t, PndEmcTwoCoordIndex *>::const_iterator
        tci = mapper->GetTciMap().begin(); tci != mapper->GetTciMap().end();
        ++tci)
    {
        // (Need to copy the neighbours as they're returned by value. Return by
        // reference might be better here?)
        PndEmcNeighbourStore neighbours = tci->second->GetNeighbours();
        
        for (PndEmcNeighbourStore::const_iterator
            neighbour = neighbours.begin(); neighbour != neighbours.end();
            ++neighbour)
        {
            // Check if the supposed neighbour is actually a neighbour.
            if (!tci->second->IsNeighbour(*neighbour))
                ++errors[tci->first / 100000000];
        }
    }

    for (map<Int_t, Int_t>::const_iterator it = errors.begin();
        it != errors.end(); ++it)
    {
        cout << it->second << " errors in module " << it->first << endl;
    }
}
