[atlas] / offline / Trigger / TrigAlgorithms / TrigMoore / src / MooHLTAlgo.cxx Repository:
ViewVC logotype

View of /offline/Trigger/TrigAlgorithms/TrigMoore/src/MooHLTAlgo.cxx

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.92 - (download) (annotate)
Fri Oct 12 13:18:23 2007 UTC (3 months, 2 weeks ago) by bigliett
Branch: MAIN
CVS Tags: TrigMoore-00-01-33
Changes since 1.91: +26 -6 lines
bug fix fot aant
#include <iostream>

#include "GaudiKernel/MsgStream.h"
#include "GaudiKernel/IDataProviderSvc.h"
#include "GaudiKernel/AlgFactory.h"
#include "GaudiKernel/PropertyMgr.h"
#include "GaudiKernel/IIncidentSvc.h"
#include "GaudiKernel/INTupleSvc.h"
#include "GaudiKernel/SmartDataPtr.h"


#include "TrigMoore/MooHLTAlgo.h"
#include "TrigMoore/MooMakePhiSegmentSeeded.h"
#include "TrigMoore/MooMakeCrudeRZSegmentsSeeded.h"
#include "TrigMoore/MooRoI.h"

#include "TrigMoore/MooMakePhiPatternSeeded.h"
#include "TrigMoore/MooMakeRzPatternSeeded.h"


#include "TrigMoore/TrigMooreHisto.h"              
#include "TrigMoore/TrigMooreAANTuple.h"

#include "TrigT1Interfaces/TrigT1Interfaces_ClassDEF.h"
#include "TrigSteeringEvent/TrigRoiDescriptor.h"
#include "TrigT1Interfaces/RecMuonRoI.h" 

#include "RegionSelector/IRegSelSvc.h"

#include "MuidEvent/MuidTrackContainer.h"
#include "MuidEvent/Muid_ClassDEF.h"
#include "MooEvent/MooTrackContainer.h"
#include "MooEvent/MdtSegmentContainer.h"
#include "TrkTrack/TrackCollection.h"

#include "MuonDigitContainer/MdtDigitContainer.h"
#include "MuonDigitContainer/MdtDigitCollection.h"

#include "MuonDigitContainer/RpcDigitContainer.h"
#include "MuonDigitContainer/RpcDigitCollection.h"

#include "MuonDigitContainer/TgcDigitContainer.h"
#include "MuonDigitContainer/TgcDigitCollection.h"

#include "TrigMuonEvent/MuonFeature.h"
#include "TrigMuonEvent/TrigMuonEFContainer.h"
#include "TrigMuonEvent/TrigMuonEF.h"

#include "TrigCosmicEvent/CosmicMuon.h"
//#include "TrigCosmicEvent/CosmicMuonCollection.h"

#include <sstream>

// ----------------------------------------
MooHLTAlgo::MooHLTAlgo(const std::string& name, ISvcLocator* pSvcLocator) :
  FexAlgo(name, pSvcLocator),
  m_print_level(0),
  m_chronotest(false),
  m_seed(true),
  m_pat(false),
  m_pRegionSelector(0),
  m_roi_num(0),
  m_track_num(0),
  m_muidtrack_num(0),
  m_muidcbtrack_num(0),
  m_roi("noSeed"),
  m_moore(0),// To run reco spectrometer 
  m_muid(0), // To add vertex  constraint
  m_muidcomb(0), // To match with inner detector
  m_ntupleID("222"),
  m_max_roi(10),
  m_prefix("tm_"),
  m_TrigMooreAANTuple(new TrigMooreAANTuple),
  m_pTrigMooreNtuple("TrigMooreNtuple"),
  m_monitoring("TrigMooreHisto"),
  m_muidMatchMaker("MuidMatchMaker"),
  m_nev(0),
  m_testbeam(false),
  m_cosmic(false),
  m_doAANtuples(false),
  m_doNtuples(false),
  m_doHistos(false),  
  m_doTimers(false),
  m_makeESD(false),
  m_makeAOD(false),
  m_AodForFexOff(true),
  m_seedFromLvl1(false),
  m_seedFromLvl2(false),
  m_deta(0),
  m_dphi(0),
  m_trigID(true),
  m_allMooreTracks(0),
  m_allMuidExtrTracks(0),
  m_allMuidCombTracks(0)
{
  declareProperty("print_level",         m_print_level);
  declareProperty("chrono_test",         m_chronotest);
  declareProperty("seed",                m_seed);
  declareProperty("pattern",             m_pat);
  declareProperty("MooreReconstruction", m_moore);
  declareProperty("MuonIdentification",  m_muid);
  declareProperty("MuidComb",            m_muidcomb);
  declareProperty("NtupleID",            m_ntupleID);
  declareProperty("maxNumerofRoi",       m_max_roi);
  declareProperty("testbeam",            m_testbeam);
  declareProperty("cosmic",            m_cosmic);
  declareProperty("NTupleVariablePrefix", m_prefix);
  declareProperty("doAANtuples",         m_doAANtuples );
  declareProperty("doNtuples",           m_doNtuples );
  declareProperty("doHistos",            m_doHistos );    
  declareProperty("doTimers",            m_doTimers );
  declareProperty("makeESD",             m_makeESD );
  declareProperty("makeAOD",             m_makeAOD );
  declareProperty("makeAODforRoI",       m_AodForFexOff);
  declareProperty("seedFromLvl1",        m_seedFromLvl1 );
  declareProperty("seedFromLvl2",        m_seedFromLvl2 );
  declareProperty("deltaEtaRoI",         m_deta);
  declareProperty("deltaPhiRoI",         m_dphi);
  declareProperty("IDFromTrigger",       m_trigID);
  declareProperty("TestString",          m_testString="");
  
//***************new TrigGenericMonitoringTool definitions (20/06/07) ***************
  declareProperty("histoPathBase",m_histo_path_base="/EXPERT/");
  declareMonitoredVariable("track_mult_moore", m_mooremult);
  declareMonitoredVariable("Chi2_moore", m_moorechi2);
  declareMonitoredVariable("Chi2prob_moore", m_moorechi2prob); 
  declareMonitoredStdContainer("mdt_res", mdt_res);
  declareMonitoredStdContainer("csc_eta_res", csc_eta_res);
  declareMonitoredStdContainer("csc_phi_res", csc_phi_res);
  declareMonitoredStdContainer("rpc_eta_res", rpc_eta_res);
  declareMonitoredStdContainer("rpc_phi_res", rpc_phi_res);
  declareMonitoredStdContainer("tgc_eta_res", tgc_eta_res);
  declareMonitoredStdContainer("tgc_phi_res", tgc_phi_res);
  declareMonitoredVariable("pt_moore", pt_moore);
  declareMonitoredVariable("phi_moore", phi_moore);
  declareMonitoredVariable("eta_moore", eta_moore);
  declareMonitoredVariable("prechits_moore", m_prechits); 
  declareMonitoredVariable ("phihits_moore", m_phihits);
  declareMonitoredVariable ("statlay_moore", m_statlay);
  declareMonitoredVariable ("charge_moore", m_charge);
//***************new TrigGenericMonitoringTool definitions (20/07/07) ***************
  declareMonitoredVariable ("chi2_muidcb", m_muidcbchi2);
  declareMonitoredVariable ("chi2prob_muidcb", m_muidcbchi2pr);
  declareMonitoredVariable ("charge_muidcb", m_chargemuidcb);
  declareMonitoredVariable ("a0_muidcb", a0_muidcb);
  declareMonitoredVariable ("z0_muidcb", z0_muidcb);
  declareMonitoredVariable ("phi_muidcb", phi_muidcb);
  declareMonitoredVariable ("eta_muidcb", eta_muidcb);
  declareMonitoredVariable ("pt_muidcb", pt_muidcb);
  declareMonitoredVariable ("phicor_muidcb-RoI", phicor_cbroi);
  declareMonitoredVariable ("etacor_muidcb-RoI", etacor_cbroi);
//***************new TrigGenericMonitoringTool definitions (26/07/07) ***************
  declareMonitoredVariable ("chi2_muidsa", m_muidsachi2);
  declareMonitoredVariable ("chi2prob_muidsa", m_muidsachi2pr);
  declareMonitoredVariable ("charge_muidsa", m_chargemuidsa);
  declareMonitoredVariable ("phi_muidsa", phi_muidsa);
  declareMonitoredVariable ("eta_muidsa", eta_muidsa);
  declareMonitoredVariable ("pt_muidsa", pt_muidsa);
  declareMonitoredVariable ("eloss_muidsa", m_muidloss);
//***********************************************************************************
  
  m_mooRoI = 0;
}

// ----------------------------------------
MooHLTAlgo::~MooHLTAlgo()
{
  delete m_TrigMooreAANTuple;
  //  delete m_mooRoI;
}

// ----------------------------------------

StatusCode
MooHLTAlgo::beginRun()
{
  FexAlgo::beginRun().ignore();

  StatusCode status = StatusCode::SUCCESS;

  MsgStream log(messageService(), name());
  log << MSG::DEBUG << "Executing MooHLTAlgo::beginRun()" << endreq;

  if (m_doAANtuples){
    if (m_seedFromLvl1 || m_seedFromLvl2 || m_moore || m_muid || m_muidcomb) {
      m_TrigMooreAANTuple->SetNtuplePrefix(m_prefix);
      log << MSG::DEBUG << "ntuple variables prefix set to: " << m_prefix << endreq;
      if(m_TrigMooreAANTuple->cleanBlocks(m_seedFromLvl1,m_seedFromLvl2,m_moore,m_muid,m_muidcomb).isFailure())
	log << MSG::WARNING << "cleanBlocks beginRun failed" << endreq;
    }
  }

  return status;
}

