Universita' di GenovaINFN Sezione di Genova  
AIRWATCH / EUSO Genova

EFocalSurfacePainter - source file

// ESAF : Euso Simulation and Analysis Framework
// $Id: EFocalSurfacePainter.cc,v 1.30 2005/05/05 17:39:22 thea Exp $
// Author A.Thea Jun, 28 2004

/*****************************************************************************
 * ESAF: Euso Simulation and Analysis Framework                              *
 *                                                                           *
 *  Id: FocalSurfacePainter                                                  *
 *  Package: EventViewer                                                     *
 *  Coordinator: Alessandro.Thea                                             *
 *                                                                           *
 *****************************************************************************/

#include "EFocalSurfacePainter.hh"

#include "EDetector.hh"
#include "EElectronics.hh"
#include "EFee.hh"
#include "EAFee.hh"
#include "EMacroCellHit.hh"

#include "Etypes.hh"
#include "ERunParameters.hh"

#include "Riostream.h"
#include "TCanvas.h"
#include "TList.h"
#include "TStyle.h"
#include "TVector3.h"

#include <TSystem.h>
#include <TGaxis.h>

using namespace TMath;
//______________________________________________________________________________
//
//  EFocalSurfacePainter
//
//  A painter to show the development of the signal on the focal surface.
//
//  Setters:
//  
//  ViewNspread
//  MaxColors
//
//
//  TO BE DONE
 
ClassImp(EFocalSurfacePainter)
ClassImp(EPolygon)

//_____________________________________________________________________________
EFocalSurfacePainter::EFocalSurfacePainter( EDetector* det, ERunParameters* runpars) {
    // 
    // Constructor
    //

    gStyle->SetPalette(1); //FIXME I would like a local palette
    fDetector = det;
    fRunPars =  runpars;

    fLines = new TList;

    fGtu               = -1;

    fModified          = kFALSE;

    // graphics options
    fViewRange       = 5.;
    fViewThreshold     = 4;
    fViewBorder        = 0.2;

    fGifCanvasW        = 600;
    fGifCanvasH        = 600;

    fTxtCounter.SetTextAlign(33);
    fTxtCounter.SetTextSize(0.05);
    fTxtSum.SetTextAlign(33);
    fTxtSum.SetTextSize(0.05);

    fPixelOff          = kFALSE;
    fPixelEmpty        = kFALSE;
    fGtuCounter        = kTRUE;
    fTimeIntegral      = kFALSE;
    fAutoRadius        = kTRUE;
    fMoveView          = kFALSE;
    
    fX1                = 0;
    fX2                = 0;
    fY1                = 0;
    fY2                = 0;

    fXmin              = 0;
    fXmax              = 0;
    fYmin              = 0;
    fYmax              = 0;


    fNumColors         = 49;
    fColorOffset       = 51;
    fMaxCounts         = 30;

    fPixelColorOff     = kBlack;
    fPixelColorEmpty   = kWhite;
    fPmtColorEmpty     = 49;
    fPmtColorHit       = 27;

    fCacheContents     = 0;

    fCurrentMapID      = kFeeSignals;

    if ( !fDetector || !fRunPars ) {
        Info("EFocalSurfacePainter","Either Event or RunParameters is not defined. Painter made zombie.");
        MakeZombie(); 
        return;
    }

    Load();
    Modified();
    FindView( kFeeHits );
    fAutoRadius = kFALSE;

}

//_____________________________________________________________________________
EFocalSurfacePainter::~EFocalSurfacePainter() {
    // 
    // Destructor
    // 
    
    Clear();
    SafeDelete(fLines);
}

//_____________________________________________________________________________
 void EFocalSurfacePainter::Clear( Option_t *opt ) {
    //
    // Clear all temporay containers
    // 

    TString option(opt);

    ClearPolyLines();

    fPmtsToPaint.clear();
    fCache.clear();

    fPmtLines.clear();
    fPixelLines.clear();

    fFrontEndHits.clear();
    fFrontEndSignals.clear();
    fMacroCellHits.clear();
}

//_____________________________________________________________________________
 void EFocalSurfacePainter::Draw( Option_t *opt ) {
    //
    // Build polylines and then append the FSP to pad
    //

    if ( IsZombie() ) return;
    
    TString options(opt);

    // rebuild the lines if needed FIXME
    if ( fPixelLines.empty() ) BuildPolyLines();

    AppendPad(opt);
}

//_____________________________________________________________________________
 void EFocalSurfacePainter::Paint( Option_t *opt ) {
    //
    // Options:
    // 
    // ng: paints pmts with a different colors according to the simulated bg
    //     rate
    //
    // hits: paints the electronics hits on the focal surface
    //

    TString options(opt);

    if (  (fGtu < fFirstGtu || fGtu > fLastGtu) && fGtu != -1 ) return;

    // something has changed, find the viewport again
    if ( fModified ) FindView( kFeeHits );

    // calculate frame
    Float_t viewWidth = fViewRange;

    if ( viewWidth < fRunPars->GetPmtData().GetPadSide() ) return;
        
    Float_t padHeight = gPad->GetWh();
    Float_t padWidth  = gPad->GetWw();
    Float_t padMin = TMath::Min(padHeight, padWidth);

    Float_t expand = 1+fViewBorder;

    fXmin = -( viewWidth*padWidth/padMin )+fViewCenter.X();
    fYmin = -( viewWidth*padHeight/padMin )+fViewCenter.Y();
    fXmax =  ( viewWidth*padWidth/padMin )+fViewCenter.X();
    fYmax =  ( viewWidth*padHeight/padMin )+fViewCenter.Y();

    // reset the box size
    fX1 = ( fXmin-fViewCenter.X() )*expand+fViewCenter.X();
    fX2 = ( fXmax-fViewCenter.X() )*expand+fViewCenter.X();
    fY1 = ( fYmin-fViewCenter.Y() )*expand+fViewCenter.Y();
    fY2 = ( fYmax-fViewCenter.Y() )*expand+fViewCenter.Y();

    gPad->Range( fX1, fY1, fX2, fY2);

    
    FindPmtsToPaint( fXmin, fYmin, fXmax, fYmax);
    
    if ( options == "ng" )
        PaintNightGlowRate();
    else 
        PaintHits();
}

//_____________________________________________________________________________
 void EFocalSurfacePainter::PaintHits() {
    //
    // Paint the 
    //

    map mMapToPaint = GetMapToPaint();

    UInt_t sum(0);
    Int_t  nPmtPads = fRunPars->GetPmtData().GetNumPads();
    for(UInt_t i(0); i< fPmtsToPaint.size(); i++){

        // check if this pmt is empty
        Bool_t isEmpty = kTRUE;
        Int_t suid = fRunPars->GetPmtGeo( fPmtsToPaint[i] )->GetStartUniqueId();
        for( Int_t k(0); k < nPmtPads; k++) 
            isEmpty &= mMapToPaint.count(suid+k) == 0;

        // draw the pmt
        EPolygon *p = GetPmtLine( fPmtsToPaint[i] );
        p->SetFillColor( isEmpty ? fPmtColorEmpty : fPmtColorHit );
        p->Paint("f");
        p->Paint();


        // paint the pixels
        for( Int_t k(0); k < nPmtPads; k++) {
            Int_t uid = suid+k;
            Color_t c = -1;
            if ( mMapToPaint.count(uid) == 0  ) {
                if ( fPixelOff )
                    c = fPixelColorOff;
            } else {
                UInt_t counts = mMapToPaint[uid];
                if ( counts == 0 ) {
                    if ( fPixelEmpty || fCurrentMapID == kCellHits )
                        // draw always empty hits in case for macrocells
                        c = fPixelColorEmpty;
                } else {
                    sum += counts;
                    if ( counts < fMaxCounts ) {
                        c = fColorOffset+((Int_t)((Float_t)fNumColors/(Float_t)fMaxCounts)*counts);
                    } else {
                        c = fColorOffset+fNumColors;
                    }
                }
            }

            if ( c < 0 ) continue;
            p = GetPixelLine( uid );
            p->SetFillColor(c);

            p->Paint("f");
            p->Paint();
        }

    }

    if ( fGtuCounter ) {
        if ( fGtu == -1 )
            fTxtCounter.SetText( fXmax, fYmax, Form("All Gtus %d-%d",fFirstGtu,fLastGtu));
        else
            fTxtCounter.SetText( fXmax, fYmax, Form("%d",fGtu));

        fTxtCounter.Paint();

        fTxtSum.SetText( fXmax, fYmax-0.1*(fYmax-fYmin), Form("Hits on screen: %d",sum));
        fTxtSum.Paint();
    }
    
}