HLT::ErrorCode
MooHLTAlgo::hltInitialize()
{
 StatusCode status;

  MsgStream ini_log(messageService(), name());
  msg() << MSG::DEBUG 
	<< "Initializing MooHLTAlgo with Steps " 
	<< "Moore : " << m_moore  << endreq;
  msg() << MSG::DEBUG 
	<< "Muid SA  " << m_muid 
	  << endreq;
  msg() << MSG::DEBUG 
	<< "Muid CB  " << m_muidcomb 
	<< endreq;
  
 if(msgLvl() <= MSG::DEBUG){
   msg() << MSG::DEBUG 
	 <<"package version = "<<PACKAGE_VERSION<<endreq;
      msg() << MSG::DEBUG 
      <<"Properties are set as follows: "<<endreq;
      msg() << MSG::DEBUG 
	    <<"print_level                    "<<m_print_level<<endreq;
      msg() << MSG::DEBUG 
	    <<"chrono_test                    "<<m_chronotest<<endreq;
      msg() << MSG::DEBUG 
	    <<"seed                           "<<m_seed<<endreq;
      msg() << MSG::DEBUG 
	    <<"MooreReconstruction            "<<m_moore<<endreq;
      msg() << MSG::DEBUG 
	    <<"MuonIdentification             "<<m_muid<<endreq;
      msg() << MSG::DEBUG 
	    <<"MuidComb                       "<<m_muidcomb<<endreq;
      msg() << MSG::DEBUG 
	    <<"NtupleID                       "<<m_ntupleID<<endreq;
      msg() << MSG::DEBUG 
	    <<"maxNumerofRoi                  "<<m_max_roi<<endreq;
      msg() << MSG::DEBUG 
	    <<"testbeam                       "<<m_testbeam<<endreq;
      msg() << MSG::DEBUG 
	    <<"cosmic                       "<<m_cosmic<<endreq;
      msg() << MSG::DEBUG 
	    <<"NTupleVariablePrefix           "<<m_prefix<<endreq;
      msg() << MSG::DEBUG 
	    <<"doAANtuples                    "<<m_doAANtuples <<endreq;
      msg() << MSG::DEBUG 
	    <<"doNtuples                      "<<m_doNtuples <<endreq;
      msg() << MSG::DEBUG 
	    <<"doHistos                       "<<m_doHistos <<endreq;    
      msg() << MSG::DEBUG 
	    <<"doTimers                       "<<m_doTimers <<endreq;
      msg() << MSG::DEBUG  
	    <<"makeESD                        "<<m_makeESD <<endreq;
      msg() << MSG::DEBUG 
	    <<"makeAOD                        "<<m_makeAOD <<endreq;
      msg() << MSG::DEBUG 
	    <<"makeAODForROI                  "<<m_AodForFexOff <<endreq;
      msg() << MSG::DEBUG 
	    <<"seedFromLvl1                   "<<m_seedFromLvl1 <<endreq;
      msg() << MSG::DEBUG 
	    <<"seedFromLvl2                   "<<m_seedFromLvl2 <<endreq;
      msg() << MSG::DEBUG 
	    <<"deltaEtaRoI                    "<<m_deta<<endreq;
      msg() << MSG::DEBUG 
	    <<"deltaPhiRoI                    "<<m_dphi<<endreq;
      msg() << MSG::DEBUG 
	    <<"IDFromTrigger                    "<< m_trigID<<endreq;
  }
  

  // initializes chrono svc
  if (m_chronotest) chronosvc = chronoSvc();
  
  // Locate the StoreGateSvc and initialize our local ptr
  status = service("StoreGateSvc", p_SGevent);
  if (!status.isSuccess() || 0 == p_SGevent){
    msg() << MSG::ERROR << "MooHLTALGO::initialize() : Could not find StoreGateSvc" 
	    << endreq;
    return HLT::BAD_JOB_SETUP;
  }
  
  if(m_cosmic){
    status = service("RegSelSvc", m_pRegionSelector);
    if(status.isFailure()) {
      msg() << MSG::FATAL
            << "Unable to retrieve RegionSelector Svc"
            << endreq;
      return HLT::BAD_JOB_SETUP;
    }
    msg() << MSG::DEBUG << "Initializing MooMakePhiPatternSeeded " << endreq;
    status = createSubAlgorithm("MooMakePhiPatternSeeded","MooMakePhiPatternSeededEF_CS" ,m_MooMakePhiPatternSeededAlg); 
    
    if (status.isFailure())
      msg() << MSG::ERROR 
            << "Cannot create sub-Alg of type MooMakePhiPatternSeeded" << endreq;
    status = createSubAlgorithm("MooMakeRzPatternSeeded", "MooMakeRzPatternSeededEF_CS",m_MooMakeRzPatternSeededAlg);
    if (status.isFailure())
      msg() << MSG::ERROR 
            << "Cannot create sub-Alg of type MooMakeRzPatternSeeded" << endreq;   
    
  }
  if(m_moore){

    // Get the region selector tool:
    status = service("RegSelSvc", m_pRegionSelector);
    if(status.isFailure()) {
      msg() << MSG::FATAL
	      << "Unable to retrieve RegionSelector Svc"
	      << endreq;
      return HLT::BAD_JOB_SETUP;
    }
    
    // initialize the sub-algorithms
    
    if(m_seed) {
      if(m_pat){
        msg() << MSG::DEBUG << "Initializing MooMakePhiPatternSeeded " << endreq;
	status = createSubAlgorithm("MooMakePhiPatternSeeded","MooMakePhiPatternSeededEF" ,m_MooMakePhiPatternSeededAlg); 
	
        if (status.isFailure())
          msg() << MSG::ERROR 
                << "Cannot create sub-Alg of type MooMakePhiPatternSeeded" << endreq;
	status = createSubAlgorithm("MooMakeRzPatternSeeded", "MooMakeRzPatternSeededEF",m_MooMakeRzPatternSeededAlg);
        if (status.isFailure())
          msg() << MSG::ERROR 
                << "Cannot create sub-Alg of type MooMakeRzPatternSeeded" << endreq;    

      }else{  
        msg() << MSG::DEBUG << "Initializing MooMakePhiSegmentsSeeded " << endreq;
	status = createSubAlgorithm("MooMakePhiSegmentSeeded", "MooMakePhiSegmentSeededEF", m_MooMakePhiSegmentSeededAlg);  
        if (status.isFailure())
          msg() << MSG::ERROR 
                << "Cannot create sub-Alg of type MooMakePhiSegmentSeeded" << endreq;
	status = createSubAlgorithm("MooMakeCrudeRZSegmentsSeeded", "MooMakeCrudeRZSegmentsSeededEF", m_MooMakeCrudeRZSegmentsSeededAlg);
        if (status.isFailure())
          msg() << MSG::ERROR 
                << "Cannot create sub-Alg of type MooMakeCrudeRZSegmentsSeeded" << endreq;    
      }
    }else{

      if(m_pat){
        msg() << MSG::DEBUG << "Initializing MooMakePhiSegments " << endreq;
	status = createSubAlgorithm("MooMakePhiPatterns", "MooMakePhiPatternsEF" ,m_MooMakePhiPatternsAlg);  
        if (status.isFailure())
          msg() << MSG::ERROR 
                << "Cannot create sub-Alg of type MooMakePhiPatterns" << endreq;
	status = createSubAlgorithm("MooMakeRzPatterns", "MooMakeRzPatternsEF" ,m_MooMakeRzPatternsAlg);  
        if (status.isFailure())
          msg() << MSG::ERROR 
                << "Cannot create sub-Alg of type MooMakeRzPatterns" << endreq;
      }else{
        msg() << MSG::DEBUG << "Initializing MooMakePhiSegments " << endreq;
	status = createSubAlgorithm("MooMakePhiSegments", "MooMakePhiSegmentsEF", m_MooMakePhiSegmentsAlg);  
        if (status.isFailure())
          msg() << MSG::ERROR 
                << "Cannot create sub-Alg of type MooMakePhiSegments" << endreq;
	status = createSubAlgorithm("MooMakeCrudeRZSegments", "MooMakeCrudeRZSegmentsEF", m_MooMakeCrudeRZSegmentsAlg);  
        if (status.isFailure())
          msg() << MSG::ERROR 
                << "Cannot create sub-Alg of type MooMakeCrudeRZSegments" << endreq;
      }
    }
    
    
    
    if(m_pat){
      
      status = createSubAlgorithm("MooCombinePatterns", "MooCombinePatternsEF", m_MooCombinePatternsAlg);
      if (status.isFailure())
	msg() << MSG::ERROR 
	      << "Cannot create sub-Alg of type MooCombinedPatterns" << endreq;

      status = createSubAlgorithm("MooCalibratedSegmentMaker", "MooCalibratedSegmentMakerEF", m_MooCalibratedSegmentMaker);
      if (status.isFailure())
        msg() << MSG::ERROR 
              << "Cannot create sub-Alg of type MooCalibratedSegmentMaker" << endreq; 

      status = createSubAlgorithm("MooRzSegmentCombinationMaker", "MooRzSegmentCombinationMakerEF", m_MooRzSegmentCombinationMaker);
      if (status.isFailure())
        msg() << MSG::ERROR 
              << "Cannot create sub-Alg of type MooCalibratedSegmentMaker" << endreq; 
     
      status = createSubAlgorithm("PhiPatternToPhiSeg", "PhiPatternToPhiSegEF", m_PhiPatToPhiSeg); 
      if (status.isFailure())
        msg() << MSG::ERROR 
              << "Cannot create sub-Alg of type PhiPatternToPhiSeg" << endreq;     

      status = createSubAlgorithm("CscPatternToCrudeSeg", "CscPatternToCrudeSegEF" ,m_CscPatToCscSeg); 
      if (status.isFailure())
        msg() << MSG::ERROR 
              << "Cannot create sub-Alg of type CscPatternToCrudeSeg" << endreq;
      
      status = createSubAlgorithm("MdtPatternToCrudeSeg", "MdtPatternToCrudeSegEF" ,m_MdtPatToMdtSeg);
      if (status.isFailure())
        msg() << MSG::ERROR 
              << "Cannot create sub-Alg of type MdtPatternToCrudeSeg" << endreq;
      
      status = createSubAlgorithm("MuonSegmentToMooRzSegment","MuonSegmentToMooRzSegmentEF", m_MuonSegToMooSeg);
      if (status.isFailure())
	msg() << MSG::ERROR 
	      << "Cannot create sub-Alg of type MuonSegmentToMooRzSegment " << endreq;
      
      status = createSubAlgorithm("MuonSegmentComboToMooRzSegmentCombo","MuonSegmentComboToMooRzSegmentComboEF" ,m_MuonSegToMooSegCombo);
      if (status.isFailure())
	msg() << MSG::ERROR 
	      << "Cannot create sub-Alg of type MuonSegmentComboToMooRzSegmentCombo " << endreq;
      
      status = createSubAlgorithm("MooRoadMaker","MooRoadMakerEF" ,m_MooRoadMaker);
      if (status.isFailure())
	msg() << MSG::ERROR 
	      << "Cannot create sub-Alg of type MooRoadMaker " << endreq;
    }

    if(!m_pat){
       msg() << MSG::DEBUG << "Initializing MooMakeRoads " << endreq;
       status = createSubAlgorithm("MooMakeRoads", "MooMakeRoadsEF" ,m_MooMakeRoadsAlgs);  
       if (status.isFailure())
	 msg() << MSG::ERROR 
	       << "Cannot create sub-Alg of type MooMakeRoads" << endreq;
    }
     
    msg() << MSG::DEBUG << "Initializing MooMakeTracks " << endreq;
    status = createSubAlgorithm("MooMakeTracks", "MooMakeTracksEF", m_MooMakeTracksAlgs);  
     if (status.isFailure())
       msg() << MSG::ERROR 
	     << "Cannot create sub-Alg of type MooMakeTracks" << endreq;
     
     msg() << MSG::DEBUG << "Initializing MooSummary " << endreq;
     status = createSubAlgorithm("MooSummary", "MooSummaryEF" ,m_MooSummaryAlgs);  
     if (status.isFailure())
       msg() << MSG::ERROR 
	     << "Cannot create sub-Alg of type MooSummary" << endreq;
	     
     
     // TrigMooHisto initialization only if required.
     if (m_doHistos) {
       if (m_monitoring.retrieve().isFailure()){
	msg() << MSG::FATAL << "Histo monitor: Unable to create the tool " << endreq;
	return HLT::BAD_JOB_SETUP;
       } 
       
     }
  }


  
  if(m_muid){
    msg() << MSG::DEBUG << "Initializing MuidStandAlone " << endreq;
    status = createSubAlgorithm("MuidStandAlone", "MuidStandAloneEF", m_MuidStandAloneAlg);
    if (status.isFailure())
      msg() << MSG::ERROR 
	    << "Cannot create sub-Alg of type MuidStandAlone" << endreq;
  }  

  if (m_muidcomb){

    msg() << MSG::DEBUG << "Initializing MuidComb " << endreq;

    if (m_muidMatchMaker.retrieve().isFailure()){
      msg() << MSG::FATAL << "Failed to retrieve MuidMatchMaker tool " << endreq;
      return HLT::BAD_JOB_SETUP;
    } else  msg() << MSG::INFO << "Retrieved MuidMatchMaker tool " << endreq;
  }
  
  //////////////////////////////////////////////////////////////////////////
  
  IIncidentSvc* pIncsvc; 
  status = serviceLocator()->service("IncidentSvc", pIncsvc);
  int priority=   100;
  if ( status.isSuccess() ) {
    pIncsvc->addListener( this, "EndEvent", priority);
  } else {
    msg() << MSG::ERROR << "Could not register incident service  " << endreq;
  }
  ///////////////////////////////////////////
  // ntuples init
  //////////////////////////////////////////
  // Book the ntuple
  if(m_doNtuples){
    if (m_pTrigMooreNtuple.retrieve().isFailure()){
        msg() << MSG::FATAL << " Unable to create the ntuple tool " << endreq;
	return HLT::BAD_JOB_SETUP;
    }
    
    SmartDataPtr<NTuple::Directory> ntid(ntupleSvc(), "/NTUPLES/FILE1/CBNT");
    if ( !ntid ){  
      msg() << MSG::DEBUG << "creating directory ...  " << endreq;
      ntid = ntupleSvc()->createDirectory("/NTUPLES/FILE1/CBNT");
    }else{
      msg() << MSG::DEBUG << "Ntuple ID retrieved from the store  " << endreq;
    }
    if ( ! ntid )  { 
      msg() << MSG::WARNING << " unable to create ntuple directory"  << endreq;
      return HLT::BAD_JOB_SETUP;
    }
    msg() << MSG::DEBUG << "ntuple ID " << m_ntupleID << endreq;
    if(m_moore){      
      msg() << MSG::DEBUG << "booking moore ntuples... " << endreq;
      m_ntuple = ntupleSvc()->book(ntid.ptr(), m_ntupleID, CLID_ColumnWiseTuple,"moore");
      msg() << MSG::DEBUG << "declaring variables... " << endreq;
      status = m_pTrigMooreNtuple->bookNtuple(m_ntuple);
      if(!status.isSuccess() ) { 
	msg() << MSG::ERROR << " failed to book Moore ntuple"  << endreq;
      } else msg() << MSG::DEBUG << " Moore ntuple booked successfully"  << endreq;
    }
    msg() << MSG::DEBUG << " bookNtuple done!" << endreq;
    if (m_ntuple){
      if(!status.isSuccess() )  
	msg() << MSG::ERROR << " failed to book MooHLT ntuple"  << endreq;
    }else {
      msg() << MSG::ERROR << " failed to book ntuple"  << endreq;
    }
  }

  if (m_doAANtuples){
    if (m_seedFromLvl1 || m_seedFromLvl2 || m_moore || m_muid || m_muidcomb) {
      m_TrigMooreAANTuple->SetNtuplePrefix(m_prefix);
      msg() << MSG::DEBUG << "ntuple variables prefix set to: " << m_prefix << endreq;
    }
  }


  if(m_doTimers){
    // add some timers:
    m_tTotal      = addTimer("tTotal","nTracks");
    m_tPhi        = addTimer("tMakePhiSeg"); 
    m_tRZ         = addTimer("tMakeRZSeg"); 
    m_tPhiSeeded  = addTimer("tMakePhiSegSd"); 
    m_tRZSeeded   = addTimer("tMakeRZSegSd"); 
    m_tMakeRoad   = addTimer("tMakeRoad","nMdt"); 
    m_tMakeTracks = addTimer("tMakeTracks","nRpc");  
    m_tMooSummary = addTimer("tMakeSummary");  
    m_tMuidSA     = addTimer("tMuidSA","nTgc");   
    m_tMuidComb   = addTimer("tMuidComb");   
  }

// *********** new 

  StatusCode sc;
 
  StoreGateSvc* detStore=0;
  sc = serviceLocator()->service("DetectorStore", detStore);
  if ( sc.isSuccess() ) {
     sc = detStore->retrieve( muon_mgr );
     if ( sc.isFailure() ) {
       msg() << MSG::ERROR << "Cannot retrieve MuonDetDescrMgr " << endreq;
     } else {
       p_MdtIdHelper = muon_mgr->mdtIdHelper();
       p_CscIdHelper = muon_mgr->cscIdHelper();
       p_RpcIdHelper = muon_mgr->rpcIdHelper();
       p_TgcIdHelper = muon_mgr->tgcIdHelper();
     }
   } else {
    msg() << MSG::ERROR << "MuonDetDescrMgr not found in DetectorStore " << endreq;
   }


// *****************

  msg() << MSG::DEBUG 
        << "End of init MooHLTAlgo" << endreq;
  return HLT::OK;
}

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