//_____________________________________________________________________________
 void EFocalSurfacePainter::PaintNightGlowRate() {
    //
    // Paint the elementary cells with color depending on the ng rate.
    //
    
    const TArrayF& rates = fRunPars->GetChipNightGlowRates();
    Double_t rmax = MaxElement(rates.GetSize(),rates.GetArray());
    Double_t rmin = MinElement(rates.GetSize(),rates.GetArray());
    Int_t ndiv = 10;

    EPolygon *p; 
    for(UInt_t i(0); i< fPmtsToPaint.size(); i++){

        // check if this pmt is empty
        Int_t suid = fRunPars->GetPmtGeo( fPmtsToPaint[i] )->GetStartUniqueId();

        // draw the pmt
        p = GetPmtLine( fPmtsToPaint[i] );

        Int_t chipId = fRunPars->FrontEndChip(suid)-1;
        Double_t rate = fRunPars->GetChipNightGlowRates()[chipId];
        p->SetFillColor( fColorOffset+(Int_t)((rate-rmin)/(rmax-rmin)*fNumColors) );

        p->Paint("f");
        p->Paint();

    }

    Float_t w = (fXmax-fXmin)/15.;
    Float_t h = (fYmax-fYmin)/fNumColors;
    TPave pave(fXmax-w,fYmin,fXmax,fYmax,0);

    for( Int_t i(0); i < fNumColors; i++) {
       pave.SetFillColor(i+fColorOffset); 
       pave.PaintPave(fXmax-w,fYmin+i*h,fXmax,fYmin+(i+1)*h,0);
    }

    TGaxis fAxis;
    fAxis.PaintAxis(fXmax,fYmin,fXmax,fYmax,rmin,rmax,ndiv,"+L");
}

//______________________________________________________________________________
 Int_t EFocalSurfacePainter::DistancetoPrimitive(Int_t px, Int_t py) {
    //
    //
    //
    Int_t pxl, pyl, pxt, pyt;
    Int_t px1 = gPad->XtoAbsPixel(fX1);
    Int_t py1 = gPad->YtoAbsPixel(fY1);
    Int_t px2 = gPad->XtoAbsPixel(fX2);
    Int_t py2 = gPad->YtoAbsPixel(fY2);
    if (px1 < px2) {pxl = px1; pxt = px2;}
    else           {pxl = px2; pxt = px1;}
    if (py1 < py2) {pyl = py1; pyt = py2;}
    else           {pyl = py2; pyt = py1;}

    if ( (px >= pxl && px <= pxt) && (py >= pyl && py <= pyt) ) return 0;
    else return 9999;

}

//______________________________________________________________________________
 void EFocalSurfacePainter::ExecuteEvent(Int_t event, Int_t px, Int_t py) {
    //
    // Execute an event when the mouse moves in the display active region
    //

    switch (event) {
        case kMouseMotion:
            break;
        case kButton1Up:
            if ( fMoveView ) {
                SetViewCenter(gPad->AbsPixeltoX(px),gPad->AbsPixeltoY(py));
                gPad->Modified();
                gPad->Update();
                fMoveView  = kFALSE;
            }
            break;
        default:
            break;
    }

}

//______________________________________________________________________________
 char* EFocalSurfacePainter::GetObjectInfo(Int_t px, Int_t py) const {
    //   
    //   Redefines TObject::GetObjectInfo.
    //
    if (!gPad) return (char*)"";
    static char info[64];
    Double_t x  = gPad->PadtoX(gPad->AbsPixeltoX(px));//+fViewCenter.X();
    Double_t y  = gPad->PadtoY(gPad->AbsPixeltoY(py));//+fViewCenter.Y();
    Int_t pmtId=-1;
    Int_t pxUID=-1;

    size_t j;
    for (j=0; j<fPmtsToPaint.size(); j++) {
        if ( FindPmtLine(fPmtsToPaint[j])->DistancetoPrimitive(px,py) == 0 ) {
            pmtId = fPmtsToPaint[j];
            break;
        }
    }

    if ( pmtId == -1 ) {
        sprintf(info,"(x=%g, y=%g, no pixel)",x,y);
    } else {
        Int_t  nPmtPads = fRunPars->GetPmtData().GetNumPads();
        // take the first uid in the mapmt
        Int_t suid = fRunPars->GetPmtGeo( pmtId )->GetStartUniqueId();
        for( Int_t k(0); k < nPmtPads; k++) {
            Int_t uid = suid+k;

            if ( fPixelLines.count(uid) == 0 ) break;

            map<Int_t, EPolygon* >::const_iterator it = fPixelLines.find(uid);
            if ( it->second->DistancetoPrimitive(px,py) == 0 ) {
                pxUID = uid;
                break;
            }

        }

        if ( pxUID != -1 ) {
            map mMap = GetMapToPaint();
            sprintf(info,"(x=%g, y=%g, pmt id=%d, px id=%d, counts=%d)",x,y,pmtId,pxUID,mMap[pxUID]);
        } else {
            sprintf(info,"(x=%g, y=%g, pmt id=%d, no pixel)",x,y,pmtId);
        }
    }

    return info;
}

//_____________________________________________________________________________
 Bool_t EFocalSurfacePainter::FindView( EMapId id ) {
    //
    // calculates center, spread and orientation of the viewport
    //

    if ( IsZombie() ) return kFALSE;

    Modified( kFALSE );

    // define range
    Int_t nPixelsOn(0);
    TVector3 vSum2(0.,0.,0.);
    fViewCenter.SetXYZ(0.,0.,0.);
    fSpread.SetXYZ(0.,0.,0.);

    Int_t nCountsOverThres(0);

    GtuCounts_t& mViewMap = fFrontEndSignals;

    // find view center and axis
    GtuCounts_t::iterator itGtu;
    Counts_t::iterator itUId;

    for(itGtu = mViewMap.begin(); itGtu !=  mViewMap.end(); itGtu++)
        for( itUId = (itGtu->second).begin(); itUId != (itGtu->second.end()); itUId++){
            if ( itUId->second == 0 ) continue;

            nPixelsOn += itUId->second;

            if ( itUId->second < fViewThreshold ) continue;

            // Pmt normals are downward oriented, while Z axis must be upward oreinted

            TVector3 dummy = fRunPars->PixelCenter(itUId->first);
            fViewCenter += dummy*itUId->second;

            nCountsOverThres+= itUId->second;

        }

    Info("FindView","%d pixels to draw, %d over threshold", nPixelsOn, nCountsOverThres);
    if ( !nCountsOverThres ) {
        Warning("FindView","Not possible to find the center. Try to Lower the threshold");

        const EPmtGeo* pmt = fRunPars->GetPmtGeoByUId(1);
        fViewCenter = pmt->GetCenter();
    } else {
        fViewCenter *= 1./(Float_t)nCountsOverThres;

        // calculate spread along the axis
        for(itGtu = mViewMap.begin(); itGtu !=  mViewMap.end(); itGtu++)
            for( itUId = (itGtu->second).begin(); itUId != (itGtu->second.end()); itUId++){
                if ( itUId->second == 0 ) continue;

                if ( itUId->second < fViewThreshold ) continue;

                TVector3 dummy = fRunPars->PixelCenter(itUId->first)-fViewCenter;
                fSpread[0] += TMath::Power((dummy.X()),2)*itUId->second;
                fSpread[1] += TMath::Power((dummy.Y()),2)*itUId->second;
                fSpread[2] += TMath::Power((dummy.Z()),2)*itUId->second;

            }

    }

    for(Int_t i(0); i<3; i++) {
        fSpread[i] = TMath::Sqrt(fSpread[i]/(Float_t)nCountsOverThres);
        fSpread[i] = TMath::Max(fSpread[i],(Double_t)fRunPars->GetPmtData().GetPadSide());
    }

    if ( fAutoRadius ) fViewRange = fSpread.Mag()*5;
    BuildPolyLines();

    return kTRUE;
}