HLT::ErrorCode
MooHLTAlgo::hltExecute(const HLT::TriggerElement* inputTE, HLT::TriggerElement* TEout){
  
  msg() << MSG::DEBUG << ": Executing MooHLTAlgo::execHLTAlgorithm()" << endreq;
  if (m_chronotest) chronosvc->chronoStart("All Algos");
  if(m_doTimers) m_tTotal->start();
    
  HLT::ErrorCode hltStatus;
  StatusCode status;
  // Need  to number the RoIs for object in the TDS 
  ++m_roi_num;
  std::ostringstream s;
  s<<m_roi_num;
  std::string roi = "RoI";
  m_roi = roi+s.str();
  msg() << MSG::DEBUG << "RoI number : " << m_roi_num <<  " " 
	<< m_roi << endreq ;
  
  if(!m_seed&&m_roi_num>1) return HLT::OK;
  
  const LVL1::RecMuonRoI *  muonRoIl1 = 0;
  const TrigRoiDescriptor * fakeRoI = 0;
  const MuonFeature * muonRoIL2 = 0;
  const MooTrackContainer * track_container = 0;
  const MuidTrackContainer * standAloneTracks = 0;
  const MuidTrackContainer * combinedTracks = 0;

  if(m_cosmic){
    double etaRoI=0., phiRoI=0., thRoI=0.;
    std::string RoIType= "Cosmic";

    std::vector<const CosmicMuon*> vectorOfCosmicMuon;
    hltStatus = getFeatures(inputTE,vectorOfCosmicMuon, "");
    if(hltStatus!=HLT::OK){ 
      msg() << MSG::VERBOSE <<"Unappropriate status for VectorOfCosmicMuon: " << hltStatus << endreq; 
    }
    const TrigRoiDescriptor* muonRoI = 0;
    hltStatus = getFeature(inputTE, muonRoI);   
    if(muonRoI==0) {
      msg() << MSG::VERBOSE  << "Could not find the initial RoIDescriptor, " 
            << "trying to get the RecMuonRoI ....." << endreq;
      std::vector<const LVL1::RecMuonRoI*> muonRoIs;
      hltStatus = getFeatures(inputTE, muonRoIs);
      if(hltStatus!=HLT::OK){ 
        msg() << MSG::VERBOSE <<"RecMuonRoIs isn't present: " << hltStatus << endreq; 
      }
      msg() << MSG::VERBOSE << "RecMuonRoI size: " << muonRoIs.size() << endreq;
      msg() << MSG::VERBOSE << "Vector of Cosmic Muon size: " << vectorOfCosmicMuon.size() << endreq;
    }else{
      etaRoI = muonRoI->eta0();
      phiRoI = muonRoI->phi0();
      thRoI = 1000.;

      msg() << MSG::VERBOSE  << "RoIid " 
            << muonRoI->roiId() << " eta =" 
            << etaRoI  << " phi =" 
            << phiRoI << " Thr = "
            << thRoI << endreq;
    }
      
      double phiToRS = phiRoI;
      
      // phi must go from 0 to 2*pi                                                                                     
      if (phiToRS < 0)  phiToRS += 2.*M_PI;
      double phiMin = phiToRS - m_dphi;
      double phiMax = phiToRS + m_dphi;


      
      if (phiMin < 0.) phiMin =0;
      if (phiMax < 0.) phiMax =+ 2.*M_PI;
      if (phiMin > 2*M_PI) phiMin -= 2*M_PI;
      if (phiMax > 2*M_PI) phiMax -= 2*M_PI;

      
      msg()<<MSG::VERBOSE<<"Phicentral = "<<phiToRS<<" Phi min, max = "<<phiMin<<" "<<phiMax<<endreq;
      msg()<<MSG::VERBOSE<<"Etacentral = "<<etaRoI<<" Eta min, max = "
	   <<etaRoI-m_deta<<" "<<etaRoI+m_deta<<endreq;
      
      std::vector<IdentifierHash>  rpc_hash_ids;
      m_pRegionSelector->DetHashIDList(RPC,
				       etaRoI-m_deta,
				       etaRoI+m_deta,
				       phiMin,
				       phiMax, &rpc_hash_ids);
      
      std::vector<IdentifierHash>  tgc_hash_ids;
      m_pRegionSelector->DetHashIDList(TGC,
				       etaRoI-m_deta,
				       etaRoI+m_deta,
				       phiMin,
				       phiMax,&tgc_hash_ids);
      
      std::vector<IdentifierHash> mdt_hash_ids;
      m_pRegionSelector->DetHashIDList(MDT,
				       etaRoI-m_deta,
				       etaRoI+m_deta,
				       phiMin,
				       phiMax, &mdt_hash_ids);
      
      std::vector<IdentifierHash> csc_hash_ids;
      m_pRegionSelector->DetHashIDList(CSC,
				       etaRoI-m_deta,
				       etaRoI+m_deta,
				       phiMin,
				       phiMax, &csc_hash_ids);
      
      msg() << MSG::VERBOSE << "The size of RPC is " << rpc_hash_ids.size() << endreq;
      msg() << MSG::VERBOSE << "The size of TGC is " << tgc_hash_ids.size() << endreq;
      msg() << MSG::VERBOSE << "The size of MDT is " << mdt_hash_ids.size() << endreq;
      msg() << MSG::VERBOSE << "The size of CSC is " << csc_hash_ids.size() << endreq;

      std::string  tmp_roi = m_roi;
      if(m_cosmic) tmp_roi = m_roi+"_CS"; 
      m_mooRoI = new MooRoI(etaRoI, phiRoI, thRoI, RoIType, tmp_roi);
      m_mooRoI->setHashIds(
			   new std::vector<IdentifierHash>(rpc_hash_ids), 
			   new std::vector<IdentifierHash>(tgc_hash_ids), 
			   new std::vector<IdentifierHash>(mdt_hash_ids), 
			   new std::vector<IdentifierHash>(csc_hash_ids)
			   );
      
       msg() << MSG::VERBOSE << "Executing MooMakePhiPattern Seeded " << endreq;
       executeAlgs(m_MooMakePhiPatternSeededAlg);
       msg() << MSG::VERBOSE << "Executing MooMakeRzPattern Seeded " << endreq;
       executeAlgs(m_MooMakeRzPatternSeededAlg);

      //      for (std::vector<IdentifierHash>::iterator i = mdt_hash_ids.begin(); i != mdt_hash_ids.end(); i++) 
      //	std::cout << "    HASH  MDT Detector from RS "
      //		  << (int) (*i)
      //		  << std::endl;
      
      if(muonRoI==0) {
        msg() << MSG::ERROR << "No RoI available... execution is over " << endreq;
        return HLT::ERROR;
      }
  return hltStatus;
  }
  
  if(m_moore){
    if (m_seed){
      
      double etaRoI=0., phiRoI=0., thRoI=0.;
      std::string RoIType;
      
      const TrigRoiDescriptor* muonRoI = 0;
      hltStatus = getFeature(inputTE, muonRoI, "forMS");
      
      if(muonRoI==0) {
        msg() << MSG::DEBUG  << "Could not find RoIDescriptor <<forMS>>, " 
	      << "trying to recover the initial one." << endreq;
	hltStatus = getFeature(inputTE, muonRoI, "");
	
	if(muonRoI==0) {
	    msg() << MSG::DEBUG << "Initial one not accessible, problem here!"
	          << endreq;
            return HLT::NAV_ERROR; 
	}
      }
     
      //it must be a non zero pointer to the RoIDescriptor here!!
      //if (muonRoI){
      // so ... use it
	msg() << MSG::DEBUG  << "Got a RoiDescriptor from inputTE " << " phi "
	      << muonRoI->phi0() << " eta "
	      << muonRoI->eta0() << endreq;
	msg() << MSG::DEBUG  <<  "ROI ID  "  << muonRoI->roiId() << endreq; 
	msg() << MSG::DEBUG  <<  "LVL1 ID  "  << muonRoI->l1Id() << endreq; 
	if (m_doHistos) {
	  msg() << MSG::DEBUG << "Histo monitor: Filling MuonRoI Histograms ....  " << endreq;
	  status=m_monitoring->fillTrigMooreHists(muonRoI);
	  if(status.isFailure()){ msg() << MSG::DEBUG
					<< "Unable to fill Histos"
					<< endreq;}
	}
      //} 
      
      
      if(m_seedFromLvl1){
	///////////////////////////////////////////////
	// Seeeded reconstruction from LVL1 muon RoIs 
	// Get vector of of pointers to all MuonRecoRoIs with label "uses":
	msg() << MSG::DEBUG <<" seeding comes from Lvl1 simulation " << endreq; 

	std::vector<const LVL1::RecMuonRoI*> vectorOfRecMuonRoI;
	
	msg() << MSG::VERBOSE <<"getting vector of RecMuonRoI from inputTE... " << endreq; 
	hltStatus = getFeatures(inputTE,vectorOfRecMuonRoI, "");
 
	if(hltStatus!=HLT::OK) return HLT::NAV_ERROR;    

	msg() << MSG::VERBOSE <<"L1 MuonRecoRoI retrieved  with status " << hltStatus << endreq; 
	
	if(vectorOfRecMuonRoI.size() != 1){
	  msg() << MSG::ERROR
		<< "Size of vector of Muon RoIs is not 1 but "
		<< vectorOfRecMuonRoI.size() 
		<< endreq;
	  //  return StatusCode::FAILURE;
	} else {
	  msg() << MSG::VERBOSE <<"getting a MuonRoI from vector of objects... " << endreq; 
	  muonRoIl1 = vectorOfRecMuonRoI.front();
	  if(!muonRoIl1) {
	    msg() << MSG::ERROR
		  << "Retrieval of RoI from vector failed"
		  << endreq;
	    status= false;
	  }
	  msg() << MSG::VERBOSE <<"DONE with status : " << status << endreq; 
	  
	  if(muonRoIl1){
	    etaRoI = muonRoIl1->eta();
	    phiRoI = muonRoIl1->phi();
	    thRoI = muonRoIl1->getThresholdValue();
	    RoIType = "LVL1";
	    //
	    msg() << MSG::DEBUG
		  << "The position of the MuonRecoRoI is phi = "
		  << phiRoI
		  << " / eta = "
		  << etaRoI
		  << " / threshold = "
		  << thRoI
		  << endreq;
	  }
	}
      } else if (m_seedFromLvl2) {	
	msg() << MSG::DEBUG <<" seeding comes from Lvl2 " << endreq;
	
	std::vector<const MuonFeature*> vectorOfMuonFex;
	msg() << MSG::VERBOSE <<"getting vector of Muon Features from inputTE... " << endreq; 
	hltStatus = getFeatures(inputTE,vectorOfMuonFex, "");
	msg() << MSG::VERBOSE <<"DONE with status : " << hltStatus << endreq; 
	
	if(vectorOfMuonFex.size() != 1) {
	  msg() << MSG::DEBUG
		<< "Size of vector of Muon Feature is not 1 but "
		<< vectorOfMuonFex.size()
		<< endreq;
	  msg() << MSG::DEBUG  << "... using RoiDescriptor with " << " phi "
		<< muonRoI->phi0() << " eta "
		<< muonRoI->eta0() << endreq;
	  
	  etaRoI = muonRoI->eta0();
	  phiRoI = muonRoI->phi0();
	  thRoI = 0;
	  //  return StatusCode::FAILURE;                               
	} else {
	  msg() << MSG::VERBOSE <<"getting a Lvl2 Muon from vector of objects... " << endreq;
	  
	  muonRoIL2 = vectorOfMuonFex.front();
	  
	  if(!muonRoIL2) {
	    msg() << MSG::ERROR
		  << "Retrieval of L2 RoI from vector failed"
		  << endreq;
	    status= false;
	  }
	  
	  msg() << MSG::VERBOSE <<"MuonFeature retrieved with status : " << status << endreq;
	  
	  if(muonRoIL2){
	    phiRoI = muonRoIL2->phi();
	    etaRoI = muonRoIL2->eta();
	    thRoI = muonRoIL2->pt();
            msg() << MSG::DEBUG <<"LVL2 phi/eta = "<<phiRoI<<"/"<<etaRoI<<" muonFeature.address "
		  <<muonRoIL2->saddress()<<endreq;
            if ( muonRoIL2->saddress()==-1) 
	      {
                msg() << MSG::DEBUG <<  " The MuonFeature does not provide eta and phi in the EC" << endreq;
                //21/11/2006 temporary// The MuonFeature does not provide eta and phi in the EC
                phiRoI = muonRoI->phi0();
                etaRoI = muonRoI->eta0();
                msg() << MSG::DEBUG <<"Resetting LVL2 phi/eta with LVL1 RoiDescriptor = "<<phiRoI<<"/"<<etaRoI<<endreq;
	      }            
	    RoIType = "LVL2";                                             
	    msg() << MSG::DEBUG
		  << "The position of the Muon Feature  is phi = "
		  << phiRoI
		  << " / eta = "
		  << etaRoI
		  << " / pt = "
		  << thRoI
		  << endreq;
	  }
	}
      } else {
	///////////////////////////////////////////////
	// Seeded reconstruction from fake RoIs 
	msg() << MSG::DEBUG <<" seeding comes from KINE fake RoIs " << endreq; 
	msg() << MSG::VERBOSE <<"getting vector of fake RoI from inputTE... " << endreq; 
	// Get RoiDescriptor
	hltStatus = getFeature(inputTE, fakeRoI, ""); 
	if(!fakeRoI) {
	  msg() << MSG::ERROR
		<< "Retrieval of fake RoI from vector failed"
		<< endreq;
	  status= false;
	}
	msg() << MSG::DEBUG <<"DONE with status : " << status << endreq; 
	if(fakeRoI){
	  etaRoI = fakeRoI->eta0();
	  phiRoI = fakeRoI->phi0();
	  thRoI = 1000;
	  RoIType = "FAKE";
	  //should be DEBUG
	  msg() << MSG::DEBUG
		<< "The position of the fake RoI is phi = "
		<< phiRoI
		<< " / eta = "
		<< etaRoI
		<< " / roiId = "
		<< fakeRoI->roiId()
		<< " / l1Id = "
		<< fakeRoI->l1Id()
		<< endreq;
	}
      }
      
      
      double phiToRS = phiRoI;
      
      // phi must go from 0 to 2*pi                                                                                     
      if (phiToRS < 0)  phiToRS += 2.*M_PI;
      double phiMin = phiToRS - m_dphi;
      double phiMax = phiToRS + m_dphi;
      
      if (phiMin < 0.) phiMin =+ 2.*M_PI;
      if (phiMax < 0.) phiMax =+ 2.*M_PI;
      if (phiMin > 2*M_PI) phiMin -= 2*M_PI;
      if (phiMax > 2*M_PI) phiMax -= 2*M_PI;
      
      msg()<<MSG::DEBUG<<"Phicentral = "<<phiToRS<<" Phi min, max = "<<phiMin<<" "<<phiMax<<endreq;
      msg()<<MSG::DEBUG<<"Etacentral = "<<etaRoI<<" Eta min, max = "
	   <<etaRoI-m_deta<<" "<<etaRoI+m_deta<<endreq;
      
      std::vector<IdentifierHash>  rpc_hash_ids;
      m_pRegionSelector->DetHashIDList(RPC,
				       etaRoI-m_deta,
				       etaRoI+m_deta,
				       phiMin,
				       phiMax, &rpc_hash_ids);
      
      std::vector<IdentifierHash>  tgc_hash_ids;
      m_pRegionSelector->DetHashIDList(TGC,
				       etaRoI-m_deta,
				       etaRoI+m_deta,
				       phiMin,
				       phiMax,&tgc_hash_ids);
      
      std::vector<IdentifierHash> mdt_hash_ids;
      m_pRegionSelector->DetHashIDList(MDT,
				       etaRoI-m_deta,
				       etaRoI+m_deta,
				       phiMin,
				       phiMax, &mdt_hash_ids);
      
      std::vector<IdentifierHash> csc_hash_ids;
      m_pRegionSelector->DetHashIDList(CSC,
				       etaRoI-m_deta,
				       etaRoI+m_deta,
				       phiMin,
				       phiMax, &csc_hash_ids);
      
      msg() << MSG::DEBUG << "The size of RPC is " << rpc_hash_ids.size() << endreq;
      msg() << MSG::DEBUG << "The size of TGC is " << tgc_hash_ids.size() << endreq;
      msg() << MSG::DEBUG << "The size of MDT is " << mdt_hash_ids.size() << endreq;
      msg() << MSG::DEBUG << "The size of CSC is " << csc_hash_ids.size() << endreq;


      //      for (std::vector<IdentifierHash>::iterator i = mdt_hash_ids.begin(); i != mdt_hash_ids.end(); i++) 
      //	std::cout << "    HASH  MDT Detector from RS "
      //		  << (int) (*i)
      //		  << std::endl;
      
      
      if(muonRoI==0 && fakeRoI==0 && muonRoIL2==0) {
        msg() << MSG::ERROR << "No RoI available... execution is over " << endreq;
       return HLT::ERROR;
      }else{
        // delete m_mooRoI;
        m_mooRoI = new MooRoI(etaRoI, phiRoI, thRoI, RoIType,m_roi);
        
        m_mooRoI->setHashIds(
			     new std::vector<IdentifierHash>(rpc_hash_ids), 
			     new std::vector<IdentifierHash>(tgc_hash_ids), 
			     new std::vector<IdentifierHash>(mdt_hash_ids), 
			     new std::vector<IdentifierHash>(csc_hash_ids)
			     );
      }

      if(removeCloseRoIs(m_mooRoI)){
        msg() << MSG::DEBUG << "This RoI is close to an another one, will give the same result " 
              << endreq;
        TEout->setActiveState(true);
        MooTrackContainer *    mooreTracks    = new MooTrackContainer();
        status = p_SGevent->record(mooreTracks, "MooreTracks"+m_roi);
        if(!status.isSuccess()) {
          msg() << MSG::VERBOSE
                << "Problem in recording MooreTracks"
                << endreq;
        }
 
        return HLT::OK;
      } else {
        msg() << MSG::VERBOSE << "This RoI is far from other RoIs... let's go on! " << endreq;
      }
    }//if m_seed


  
    std::string m_myautokey = "";
  
    if(m_seed)  {
      if(m_pat){
        msg() << MSG::DEBUG << "Executing MooMakePhiPattern Seeded " << endreq;
        if(m_doTimers) m_tPhiPatSeed->start();
	executeAlgs(m_MooMakePhiPatternSeededAlg);
        if(m_doTimers) m_tPhiPatSeed->stop();
        msg() << MSG::DEBUG << "Executing MooMakeRzPattern Seeded " << endreq;
        if(m_doTimers) m_tRzPatSeed->start();
        executeAlgs(m_MooMakeRzPatternSeededAlg);
        if(m_doTimers) m_tRzPatSeed->stop();
      }else{
        msg() << MSG::DEBUG << "Executing MooMakePhiSegments Seeded " << endreq;
        if(m_doTimers) m_tPhiSeeded->start();
        executeAlgs(m_MooMakePhiSegmentSeededAlg);
        if(m_doTimers) m_tPhiSeeded->stop();
        msg() << MSG::DEBUG << "Executing MooMakeCrudeRZSegments Seeded " << endreq;
        if(m_doTimers) m_tRZSeeded->start();
        executeAlgs(m_MooMakeCrudeRZSegmentsSeededAlg);
        if(m_doTimers) m_tRZSeeded->stop();
      }
    } else {
      if(m_pat){
        msg() << MSG::DEBUG << "Executing MooMakePhiPattern " << endreq;
        if(m_doTimers) m_tPhiPat->start();
	 executeAlgs(m_MooMakePhiPatternsAlg);
        if(m_doTimers) m_tPhiPat->stop();
        msg() << MSG::DEBUG << "Executing MooMakeRzPatterns" << endreq;
        if(m_doTimers) m_tRzPat->start();
        executeAlgs(m_MooMakeRzPatternsAlg);
        if(m_doTimers) m_tRzPat->stop();
      }else{
        msg() << MSG::DEBUG << "Executing MooMakePhiSegments " << endreq;
        if(m_doTimers) m_tPhi->start();
        executeAlgs(m_MooMakePhiSegmentsAlg);
        if(m_doTimers) m_tPhi->stop();
        msg() << MSG::DEBUG << "Executing MooMakeCrudeRZSegments" << endreq;
        if(m_doTimers) m_tRZ->start();
        executeAlgs(m_MooMakeCrudeRZSegmentsAlg);
        if(m_doTimers) m_tRZ->stop();
      }
    }
    
    if(m_pat){
      msg() << MSG::DEBUG << "Executing MooCombine Patterns ... " << endreq;
      if(m_doTimers) m_tPatComb->start();
      executeAlgs(m_MooCombinePatternsAlg);
      if(m_doTimers) m_tPatComb->stop();

      msg() << MSG::DEBUG << "Executing MooCalibratedSegmentMaker ... " << endreq;
      executeAlgs(m_MooCalibratedSegmentMaker);

      msg() << MSG::DEBUG << "Executing MooRzSegmentCombinationMaker ... " << endreq;
      executeAlgs(m_MooRzSegmentCombinationMaker);
      
      msg() << MSG::DEBUG << "Executing  PhiPat to PhiSeg Seeded " << endreq;
       executeAlgs(m_PhiPatToPhiSeg);

      msg() << MSG::DEBUG << "Executing CscPat to CscSeg Seeded " << endreq;
       executeAlgs(m_CscPatToCscSeg);

      msg() << MSG::DEBUG << "Executing MdtPat to MdtSeg Seeded " << endreq;
       executeAlgs(m_MdtPatToMdtSeg);

      msg() << MSG::DEBUG << "Executing MuonPat to MooSeg Seeded " << endreq;
      executeAlgs(m_MuonSegToMooSeg);

      msg() << MSG::DEBUG << "Executing MuonPat to MooSeg Combo " << endreq;
      executeAlgs(m_MuonSegToMooSegCombo);
      
      msg() << MSG::DEBUG << "Executing MooRoadMaker... " << endreq;
     executeAlgs(m_MooRoadMaker);

    }


    if(!m_pat){
      msg() << MSG::DEBUG << "Executing MooMakeRoads " << endreq;
      if(m_doTimers) m_tMakeRoad->start();
      executeAlgs(m_MooMakeRoadsAlgs);
      if(m_doTimers) m_tMakeRoad->stop();
    }

    
    msg() << MSG::DEBUG << "Executing MooMakeTracks " << endreq;
    if(m_doTimers) m_tMakeTracks->start();
    executeAlgs(m_MooMakeTracksAlgs);
    if(m_doTimers) m_tMakeTracks->stop();
    if(m_doTimers) m_tMooSummary->start();
    if(!m_testbeam){
      msg() << MSG::DEBUG << "Executing MooSummary " << endreq;
      executeAlgs(m_MooSummaryAlgs);
    }
    if(m_doTimers) m_tMooSummary->stop();

    
    if (StatusCode::SUCCESS != p_SGevent->retrieve(track_container,"MooreTracks"+m_roi) ) {
      msg() << MSG::ERROR 
	    << "Could not find Moore Track container" 
	    << endreq;

      if (!status) return HLT::SG_ERROR;
    } else  {
      msg() << MSG::DEBUG 
	    << "Moore Track container retrieved with size : " 
	    << track_container->size() 
	    <<  endreq;
    
      if (track_container->size() > 0){ 
//***************new TrigGenericMonitoringTool definitions (20/06/07) ***************

	  m_mooremult = track_container->size();
//***********************************************************************************	  
     
      for(MooTrackContainer::const_iterator it = track_container->begin();
	  it!=track_container->end(); ++it){
	const PerigeeParameters& perigee = (**it).perigee_parameters();
	msg() << MSG::DEBUG 
	      << " REGTEST MS pt/ctheta/phi " << 1./perigee.inverse_pt()
	      << " / " << perigee.cot_theta() << " / " <<  perigee.phi()  
	      << endreq;

//***************new TrigGenericMonitoringTool definitions (20/06/07) ***************
          m_moorechi2 = (*it)->fit_quality().chi_squared();
          m_moorechi2prob = (*it)->fit_quality().fit_probability(); 
	      if (perigee.inverse_pt() != 0) {
			  pt_moore =  fabs(1./perigee.inverse_pt())/1000.;
		      if (perigee.inverse_pt() < 0) m_charge  = -1;
		      else m_charge  = 1 ;
		  }	   
		  phi_moore = perigee.phi();
	      if(perigee.cot_theta()!=0.) {
            eta_moore = -log(tan(atan(1./fabs(perigee.cot_theta()))/2.));
		    if (perigee.cot_theta()<0.) eta_moore = -eta_moore;
	      }
	      m_prechits =( (*it)->mdt_hits_count() + (*it)->csc_hits_count() );
          m_phihits  = (*it)->phi_hits_count();
	      m_statlay  = (*it)->station_layers_count();


//***********************************************************************************


      }
      }
      
      if(m_AodForFexOff){
	const double myMass = 105.7;
	TrigMuonEFContainer * myMuonEFCont = new TrigMuonEFContainer();
	for(MooTrackContainer::const_iterator it = track_container->begin();
	    it!=track_container->end(); ++it){
	  const PerigeeParameters& perigee = (**it).perigee_parameters();
	  double myPhi = perigee.phi();
	  double myTheta = perigee.cot_theta();
	  double myIpt = std::fabs(perigee.inverse_pt());
	  double myCharge = perigee.charge();
	  std::string myRoi= m_roi;
	  //   int myCode = (**it).muonCode();
	  int myCode = 0;
	  TrigMuonEF * mytrimuon = new TrigMuonEF(myIpt,myTheta,myPhi,myMass);
	  mytrimuon->set_muonCode(myCode);
	  mytrimuon->set_RoINum(myRoi);
	  mytrimuon->set_Charge(myCharge);
	  myMuonEFCont->push_back(mytrimuon);
	}	  
	msg() << MSG::DEBUG << "Attaching Moore Feature ... "  << endreq;
	
	hltStatus = attachFeature(TEout, myMuonEFCont, "MuonEF");
	
	if(hltStatus!=HLT::OK) msg() << MSG::DEBUG << "No MuonFeature per RoI" << endreq;
	
      }else{
	msg() << MSG::DEBUG << "Attaching Moore Full Tracks ... "  << endreq;
	hltStatus = attachFeature(TEout, track_container , "MuonEF");
	if(hltStatus!=HLT::OK) msg() << MSG::DEBUG << "Only Track container record" << endreq;
      }
      
      if (m_doHistos) {
	MooTrackContainer * ptrigTrackContainer= const_cast<MooTrackContainer *>(track_container);
	msg() << MSG::DEBUG << "Histo Monitor: Filling Track_Container Histograms ....  " << endreq;
	status=m_monitoring->fillTrigMooreHists(ptrigTrackContainer);
	if(status.isFailure()) msg() << MSG::DEBUG
				     << "Unable to fill Histos"
				     << endreq; 
      }
    }
  }
  
  //MuonIdentification
  if(m_muid){ 
    msg() << MSG::DEBUG << "Executing MuidStandAlone " << endreq;
    if(m_doTimers) m_tMuidSA->start();
    executeAlgs(m_MuidStandAloneAlg);
    if(m_doTimers) m_tMuidSA->stop();

    if (StatusCode::SUCCESS 
	!= p_SGevent->retrieve(standAloneTracks,"MuidStandAlone"+m_roi) ) {
      msg() << MSG::DEBUG 
	    << "Could not find MuonIdentification Track container" 
	    << endreq;
      //return( StatusCode::FAILURE);
    }   else  {
      msg() << MSG::DEBUG 
	    << "Muid Extr Track container retrieved with size : " 
	    << standAloneTracks->size() 
	    <<  endreq;

      for(MuidTrackContainer::const_iterator it = standAloneTracks->begin();
	  it!=standAloneTracks->end(); ++it){
	const PerigeeParameters& perigee = (**it).perigee_parameters();
	msg() << MSG::DEBUG 
	      << " REGTEST Extr pt/ctheta/phi " << 1./perigee.inverse_pt()
	      << " / " << perigee.cot_theta() << " / " <<  perigee.phi()  
	      << endreq;

//***************new TrigGenericMonitoringTool definitions (20/06/07) ***************
 	    if ((**it).has_perigee_parameters()){
	      phi_muidsa	=	perigee.phi();		  
          if (perigee.inverse_pt() != 0) {
       		  pt_muidsa	=	fabs(1./perigee.inverse_pt())/1000.;
			  if (perigee.inverse_pt() < 0) m_chargemuidsa  = -1;
		      else m_chargemuidsa  = 1 ;
		  }	
	      if(perigee.cot_theta()!=0.) {
            eta_muidsa = -log(tan(atan(1./fabs(perigee.cot_theta()))/2.));
		    if (perigee.cot_theta()<0.) eta_muidsa = -eta_muidsa;
	      }
		    
	      if ((**it).has_fit_quality()){
		      m_muidsachi2	=	(**it).fit_quality().chi_squared();
		      m_muidsachi2pr =	(**it).fit_quality().fit_probability();
	      }
	     }
//	     if ((**it).has_calo_energy()) {
//	       			m_muidloss = (**it).calo_energy().energyDeposit();
	       //           m_muidloss = (**it).calo_energy().energy_deposition();    
		

 //		  std::cout << "  calo energy " <<   (**it).calo_energy().energy_deposition()   <<  std::endl;
 //         }
		  
//***********************************************************************************



      }
      
      if(m_AodForFexOff){
	const double myMass = 105.7;
	TrigMuonEFContainer * myMuonEFCont = new TrigMuonEFContainer();
	for(MuidTrackContainer::const_iterator it = standAloneTracks->begin();
	    it!=standAloneTracks->end(); ++it){
	  const PerigeeParameters& perigee = (**it).perigee_parameters();
	  double myPhi = perigee.phi();
	  double myTheta = perigee.cot_theta();
	  double myIpt = std::fabs(perigee.inverse_pt());
	  double myCharge = perigee.charge();
	  std::string myRoi= m_roi;
	  //	  int myCode = (**it).muonCode();	  
	  int myCode = (**it).muonCode();
	  // int myCode = 1;
          TrigMuonEF * mytrimuon = new TrigMuonEF(myIpt,myTheta,myPhi,myMass);
	  mytrimuon->set_muonCode(myCode);
   	  mytrimuon->set_RoINum(myRoi);
          mytrimuon->set_Charge(myCharge);
	  myMuonEFCont->push_back(mytrimuon);
	}
	msg() << MSG::DEBUG << "Attaching Muid SA Feature ... "  << endreq;
	hltStatus = attachFeature(TEout, myMuonEFCont, "MuonEF");
	if(hltStatus!=HLT::OK) msg() << MSG::DEBUG << "No MuonFeature per RoI" << endreq;
	
      }else{
	msg() << MSG::DEBUG << "Attaching Muid SA Full Tracks ... "  << endreq;
	hltStatus = attachFeature(TEout, standAloneTracks, "MuonExtrEF");
	if(hltStatus!=HLT::OK) msg() << MSG::DEBUG << "Full Track container attached to " << endreq;
	
      }
    }
  }

  
  if(m_muidcomb){
    msg() << MSG::DEBUG << "Executing MuidComb " << endreq;
    if(m_doTimers) m_tMuidComb->start();
    //executeAlgs(&m_MuidCombAlgs);
    
    if (StatusCode::SUCCESS 
	!= p_SGevent->retrieve(standAloneTracks,"MuidStandAlone"+m_roi) ) {
      msg() << MSG::DEBUG 
	    << "Could not find MuonIdentification Track container" 
	    << endreq;
    }   else  {
      msg() << MSG::DEBUG 
	    << "Muid Extr Track container retrieved with size : " 
	    << standAloneTracks->size() 
	    <<  endreq;    
    }
    
    //retrieving the Inner Detector  Track Container
    const TrackCollection*  indetTracks = 0;
    if(!m_trigID){
      if (StatusCode::SUCCESS != p_SGevent->retrieve(indetTracks,"Tracks"))
	msg()<< MSG::ERROR 
	     << "MuidCombined::Could not retrieve ID Track container" 
	     << endreq; 
    }else {
      if ( HLT::OK != getFeature(inputTE, indetTracks) ) 
	msg() << MSG::ERROR 
	      << "MuidCombined::Could not retrieve TrigID Track container" 
	      << endreq; 
    }
        
    if(indetTracks!=0){
      msg()<< MSG::DEBUG
	   << "MuidCombined:: ID Track container retrieved with size : "
	   << indetTracks->size() 
	   << endreq; 
      if(standAloneTracks!=0){
	combinedTracks = m_muidMatchMaker->matchingProcedure(indetTracks,standAloneTracks);
	if(combinedTracks!=0) msg() << MSG::DEBUG << "Reconstructred Combined tracks " 
				    << combinedTracks->size() << endreq;
      }
    }
    
    if (StatusCode::SUCCESS != p_SGevent->record(combinedTracks, "MuidCombinedHyb"+m_roi)){
      msg()<< MSG::ERROR << "MuidCombined: Could not store final tracks" << endreq;

      return HLT::OK;
    }    

    if(m_doTimers) m_tMuidComb->stop();

    for(MuidTrackContainer::const_iterator it = combinedTracks->begin();
	it!=combinedTracks->end(); ++it){
      const PerigeeParameters& perigee = (**it).perigee_parameters();
      msg() << MSG::DEBUG 
	    << " REGTEST Comb pt/ctheta/phi " << 1./perigee.inverse_pt()
	    << " / " << perigee.cot_theta() << " / " <<  perigee.phi()  
	    << endreq;
//***************new TrigGenericMonitoringTool definitions (20/06/07) ***************
	    if ((**it).has_perigee_parameters()){
	      a0_muidcb	=	perigee.transverse_impact();
	      z0_muidcb	=	perigee.z();
	      phi_muidcb	=	perigee.phi();		  
//              phicor_cbroi=phi_muidcb - phiRoI;
//	      std::cout << " phi_muidcb - phiRoI-phicor" << phi_muidcb << " " << phiRoI << " " << phicor_cbroi << std::endl;
   	      if (perigee.inverse_pt() != 0) {
       		  pt_muidcb	=	fabs(1./perigee.inverse_pt())/1000.;
			  if (perigee.inverse_pt() < 0) m_chargemuidcb  = -1;
		      else m_chargemuidcb  = 1 ;
		  }	
	      if(perigee.cot_theta()!=0.) {
            eta_muidcb = -log(tan(atan(1./fabs(perigee.cot_theta()))/2.));
		    if (perigee.cot_theta()<0.) eta_muidcb = -eta_muidcb;

//			 etacor_cbroi= eta_muidcb - etaRoI;
//			 std::cout << " eta_muidcb - etaRoI-etacor" << eta_muidcb << " " << etaRoI << " " << etacor_cbroi << std::endl;
	      }
		    
	      if ((**it).has_fit_quality()){
		      m_muidcbchi2	=	(**it).fit_quality().chi_squared();
		      m_muidcbchi2pr =	(**it).fit_quality().fit_probability();
	      }
	    }
	      


//***********************************************************************************
	    
	    
    }
    
    if(combinedTracks!=0){
      if(m_AodForFexOff){
	const double myMass = 105.7;
	TrigMuonEFContainer * myMuonEFCont = new TrigMuonEFContainer();
	for(MuidTrackContainer::const_iterator it = combinedTracks->begin();
	    it!=combinedTracks->end(); ++it){
	  const PerigeeParameters& perigee = (**it).perigee_parameters();
	  double myPhi = perigee.phi();
	  double myTheta = perigee.cot_theta();
	  double myIpt = std::fabs(perigee.inverse_pt());
	  double myCharge = perigee.charge();
	  std::string myRoi= m_roi;
	  //	  	  int myCode = (**it).muonCode();
	  int myCode = (**it).muonCode();
	  // int myCode = 1;
	  TrigMuonEF * mytrimuon = new TrigMuonEF(myIpt,myTheta,myPhi,myMass);
	  mytrimuon->set_muonCode(myCode);
	  mytrimuon->set_RoINum(myRoi);
	  mytrimuon->set_Charge(myCharge);
	  myMuonEFCont->push_back(mytrimuon);
	}
	msg() << MSG::DEBUG << "Attaching Muid Comb Feature ... "  << endreq;
	hltStatus = attachFeature(TEout, myMuonEFCont, "MuonEF");
	if(hltStatus!=HLT::OK) msg() << MSG::DEBUG << "No MuonFeature per RoI" << endreq;
      }else{
	msg() << MSG::DEBUG << "Attaching Muid Comb Full Tracks ... "  << endreq;
	hltStatus = attachFeature(TEout, combinedTracks, "MuonCombEF");
	if(hltStatus!=HLT::OK) msg() << MSG::DEBUG << "Full Track container attached to TE" << endreq;
      }
    } else  msg() << MSG::DEBUG 
	    << "NO idtrack Offline switch-on could not find MuonIdentification Comb Track container" 
	    << endreq;
  }

  if(m_makeESD||m_makeAOD){
    
    std::string mooKey      = "TrigMooreTracks";
    std::string muidExtrKey = "TrigMuidExtrTracksnoSeed";
    std::string muidCombKey = "TrigMuidCombTracksnoSeed";
    
    if(m_roi_num==1) {
      if(m_moore){
	m_allMooreTracks    = new MooTrackContainer();
	status = p_SGevent->record(m_allMooreTracks, mooKey);
	if (status.isFailure()){
	  msg() << MSG::ERROR << "Record of Moore track into StoreGate failed" << endreq;

	  return  HLT::SG_ERROR;
	} else msg() << MSG::DEBUG << "Moore track saved in "<< mooKey << endreq;
      }
      
      if(m_muid){
	m_allMuidExtrTracks = new MuidTrackContainer();        
	status = p_SGevent->record(m_allMuidExtrTracks, muidExtrKey);
	if (status.isFailure()){
	  msg() << MSG::ERROR << "Record of Muid extrapolated track into StoreGate failed" << endreq;
 
	  return  HLT::SG_ERROR;
	} else msg() << MSG::DEBUG << "Muid extrapolated track saved in "
		     <<  muidExtrKey << endreq;
      }
      
      if(m_muidcomb){
	m_allMuidCombTracks = new MuidTrackContainer();
	status = p_SGevent->record(m_allMuidCombTracks, muidCombKey);
	if (status.isFailure()){
	  msg() << MSG::ERROR << "Record of Muid combined track into StoreGate failed" << endreq;

	  return  HLT::SG_ERROR;
	} else   msg() << MSG::DEBUG << "Muid combined track saved in " << 
		   muidCombKey << endreq;
      }
      
      if(!m_seed){
	msg() << MSG::DEBUG << "building ESD/AOD  for the Wrapper "  << endreq;
	if(track_container!=0){
	  for(MooTrackContainer::const_iterator it = track_container->begin();
	      it!=track_container->end(); ++it) 
	    m_allMooreTracks->push_back(new MooiPatTrack(**it));
	}
	if(standAloneTracks!=0){
	  for(MuidTrackContainer::const_iterator it1 = standAloneTracks->begin();
	      it1!=standAloneTracks->end(); ++it1){
	    MuidTrack * tmpTracksa = new MuidTrack(**it1);
	    m_allMuidExtrTracks->push_back(tmpTracksa);
	  }
	}
	if(combinedTracks!=0){
	  for(MuidTrackContainer::const_iterator it2 = combinedTracks->begin();
	      it2!=combinedTracks->end(); ++it2){
	    MuidTrack * tmpTrackcb = new MuidTrack(**it2);
	    m_allMuidCombTracks->push_back(tmpTrackcb);
	  }
	}
      }
    }
    
    if(m_seed){
      msg() << MSG::DEBUG << "building ESD/AOD for the seeded "  << endreq;
      if(m_moore){
	if(track_container!=0){
	  msg() << MSG::DEBUG << "Preparing Moore Container for ESD "  << endreq;
	  for(MooTrackContainer::const_iterator mooit = track_container->begin();
	      mooit!=track_container->end(); ++mooit) {
	    m_allMooreTracks->push_back(new MooiPatTrack(*(*mooit)));
	  }
	}
      }
    
      
      if(m_muid){
	if(standAloneTracks!=0){
	  msg() << MSG::DEBUG << "Preparing Muid SA Container for ESD "  << endreq;
	  for(MuidTrackContainer::const_iterator muit = standAloneTracks->begin();
	      muit!=standAloneTracks->end(); ++muit){
	    m_allMuidExtrTracks->push_back(new MuidTrack(*(*muit)));
	  }
	}
      }
      
      
      if(m_muidcomb){
	if(combinedTracks!=0){
	  msg() << MSG::DEBUG << "Preparing Muid CB Container for ESD "  << endreq;
	  for(MuidTrackContainer::const_iterator cbit = combinedTracks->begin();
	      cbit!=combinedTracks->end(); ++cbit){
	    m_allMuidCombTracks->push_back(new MuidTrack(*(*cbit)));
	  }
	}
      }
    }


    if (m_print_level>0){    
      if(m_moore){
	const MooTrackContainer * testMooreTracks;
	if (StatusCode::SUCCESS 
	    != p_SGevent->retrieve(testMooreTracks,mooKey) ) {
	  msg() << MSG::ERROR 
		<< "Could not find Moore Track container" 
		<< endreq;
	  //return( StatusCode::FAILURE);
	}  else msg() << MSG::DEBUG << "Moore tracks found with size " 
		      <<  testMooreTracks->size() << endreq;
      }
      if(m_muid){
	const MuidTrackContainer * testMuidsaTracks;
	if (StatusCode::SUCCESS 
	    != p_SGevent->retrieve(testMuidsaTracks,muidExtrKey) ) {
	  msg() << MSG::ERROR 
		<< "Could not find MuonIdentification Extr Track container" 
		<< endreq;
	  //return( StatusCode::FAILURE);
	}  else msg() << MSG::DEBUG << "Muid Extr tracks found with size " 
		      <<  testMuidsaTracks->size() << endreq;
      }
      if(m_muidcomb){
	const MuidTrackContainer * testMuidTracks;
	if (StatusCode::SUCCESS 
	    != p_SGevent->retrieve(testMuidTracks,muidCombKey) ) {
	  msg() << MSG::ERROR 
		<< "Could not find MuonIdentification Track container" 
		<< endreq;
	  //return( StatusCode::FAILURE);
	}  else msg() << MSG::DEBUG << "Muid tracks found with size " 
		      <<  testMuidTracks->size() << endreq;
      }
    }
  
  }

  if(m_moore){
    if (track_container){
	m_track_num += track_container->size();
	if(m_doTimers) m_tTotal->propVal(track_container->size() );
    }
  }
  
  //Let's suppose Moore always validates the sequence...
  TEout->setActiveState(true);
  if(m_doTimers) m_tTotal->stop();
  if (m_chronotest) chronosvc->chronoStop("All Algos");
  
  if (m_doAANtuples || m_doNtuples) {
    /////////////////////Fill Ntuples ///////////////////////////
    //FILL RECOMUONROI NTUPLE

    
    msg() << MSG::DEBUG << " Filling ntuples ....  " << endreq;

    
    //    if (m_seedFromLvl1 || m_seedFromLvl2 || m_moore || m_muid || m_muidcomb) {
    //      if(m_TrigMooreAANTuple->cleanBlocks(m_seedFromLvl1,m_seedFromLvl2,m_moore,m_muid,m_muidcomb).isFailure())
    //	msg() << MSG::WARNING << "cleanBlocks beginRun failed" << endreq;
    //}

    if(m_moore){
      if(m_seed&&m_seedFromLvl1){
	msg() << MSG::DEBUG << " LVL1 ntuples ....  " << endreq;
	if (m_doAANtuples) {status = m_TrigMooreAANTuple->execLVL1Block(muonRoIl1,m_roi_num);
	if (status.isFailure()) {
	  msg() << MSG::WARNING << " problems with lvl1 variables in the AAAntuple" << endreq;
	  //return status;
	}}
	if (m_doNtuples){ status = m_pTrigMooreNtuple->fillLVL1Block(muonRoIl1,m_roi_num);
	if (status.isFailure()) {
	  msg() << MSG::WARNING << " problems with lvl1 variables in the ntuple" << endreq;
	  //return status;
	}}
      }else if (m_seed&&m_seedFromLvl2){
	msg() << MSG::DEBUG << " LVL2 ntuples ....  " << endreq;
	if (m_doAANtuples){ status = m_TrigMooreAANTuple->execLVL2Block(muonRoIL2,m_roi_num);
	if (status.isFailure()) {
	  msg() << MSG::WARNING << " problems with AA lvl2 variables in the ntuple" << endreq;
	  //return status;
	}}
	if (m_doNtuples){ status = m_pTrigMooreNtuple->fillLVL2Block(muonRoIL2,m_roi_num);
	if (status.isFailure()) {
	  msg() << MSG::WARNING << " problems with lvl2 variables in the ntuple" << endreq;
	  //return status;
	}}
      }
      
      std::string key = "MDT_DIGITS";
      const MdtDigitContainer * mdt_container;
      status = p_SGevent->retrieve(mdt_container,key);
      if (status.isFailure()) {
	msg() << MSG::ERROR << " Cannot retrieve MDT Digit Container " << endreq;
 
	return  HLT::SG_ERROR;
      }
      
      int mdt_count = 0;
      // Loop on mdt collections
      for (MdtDigitContainer::const_iterator c = mdt_container->begin(); c != mdt_container->end(); ++c) {
	mdt_count += (*c)->size();
      }
      
      if (m_doTimers) m_tMakeRoad->propVal(mdt_count);  
      
      key = "RPC_DIGITS";
      const RpcDigitContainer* rpc_container;
      status = p_SGevent->retrieve(rpc_container,key);
      if (status.isFailure()) {
	msg() << MSG::ERROR << " Cannot retrieve RPC Digit Container " << endreq;

	return  HLT::SG_ERROR;
      }
      
      int rpc_count = 0;
      // Loop on collections
      for (RpcDigitContainer::const_iterator c = rpc_container->begin(); c != rpc_container->end(); ++c) 
	rpc_count += (*c)->size();
      if (m_doTimers) m_tMakeTracks->propVal(rpc_count );
      
      
      key = "TGC_DIGITS";
      const TgcDigitContainer* tgc_container;
      status = p_SGevent->retrieve(tgc_container,key);
      if (status.isFailure()) {
	msg() << MSG::ERROR << " Cannot retrieve TGC Digit Container " << endreq;

	return  HLT::SG_ERROR;
      }
      
      int tgc_count = 0;
      // Loop on collections
      for (TgcDigitContainer::const_iterator c = tgc_container->begin(); c != tgc_container->end(); ++c) 
	tgc_count += (*c)->size();
      if (m_doTimers) m_tMuidSA->propVal(tgc_count );
      //msg() << MSG::VERBOSE << " MOORE ntuples ....  " << endreq;
      msg() << MSG::DEBUG << " MOORE ntuples ....  " << endreq;
      if (m_doAANtuples) status = m_TrigMooreAANTuple->execMOOREBlock(track_container,m_track_num,m_roi_num);
      if (status.isFailure()) {
        msg() << MSG::WARNING << " problems with AA-Moore variables in the ntuple" << endreq;
        //return status;
      }
      if (m_doNtuples) status = m_pTrigMooreNtuple->fillMOOREBlock(track_container,m_track_num,m_roi_num);
      if (status.isFailure()) {
	msg() << MSG::WARNING << " problems with MOORE variables in the ntuple" << endreq;
	//return status;
      }
    } // m_moore
    
    if(m_muid){
      if (standAloneTracks)
	if (standAloneTracks->size() > 0 ) {
	  m_muidtrack_num += standAloneTracks->size();
	}
      msg() << MSG::DEBUG << " MUID STANDALONE ntuples ....  " <<standAloneTracks << endreq;
      
      if (m_doAANtuples) {status = m_TrigMooreAANTuple->execMUIDBlock(standAloneTracks,m_muidtrack_num, m_roi_num);
      if (status.isFailure()) {
        msg() << MSG::WARNING << " problems with AA MUID variables in the ntuple" << endreq;
        //return status;
      }}
      if (m_doNtuples) {status = m_pTrigMooreNtuple->fillMUIDBlock(standAloneTracks,m_muidtrack_num, m_roi_num);
      if (status.isFailure()) {
	msg() << MSG::WARNING << " problems with MUID variables in the ntuple" << endreq;
	//return status;
      }}
    } // m_muid
    
    if (m_muidcomb){
      if(combinedTracks){
	if (combinedTracks->size() > 0 ) {
	  m_muidcbtrack_num += combinedTracks->size();
	}
	msg() << MSG::DEBUG << " MUID COMB ntuples ....  " << endreq;
	if (m_doAANtuples) {status = m_TrigMooreAANTuple->execMUIDCBBlock(combinedTracks,m_muidcbtrack_num, m_roi_num);
	if (status.isFailure()) {
	  msg() << MSG::WARNING << " problems with MUIDCB AA  variables in the ntuple" << endreq;
	  //return status;
	}}
      }
      
      if (m_doNtuples){ status = m_pTrigMooreNtuple->fillMUIDCBBlock(combinedTracks,m_muidcbtrack_num, m_roi_num);
      if (status.isFailure()) {
	msg() << MSG::WARNING << " problems with MUIDCB variables in the ntuple" << endreq;
	//return status;
      }}
    } // m_muidcomb
    
    if (m_doNtuples) {
      status = ntupleSvc()->writeRecord("/NTUPLES/FILE1/CBNT"+m_ntupleID);
      if (status.isFailure()) {
        msg() << MSG::WARNING << " problems with write record in the ntuple" << endreq;
        //return status;
      }
    }
  }
  if (status.isFailure()) {
    msg() << MSG::WARNING << " HLT::OK, but status.isFailure()" << endreq;
    //return status;
  }
  
// ******** new *****
  ResidualsMOORE(track_container);
// *******************  
  return HLT::OK; 
}


// ********* new

//__________________________________________________________________________
void MooHLTAlgo::ResidualsMOORE(const MooTrackContainer * track_container)
{
  mdt_res.clear();
  csc_eta_res.clear();
  csc_phi_res.clear();
  rpc_eta_res.clear();
  rpc_phi_res.clear();
  tgc_eta_res.clear();
  tgc_phi_res.clear();
  
  if (track_container){ 
    if (track_container->size() > 0 ){
      msg() << MSG::DEBUG << "MOORE Hit Residual" << endreq;
      // iterate on tracks
      for (MooTrackContainer::const_iterator tr = track_container->begin(); tr != track_container->end(); ++tr){
      // iterate on hits
      for (HitList::hit_citerator h = (*tr)->hit_list_begin(); h != (*tr)->hit_list_end(); ++h){
      
        // gives the hit technology
        if ( p_MdtIdHelper->is_mdt((*h)->identifier()) ) {
       	  
		  mdt_res.push_back( (*h)->residual() );
 		  
        
		} else if ( p_MdtIdHelper->is_csc((*h)->identifier()) ) {
          if ( fabs((*h)->sin_stereo()) > 0.5 ) {
      
		  csc_eta_res.push_back( (*h)->residual() );
		   
          
		}
          else {
        
		  csc_phi_res.push_back( (*h)->residual() );
		
          
		  }
        } else if ( p_MdtIdHelper->is_rpc((*h)->identifier()) ) {
          if ( fabs((*h)->sin_stereo()) > 0.5 ) {
       	  
		  rpc_eta_res.push_back( (*h)->residual() );
		          
		  }
          else {
          
		  rpc_phi_res.push_back( (*h)->residual() );
		  
          
		  }
        } else if ( p_MdtIdHelper->is_tgc((*h)->identifier()) ) {
          if ( fabs((*h)->sin_stereo()) > 0.5 ) {
		  
          tgc_eta_res.push_back( (*h)->residual() );
		  
          
		  }
          else {
          
		  tgc_phi_res.push_back( (*h)->residual() );
		
		  
		  }
        }
      }
      }
//	  msg() << MSG::DEBUG << "mdt hit residual (size)     = " << mdt_res.size     << endreq; 
//      msg() << MSG::DEBUG << "csc_eta hit residual (size) = " << csc_eta_res.size << endreq;
//      msg() << MSG::DEBUG << "csc_phi hit residual (size) = " << csc_phi_res.size << endreq;
//      msg() << MSG::DEBUG << "rpc_eta hit residual (size) = " << rpc_eta_res.size << endreq; 
//      msg() << MSG::DEBUG << "rpc_phi hit residual (size) = " << rpc_phi_res.size << endreq;
//      msg() << MSG::DEBUG << "tgc_eta hit residual (size) = " << tgc_eta_res.size << endreq;
//	  msg() << MSG::DEBUG << "tgc_phi hit residual (size) = " << tgc_phi_res.size << endreq;
    }
  }
}