//______________________________________________________________________________
 void EFocalSurfacePainter::CenterViewOnPmt( Int_t pmtid){
    //
    //
    //

    fViewCenter = fRunPars->GetPmtGeo(pmtid)->GetCenter();
}
//_____________________________________________________________________________
 void EFocalSurfacePainter::BuildPolyLines() {
    // 
    // Creates all the polylines for all the hits in the event
    //
    
    if ( !fDetector ) {
        Error("BuildPolyLines","No Event defined");
        return;
    }

    ClearPolyLines();

    // parse all maps and build all the pixels and the pmts
    GtuCounts_t::iterator itGtu;
    Counts_t::iterator itUId;


    for(itGtu = fFrontEndHits.begin(); itGtu !=  fFrontEndHits.end(); itGtu++)
        for( itUId = itGtu->second.begin(); itUId != itGtu->second.end(); itUId++) {

            Int_t uid = itUId->first;
            GetPixelLine( uid );

            // associated pmt
            Int_t id = fRunPars->Pmt(uid);
            GetPmtLine( id );

        }

}

//______________________________________________________________________________
 void EFocalSurfacePainter::ClearPolyLines() {
    //
    // remove all the polylines stored in memory
    //

    fLines->Clear();
    fPmtLines.clear();
    fPixelLines.clear();
}

//_____________________________________________________________________________
 Bool_t EFocalSurfacePainter::Load() {
    // 
    // Load data and fills GtuCounts from the event and the runparameters
    //
    
    Clear();

    fFirstGtu = kMaxInt;
    fLastGtu = -kMaxInt;

    if ( !fDetector->GetNumFee() ) {
        Info("Load","No EFee in EDetector object. Painter made zombie.");
        MakeZombie();
        return kFALSE;
    }
    
    // read EFee from event
    for(Int_t i(0); i<fDetector->GetNumFee(); i++) {
        EFee *dfee = fDetector->GetFee(i);
        Int_t uid =  dfee->GetChUId();
        Int_t gtu =  dfee->GetGtu();

        if ( fFrontEndHits[gtu].count( uid ) == 0 ) 
            fFrontEndHits[gtu][uid] = dfee->GetNumHits();
        else
            fFrontEndHits[gtu][uid] += dfee->GetNumHits();

        if ( fFrontEndSignals.count( uid ) == 0 ) 
            fFrontEndSignals[gtu][uid] = dfee->GetNumSignals();
        else
            fFrontEndSignals[gtu][uid] += dfee->GetNumSignals();

        // look for first and last gtu
        if ( gtu < fFirstGtu ) fFirstGtu = gtu;
        if ( gtu > fLastGtu )  fLastGtu = gtu;
    }

    // read CellHits from event
    for(Int_t i(0); i<fDetector->GetNumCellHits(); i++) {
        EMacroCellHit* mchit = fDetector->GetMacroCellHit(i);
        Int_t uid =  mchit->GetChUId();
        Int_t gtu =  mchit->GetGtu();

        if ( fMacroCellHits[gtu].count( uid ) == 0 ) 
            fMacroCellHits[gtu][uid] = mchit->GetCounts();
        else
            fMacroCellHits[gtu][uid] += mchit->GetCounts();

        // look for first and last gtu
        if ( gtu < fFirstGtu ) fFirstGtu = gtu;
        if ( gtu > fLastGtu )  fLastGtu = gtu;
    }

    return kTRUE;
}


//_____________________________________________________________________________
 EPolygon* EFocalSurfacePainter::GetPixelLine( Int_t uid ) {
    // 
    // Returns a EPolygon corresponding to uid if doesn't exist.
    // If doesn't exists yet in fPixelLines creates a new one. 
    //

    if ( fPixelLines.count(uid) != 0 )
        return fPixelLines[uid];
    
    // projections of the axis of the geometry

    Float_t x[5], y[5];
    for(Int_t iCorner(0); iCorner<4; iCorner++){
        TVector3 dummy = fRunPars->PixelCorner(uid,iCorner);//-fViewCenter;
        x[iCorner] = dummy.X();//*fXaxis;
        y[iCorner] = dummy.Y();//*fYaxis;
    }
    x[4] = x[0];
    y[4] = y[0];

    EPolygon *p = new EPolygon(5,x,y); 
    fPixelLines[uid] = p;
    fLines->Add(p);

    fPixelLines[uid] = p;
    fLines->Add(p);

    return p;
}

//_____________________________________________________________________________
 EPolygon* EFocalSurfacePainter::FindPmtLine( Int_t id ) const {
    //
    // Lookf for EPolygon corresponding to id among the ones already created
    //
    EPolygon *pol = 0;
    
    if ( fPmtLines.count( id ) != 0 ) pol = fPmtLines.find(id)->second;
    return pol;
    
}

//_____________________________________________________________________________
 EPolygon* EFocalSurfacePainter::GetPmtLine( Int_t id ) {
    //
    // Returns a EPolygon corresponding to id if doesn't exist.
    // If doesn't exists yet in fPmtLines creates a new one. 
    //

    if ( fPmtLines.count( id ) != 0 )
        return fPmtLines[id];

    // this pmt doesn't exists. make new polyline

    Float_t x[5], y[5];
    for(Int_t iCorner(0); iCorner<4; iCorner++){
        TVector3 dummy = fRunPars->PmtCorner(id, iCorner);
        x[iCorner] = dummy.X();
        y[iCorner] = dummy.Y();
    }
    x[4] = x[0];
    y[4] = y[0];

    EPolygon *p = new EPolygon(5,x,y); 
    fPmtLines[id] = p;
    
    fLines->Add(p);
    return p;
}

//_____________________________________________________________________________
 void EFocalSurfacePainter::FindPmtsToPaint( Float_t xmin, Float_t ymin, Float_t xmax, Float_t ymax ) {
    // 
    // Looks for Pmts which centers are in the viewport. 
    // Fills fPmtsToPaint and fPixelsToPaint
    //
    
    fPmtsToPaint.clear();

    for( Int_t i(1); i <= fRunPars->GetNumPmts(); i++) {
        TVector3 dummy = fRunPars->GetPmtGeo(i)->GetCenter();
        if ( dummy.X() < xmin || dummy.X() > xmax ||
             dummy.Y() < ymin || dummy.Y() > ymax )
            continue;

        // add the polyline to the list
        GetPmtLine(i);
        fPmtsToPaint.push_back(i);
    }

}


//_____________________________________________________________________________
 void EFocalSurfacePainter::Play( Option_t *opt ) {
    // 
    // Start the movie
    //

    if ( gPad ) gPad->Clear();

    TString option(opt);

    Draw( option );

    fTimer->Connect("Timeout()","EFocalSurfacePainter", this, "Animate()" );
    Rewind(); 

    Info("Play","Animation started");
    fTimer->TurnOn();
}

//_____________________________________________________________________________
 void EFocalSurfacePainter::Animate() {
    //
    // Move to next animation step and increase the counter
    //
   
    if ( IsEnded() ) {
        // leave the last frame to be painted
        fGtu = fLastGtu;
        Stop();
        return;
    } 

    NextFrame();

    if ( gPad ) {
        gPad->Modified();
        gPad->Update();
    }

}

//_____________________________________________________________________________
 void EFocalSurfacePainter::NextFrame() {
    //
    // Move the internal counter to the next frame
    //
    
    if ( fGtu < fLastGtu ) fGtu++;
}