// ****************


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
HLT::ErrorCode
MooHLTAlgo:: hltFinalize(){

if(msgLvl() <= MSG::DEBUG)
   msg() << MSG::DEBUG << "Finalizing MooHLTAlgo" << endreq;

return  HLT::OK;
}

void 
MooHLTAlgo::executeAlgs(Algorithm * mooAlg){

  MsgStream log(messageService(), name());
  StatusCode status = StatusCode::SUCCESS;

  std::string tmp_roi = m_roi; 
  if(m_cosmic)  m_roi = tmp_roi + "_CS";
  
  status= mooAlg->setProperty(StringProperty("RoInumber",m_roi));
  if(!status.isSuccess()) {
    log << MSG::VERBOSE << "Unable to set properties" << endreq;
  }
  std::string name = mooAlg->name();

  if( mooAlg->name() == "MooRoadMakerEF" ) {
    status=mooAlg->setProperty(StringProperty("RpcPrdLocation",""));
    status=mooAlg->setProperty(StringProperty("TgcPrdLocation",""));
    if (status.isFailure()) log << MSG::WARNING
				<< "Unable to set property Algo"
				<< endreq;
  }

  if( mooAlg->name() == "MooMakeTracksEF" ) {
    status=mooAlg->setProperty(StringProperty("TrackLocation","MooreTracks"));
    status=mooAlg->setProperty(StringProperty("RpcPrdLocation",""));
    status=mooAlg->setProperty(StringProperty("TgcPrdLocation",""));
    if (status.isFailure()) log << MSG::WARNING
				<< "Unable to set property Algo"
				<< endreq;
  }

  if(name == "MooMakePhiPatternSeededEF" || name == "MooMakePhiPatternSeededEF_CS") {
    MooMakePhiPatternSeeded *  tmpAlg = static_cast<MooMakePhiPatternSeeded *>(mooAlg);
    tmpAlg->setMuonRoI(m_mooRoI);  
    status=tmpAlg->execute();
    if (status.isFailure()) log << MSG::WARNING
				<< "Unable to exec algo"
				<< endreq;
  } else if (name == "MooMakeRzPatternSeededEF" || name== "MooMakeRzPatternSeededEF_CS") {
      MooMakeRzPatternSeeded *  tmpAlg = static_cast<MooMakeRzPatternSeeded *>(mooAlg);
     tmpAlg->setMuonRoI(m_mooRoI);  
     status=tmpAlg->execute();
    if (status.isFailure()) log << MSG::VERBOSE
				<< "Unable to exec algo"
				<< endreq;

  } else if(name == "MooMakePhiSegmentSeededEF") {
    MooMakePhiSegmentSeeded *  tmpAlg = static_cast<MooMakePhiSegmentSeeded *>(mooAlg);
    tmpAlg->setMuonRoI(m_mooRoI);  
    status=tmpAlg->execute();
    if (status.isFailure()) log << MSG::VERBOSE
				<< "Unable to exec algo"
				<< endreq;
  } else if(name == "MooMakeCrudeRZSegmentsSeededEF") {
    MooMakeCrudeRZSegmentsSeeded *  tmpAlg = static_cast<MooMakeCrudeRZSegmentsSeeded *>(mooAlg);
    tmpAlg->setMuonRoI(m_mooRoI);  
    status=tmpAlg->execute();
    if (status.isFailure()) log << MSG::VERBOSE
				<< "Unable to exec algo"
				<< endreq;

  } else {
    status=mooAlg->execute();
    if (status.isFailure()) log << MSG::VERBOSE
				<< "Unable to exec algo"
                                << endreq;
  }
  
  m_roi = tmp_roi;
}


bool
MooHLTAlgo::removeCloseRoIs(MooRoI * roi){

  std::vector<IdentifierHash> mdtHashs = *roi->getMdtHashIds();
  std::vector<IdentifierHash> rpcHashs = *roi->getRpcHashIds();
  std::vector<IdentifierHash> tgcHashs = *roi->getTgcHashIds();
  
  sort( mdtHashs.begin(), mdtHashs.end() );
  sort( rpcHashs.begin(), rpcHashs.end() );
  sort( tgcHashs.begin(), tgcHashs.end() );


  if(m_RoIs.size()==0) {
    m_RoIs.push_back(roi);
    return false;
  }
  
  for(std::vector<MooRoI *>::const_iterator roiIt = m_RoIs.begin();
      roiIt != m_RoIs.end(); ++roiIt){
    
    std::vector<IdentifierHash> rpcHashsIt = *( (*roiIt)->getRpcHashIds() ) ;
    std::vector<IdentifierHash> tgcHashsIt = *( (*roiIt)->getTgcHashIds() ) ;
    std::vector<IdentifierHash> mdtHashsIt = *( (*roiIt)->getMdtHashIds() ) ;
    
    sort( mdtHashsIt.begin(), mdtHashsIt.end() );
    sort( rpcHashsIt.begin(), rpcHashsIt.end() );
    sort( tgcHashsIt.begin(), tgcHashsIt.end() );

    //    for(std::vector<int>::const_iterator it=mdtHashsIt.begin() ;it !=mdtHashsIt.end(); ++it ){
    //std::cout << *it << std::endl;
    //}
    
    
    if ( includes(rpcHashsIt.begin(), rpcHashsIt.end(), rpcHashs.begin(), rpcHashs.end()) 
	 && 
	 includes(mdtHashsIt.begin(), mdtHashsIt.end(), mdtHashs.begin(), mdtHashs.end())
	 &&
	 includes(tgcHashsIt.begin(), tgcHashsIt.end(), tgcHashs.begin(), tgcHashs.end())
	 ){
      //the processed RoiI is already included in one of those in the RoI list
      delete m_mooRoI;
      return true;
    }

      
    if (includes(rpcHashs.begin(), rpcHashs.end(), rpcHashsIt.begin(), rpcHashsIt.end()) 
	&& 
	includes(mdtHashs.begin(), mdtHashs.end(), mdtHashsIt.begin(), mdtHashsIt.end()) 
	&&
	includes(tgcHashs.begin(), tgcHashs.end(), tgcHashsIt.begin(), tgcHashsIt.end()) 
	){
      //the processed RoiI includes one in the RoI list
      //     m_RoIs.erase(*roiIt);
      m_RoIs.push_back(roi);
      return true;
    }
  }
  
  m_RoIs.push_back(roi);
  return false;
}