//_____________________________________________________________________________
 void EFocalSurfacePainter::MakeGifSequence(const char *option, const char *path) {
    //
    // Build a sequence of gifs inside a dedicated canvas.
    // Gifs are created with opt options 
    //


    TString opt(option);

    TCanvas *display = new TCanvas("MakeGifDisplay","MakeGifDisplay",
                                   fGifCanvasW, fGifCanvasH);

    fGtu = -1;

    display->GetListOfPrimitives()->Add(this,opt);
    display->Modified(kTRUE);

    display->SaveAs("AnimHead.gif");

    for( fGtu = fFirstGtu; fGtu <= fLastGtu; fGtu++) {
        display->Modified();
        display->Update();
        display->SaveAs(Form("%s%.3d.gif",path,fGtu));
    }

    fGtu = -1;
    display->Modified();
    display->SaveAs("AnimTail.gif");

    delete display;

}
//_____________________________________________________________________________
 map EFocalSurfacePainter::GetMapToPaint() const {

    map mMapToPaint;

    const GtuCounts_t *pMap = GtuCountsToPaint();
    if (!pMap) return mMapToPaint;

    GtuCounts_t::const_iterator itGtu;
    if (fGtu == -1 ) {
        // paint all hits superimposed

        for( itGtu = pMap->begin(); itGtu != pMap->end(); itGtu++)
            mMapToPaint += (itGtu->second);

    } else if ( fTimeIntegral ) {
        // draw superposition of all hits from first gtu to the current one.


        // check cache content
        if ( fCacheContents != fCurrentMapID ) CacheTimeInt();

        // look for the right entry
        if ( (itGtu = fCache.find(fGtu)) != fCache.end() ) {
            mMapToPaint = (itGtu->second);
        } else {
            for( Int_t gtu = fFirstGtu; gtu <= fGtu; gtu++) {
                if ( (itGtu = pMap->find( gtu )) == pMap->end() ) continue;

                mMapToPaint += (itGtu->second);
            }
        }

    } else {
        // draw just a gtu
        itGtu = pMap->find(fGtu);
        if ( itGtu != pMap->end() ) mMapToPaint = itGtu->second;
//        else Printf("What? %d",fGtu);

    }

    return mMapToPaint;

}

//_____________________________________________________________________________
 const GtuCounts_t* EFocalSurfacePainter::GtuCountsToPaint() const {
   
    switch( fCurrentMapID ) {
        case kFeeHits:
            return &fFrontEndHits;
        case kFeeSignals:
            return &fFrontEndSignals;
        case kCellHits:
            return &fMacroCellHits;
        default:
            Error("MapToPaint","bad id in fCurrentMapID, returning 0");
    }
     return 0;
}

//_____________________________________________________________________________
 void EFocalSurfacePainter::CacheTimeInt() const {
    // cache data to speedup animation
    
    Info("CacheTimeInt","Caching data");
    
    map dummy;
    const GtuCounts_t *map = GtuCountsToPaint();
    if ( !map ) return;

    fCache.clear();

    GtuCounts_t::const_iterator itGtu;
    for( Int_t gtu = fFirstGtu; gtu <= fLastGtu; gtu++ ) {
        if ( (itGtu = map->find(gtu)) != map->end())
        dummy += (itGtu->second); 
        fCache[gtu] = dummy;
    }

    fCacheContents = fCurrentMapID;
    
}

const map operator+(const map& a, const map& b){
    Counts_t::iterator itA;
    Counts_t::const_iterator itB;
    map tmp = a;

    for( itB = b.begin(); itB != b.end(); itB++) {
        if ( (itA = tmp.find(itB->first) ) == tmp.end() )
            tmp[itB->first] = itB->second;
        else
            itA->second += itB->second;
    }
    return tmp;
}

map& operator+=( map& a, const map& b){
    Counts_t::iterator itA;
    Counts_t::const_iterator itB;

    for( itB = b.begin(); itB != b.end(); itB++) {
        if ( (itA = a.find(itB->first) ) == a.end() )
            a[itB->first] = itB->second;
        else
            itA->second += itB->second;
    }
    return a;
}


//______________________________________________________________________________
EPolygon::EPolygon( Int_t n, Float_t *x, Float_t *y ) : TPolyLine( n, x, y ) {
    //
    // Constructor
    //
}


//______________________________________________________________________________
EPolygon::~EPolygon() {
    //
    // Destructor
    //

}


//______________________________________________________________________________
void EPolygon::ExecuteEvent(Int_t event, Int_t px, Int_t py) {


    TPolyLine::ExecuteEvent(event, px, py);
}


//______________________________________________________________________________
Int_t EPolygon::DistancetoPrimitive(Int_t px, Int_t py) {

    Int_t distance= 999999;
    if (TMath::IsInside(gPad->AbsPixeltoX(px),gPad->AbsPixeltoY(py),fLastPoint+1,fX,fY)) distance = 0;

    return distance;
}


About Us | EUSO Official Website | Web pages created by Roberto Pesce and Alessandro Thea - Last Update 14-May-2005 21:31