void 
MooHLTAlgo::handle(const Incident &inc) {
  if ( inc.type() == "EndEvent"){

    MsgStream log( messageService(), name() );
    int outputLevel = msgSvc()->outputLevel( name() );
    if(outputLevel <= MSG::DEBUG)
      log << MSG::DEBUG << "=====> End of Event" << endreq;
    
    m_roi_num = 0;
    m_track_num = 0;
    m_muidtrack_num = 0;
    m_muidcbtrack_num = 0;
    std::vector<MooRoI *>::const_iterator roiIt =  m_RoIs.begin();
    // clear all elements from the array
    for(; roiIt <  m_RoIs.end(); roiIt++)
      delete *roiIt;    // free the element from memory
    // finally, clear all elements from the array
    m_RoIs.clear();

    if (!m_doAANtuples) return;
    if (m_seedFromLvl1 || m_seedFromLvl2 || m_moore || m_muid || m_muidcomb) {
      if(outputLevel <= MSG::DEBUG)
	log << MSG::DEBUG << "calling cleanBlocks" << endreq;
      if(m_TrigMooreAANTuple->cleanBlocks(m_seedFromLvl1,m_seedFromLvl2,m_moore,m_muid,m_muidcomb).isFailure())
	log << MSG::WARNING << "end of event not correctly handled" << endreq;
    }

  }
}

CERN Central CVS service
ViewVC Help
Powered by ViewVC 1.0.4