CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_9_patch3/src/L1Trigger/RPCTechnicalTrigger/src/RPCTechnicalTrigger.cc

Go to the documentation of this file.
00001 // $Id: 
00002 
00003 //-----------------------------------------------------------------------------
00004 // Implementation file for class : RPCTechnicalTrigger
00005 //
00006 // 2008-10-15 : Andres Osorio
00007 //-----------------------------------------------------------------------------
00008 
00009 //=============================================================================
00010 // Standard constructor, initializes variables
00011 //=============================================================================
00012 
00013 // Include files
00014 
00015 // local
00016 #include "L1Trigger/RPCTechnicalTrigger/interface/RPCTechnicalTrigger.h"
00017 #include "L1Trigger/RPCTechnicalTrigger/interface/ProcessTestSignal.h"
00018 #include "L1Trigger/RPCTechnicalTrigger/interface/RBCProcessRPCDigis.h"
00019 #include "L1Trigger/RPCTechnicalTrigger/interface/RBCProcessRPCSimDigis.h"
00020 
00021 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00022 #include "FWCore/MessageLogger/interface/MessageDrop.h"
00023 
00024 //=============================================================================
00025 // Standard constructor, initializes variables
00026 //=============================================================================
00027 
00028 RPCTechnicalTrigger::RPCTechnicalTrigger(const edm::ParameterSet& iConfig) {
00029   
00030   //...........................................................................
00031   
00032   std::string configFile  = iConfig.getParameter<std::string>("ConfigFile");
00033   m_verbosity             = iConfig.getUntrackedParameter<int>("Verbosity", 0);
00034   m_rpcDigiLabel          = iConfig.getParameter<edm::InputTag>("RPCDigiLabel");
00035   m_ttBits                = iConfig.getParameter< std::vector<unsigned> >("BitNumbers");
00036   m_ttNames               = iConfig.getParameter< std::vector<std::string> >("BitNames");
00037   m_useEventSetup         = iConfig.getUntrackedParameter<int>("UseEventSetup", 0);
00038   m_useRPCSimLink         = iConfig.getUntrackedParameter<int>("UseRPCSimLink", 0);
00039   m_rpcSimLinkInstance    = iConfig.getParameter<edm::InputTag>("RPCSimLinkInstance");
00040 
00041   edm::FileInPath f1("L1Trigger/RPCTechnicalTrigger/data/" + configFile);
00042   m_configFile = f1.fullPath();
00043 
00044   if ( m_verbosity ) {
00045     LogTrace("RPCTechnicalTrigger")
00046       << m_rpcDigiLabel << '\n'
00047       << std::endl;
00048 
00049     LogTrace("RPCTechnicalTrigger")
00050       << "\nConfiguration file used for UseEventSetup = 0 \n" << m_configFile << '\n'
00051       << std::endl;
00052   }
00053   
00054   //...........................................................................
00055   //... There are three Technical Trigger Units Boards: 1 can handle 2 Wheels
00056   //... n_Wheels sets the number of wheels attached to board with index boardIndex
00057   
00058   m_boardIndex[0] = 1;
00059   m_boardIndex[1] = 2;
00060   m_boardIndex[2] = 3;
00061   
00062   m_nWheels[0]    = 2;
00063   m_nWheels[1]    = 1;
00064   m_nWheels[2]    = 2;
00065   
00066   m_ttu[0] = new TTUEmulator( m_boardIndex[0] , m_nWheels[0] );
00067   m_ttu[1] = new TTUEmulator( m_boardIndex[1] , m_nWheels[1] );
00068   m_ttu[2] = new TTUEmulator( m_boardIndex[2] , m_nWheels[2] );
00069 
00070   //... This is second line that delivers in parallel a second trigger
00071   m_ttuRbcLine[0] = new TTUEmulator( m_boardIndex[0] , m_nWheels[0] );
00072   m_ttuRbcLine[1] = new TTUEmulator( m_boardIndex[1] , m_nWheels[1] );
00073   m_ttuRbcLine[2] = new TTUEmulator( m_boardIndex[2] , m_nWheels[2] );
00074   
00075   m_WheelTtu[-2] = 3;
00076   m_WheelTtu[-1] = 3;
00077   m_WheelTtu[0 ] = 2;
00078   m_WheelTtu[1 ] = 1;
00079   m_WheelTtu[2 ] = 1;
00080   
00081   //...........................................................................
00082   //For the pointing Logic: declare here the first sector of each quadrant
00083   //
00084   m_quadrants.push_back(2);
00085   m_quadrants.push_back(3);
00086   m_quadrants.push_back(4);
00087   m_quadrants.push_back(5);
00088   m_quadrants.push_back(6);
00089   m_quadrants.push_back(7);
00090   m_quadrants.push_back(8);
00091   m_quadrants.push_back(9);
00092   m_quadrants.push_back(10);
00093   m_quadrants.push_back(11);
00094 
00095   //...........................................................................
00096   
00097   m_ievt = 0;
00098   m_cand = 0;
00099   m_maxTtuBoards = 3;
00100   m_maxBits = 5;
00101   m_hasConfig = false;
00102   m_readConfig = NULL;
00103   produces<L1GtTechnicalTriggerRecord>();
00104   
00105 }
00106 
00107 
00108 RPCTechnicalTrigger::~RPCTechnicalTrigger()
00109 {
00110   
00111   LogDebug("RPCTechnicalTrigger") << "RPCTechnicalTrigger: object starts deletion" << std::endl;
00112 
00113   if ( m_hasConfig ) {
00114     
00115     delete m_ttu[0];
00116     delete m_ttu[1];
00117     delete m_ttu[2];
00118     
00119     delete m_ttuRbcLine[0];
00120     delete m_ttuRbcLine[1];
00121     delete m_ttuRbcLine[2];
00122     
00123     if ( m_readConfig )
00124       delete m_readConfig;
00125     
00126   }
00127   
00128   m_WheelTtu.clear();
00129     
00130   LogDebug("RPCTechnicalTrigger") << "RPCTechnicalTrigger: object deleted" << '\n';
00131   
00132 }
00133 
00134 //=============================================================================
00135 void RPCTechnicalTrigger::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
00136 
00137 
00138   bool status(false);
00139   
00140   edm::Handle<RPCDigiCollection> pIn;
00141   
00142   edm::Handle<edm::DetSetVector<RPCDigiSimLink> > simIn;
00143   
00144   std::auto_ptr<L1GtTechnicalTriggerRecord> output(new L1GtTechnicalTriggerRecord());
00145   
00146   if ( m_useRPCSimLink == 0 ) {
00147 
00148     iEvent.getByLabel(m_rpcDigiLabel, pIn);
00149     if ( ! pIn.isValid() ) {
00150       edm::LogError("RPCTechnicalTrigger") << "can't find RPCDigiCollection with label: " 
00151                                            << m_rpcDigiLabel << '\n';
00152       iEvent.put(output);
00153       return;
00154     }
00155     m_signal  = dynamic_cast<ProcessInputSignal*>(new RBCProcessRPCDigis( m_rpcGeometry, pIn ));
00156     
00157   } else {
00158     
00159     iEvent.getByLabel("simMuonRPCDigis", "RPCDigiSimLink", simIn);
00160     
00161     if ( ! simIn.isValid() ) {
00162       edm::LogError("RPCTechnicalTrigger") << "can't find RPCDigiCollection with label: " 
00163                                            << m_rpcDigiLabel << '\n';
00164       iEvent.put(output);
00165       return;
00166     }
00167     m_signal  = dynamic_cast<ProcessInputSignal*>(new RBCProcessRPCSimDigis( m_rpcGeometry, simIn ));
00168   }
00169   
00170   LogDebug("RPCTechnicalTrigger") << "signal object created" << '\n';
00171   
00172   if ( ! m_hasConfig ) {
00173     edm::LogError("RPCTechnicalTrigger") << "cannot read hardware configuration \n";
00174     iEvent.put(output);
00175     return;
00176   }
00177   
00178   status = m_signal->next();
00179   
00180   if ( !status)  { 
00181     delete m_signal;
00182     iEvent.put(output);
00183     return;
00184   }
00185   
00186   m_input = m_signal->retrievedata();
00187   
00188   std::vector<L1GtTechnicalTrigger> ttVec( m_ttBits.size() );
00189   
00190   //. distribute data to different TTU emulator instances and process it
00191   
00192   m_triggerbits.reset();
00193   
00194   std::vector<TTUEmulator::TriggerResponse*>::const_iterator outItr;
00195   
00196   for(int k=0; k < m_maxTtuBoards; ++k) {
00197     
00198     m_ttu[k]->processTtu( m_input );
00199     
00200     //work out Pointing Logic to Tracker
00201     for( m_firstSector = m_quadrants.begin(); m_firstSector != m_quadrants.end(); ++m_firstSector)
00202       m_ttuRbcLine[k]->processTtu( m_input , (*m_firstSector) );
00203     
00204     //...for trigger 1
00205     for( outItr  = m_ttu[k]->m_triggerBxVec.begin(); outItr != m_ttu[k]->m_triggerBxVec.end(); ++outItr )
00206       m_serializedInfoLine1.push_back( new TTUResults( k, (*outItr)->m_bx, (*outItr)->m_trigger[0], (*outItr)->m_trigger[1] ) );
00207     m_ttu[k]->clearTriggerResponse();
00208     
00209     //...for trigger 2
00210     for( outItr  = m_ttuRbcLine[k]->m_triggerBxVec.begin(); outItr != m_ttuRbcLine[k]->m_triggerBxVec.end(); ++outItr )
00211       m_serializedInfoLine2.push_back( new TTUResults( k, 
00212                                                        (*outItr)->m_bx, 
00213                                                        (*outItr)->m_trigger[0], 
00214                                                        (*outItr)->m_trigger[1], 
00215                                                        (*outItr)->m_wedge ) );
00216     
00217     m_ttuRbcLine[k]->clearTriggerResponse();
00218     
00219   }
00220   
00221   //.. write results to technical trigger bits
00222   int bx(0);
00223   int infoSize(0);
00224   
00225   infoSize = m_serializedInfoLine1.size();
00226 
00227   std::vector<RPCTechnicalTrigger::TTUResults*>::const_iterator ttuItr;
00228   
00229   std::sort( m_serializedInfoLine1.begin(), m_serializedInfoLine1.end(), sortByBx() );
00230   
00231   for( ttuItr = m_serializedInfoLine1.begin(); ttuItr != m_serializedInfoLine1.end(); ++ttuItr ) {
00232     if ( m_verbosity && abs( (*ttuItr)->m_bx ) <= 1 ) 
00233       std::cout << "RPCTechnicalTrigger> " 
00234                 << (*ttuItr)->m_ttuidx << '\t'
00235                 << (*ttuItr)->m_bx << '\t'
00236                 << (*ttuItr)->m_trigWheel1 << '\t'
00237                 << (*ttuItr)->m_trigWheel2 << '\n';
00238   }
00239   
00240   bool has_bx0 = false;
00241   
00242   for(int k = 0; k < infoSize; k+=m_maxTtuBoards) {
00243     
00244     bx = m_serializedInfoLine1[k]->m_bx;
00245     
00246     if ( bx == 0 ) {
00247       
00248       m_triggerbits.set(0, m_serializedInfoLine1[k]->m_trigWheel2);
00249       m_triggerbits.set(1, m_serializedInfoLine1[k]->m_trigWheel1);
00250       m_triggerbits.set(2, m_serializedInfoLine1[k+1]->m_trigWheel1);
00251       m_triggerbits.set(3, m_serializedInfoLine1[k+2]->m_trigWheel1);
00252       m_triggerbits.set(4, m_serializedInfoLine1[k+2]->m_trigWheel2);
00253       
00254       bool five_wheels_OR = m_triggerbits.any();
00255       
00256       ttVec.at(0)=L1GtTechnicalTrigger(m_ttNames.at(0), m_ttBits.at(0), bx, five_wheels_OR ) ;   // bit 24 = Or 5 wheels in TTU mode
00257       ttVec.at(2)=L1GtTechnicalTrigger(m_ttNames.at(2), m_ttBits.at(2), bx, m_triggerbits[0] ) ; // bit 26 
00258       ttVec.at(3)=L1GtTechnicalTrigger(m_ttNames.at(3), m_ttBits.at(3), bx, m_triggerbits[1] ) ; // bit 27 
00259       ttVec.at(4)=L1GtTechnicalTrigger(m_ttNames.at(4), m_ttBits.at(4), bx, m_triggerbits[2] ) ; // bit 28 
00260       ttVec.at(5)=L1GtTechnicalTrigger(m_ttNames.at(5), m_ttBits.at(5), bx, m_triggerbits[3] ) ; // bit 29
00261       ttVec.at(6)=L1GtTechnicalTrigger(m_ttNames.at(6), m_ttBits.at(6), bx, m_triggerbits[4] ) ; // bit 30
00262       
00263       m_triggerbits.reset();
00264       
00265       has_bx0 = true;
00266       
00267       break;
00268       
00269     } else continue;
00270     
00271   }
00272   
00273   infoSize = m_serializedInfoLine2.size();
00274   
00275   std::sort( m_serializedInfoLine2.begin(), m_serializedInfoLine2.end(), sortByBx() );
00276   
00277   for( ttuItr = m_serializedInfoLine2.begin(); ttuItr != m_serializedInfoLine2.end(); ++ttuItr ) {
00278     if ( m_verbosity && abs ( (*ttuItr)->m_bx ) <= 1 )
00279       std::cout << "RPCTechnicalTrigger> " 
00280                 << (*ttuItr)->m_ttuidx << '\t'
00281                 << (*ttuItr)->m_bx << '\t'
00282                 << (*ttuItr)->m_trigWheel1 << '\t'
00283                 << (*ttuItr)->m_trigWheel2 << '\t'
00284                 << (*ttuItr)->m_wedge << '\n';
00285   }
00286   
00287   infoSize = convertToMap( m_serializedInfoLine2 );
00288   
00289   std::bitset<8> triggerCoincidence;
00290   triggerCoincidence.reset();
00291   
00292   // searchCoincidence( W-2 , W0 )
00293   bool result = searchCoincidence( -2, 0 );
00294   triggerCoincidence.set(0, result );
00295   
00296   // searchCoincidence( W-2 , W+1 )
00297   result = searchCoincidence( -2, 1 );
00298   triggerCoincidence.set(1, result );
00299   
00300   // searchCoincidence( W-1 , W0  )
00301   result = searchCoincidence( -1, 0 );
00302   triggerCoincidence.set(2, result );
00303   
00304   // searchCoincidence( W-1 , W+1 )
00305   result = searchCoincidence( -1, 1 );
00306   triggerCoincidence.set(3, result );
00307   
00308   // searchCoincidence( W-1 , W+2 )
00309   result = searchCoincidence( -1, 2 );
00310   triggerCoincidence.set(4, result );
00311   
00312   // searchCoincidence( W0  , W0  )
00313   result = searchCoincidence( 0 , 0 );
00314   triggerCoincidence.set(5, result );
00315   
00316   // searchCoincidence( W+1 , W0  )
00317   result = searchCoincidence( 1, 0 );
00318   triggerCoincidence.set(6, result );
00319   
00320   // searchCoincidence( W+2 , W0  ) 
00321   result = searchCoincidence( 2, 0 );
00322   triggerCoincidence.set(7, result );
00323   
00324   bool five_wheels_OR = triggerCoincidence.any();
00325 
00326   if ( m_verbosity ) std::cout << "RPCTechnicalTrigger> pointing trigger: " << five_wheels_OR << '\n';
00327   
00328   ttVec.at(1)=L1GtTechnicalTrigger(m_ttNames.at(1), m_ttBits.at(1), bx, five_wheels_OR ) ; // bit 25 = Or 5 wheels in RBC mode
00329   
00330   triggerCoincidence.reset();
00331   
00332   //...check that data appeared at bx=0
00333   
00334   if ( ! has_bx0 ) {
00335     iEvent.put(output);
00336     status = Reset();
00337     ++m_ievt;
00338     LogDebug("RPCTechnicalTrigger") << "RPCTechnicalTrigger> end of event loop" << std::endl;
00339     return;
00340     
00341   }
00342   
00343   output->setGtTechnicalTrigger(ttVec);    
00344   iEvent.put(output);
00345   
00346   //.... all done
00347   
00348   status = Reset();
00349   ++m_ievt;
00350   LogDebug("RPCTechnicalTrigger") << "RPCTechnicalTrigger> end of event loop" << std::endl;
00351   
00352 }
00353 
00354 bool RPCTechnicalTrigger::Reset()
00355 {
00356   
00357   m_input->clear();
00358   m_triggerbits.reset();
00359   std::vector<TTUResults*>::iterator itrRes;
00360   
00361   for( itrRes=m_serializedInfoLine1.begin(); itrRes!=m_serializedInfoLine1.end(); ++itrRes)
00362     delete (*itrRes);
00363   
00364   for( itrRes=m_serializedInfoLine2.begin(); itrRes!=m_serializedInfoLine2.end(); ++itrRes)
00365     delete (*itrRes);
00366   
00367   m_serializedInfoLine1.clear();
00368   m_serializedInfoLine2.clear();
00369   m_ttuResultsByQuadrant.clear();
00370   
00371   delete m_signal; 
00372   
00373   return true;
00374   
00375 }
00376 
00377 // ------------ method called once each job just before starting event loop  ------------
00378 void RPCTechnicalTrigger::beginRun(edm::Run& iRun, const edm::EventSetup& evtSetup)
00379 {
00380   LogDebug("RPCTechnicalTrigger") << "RPCTechnicalTrigger::beginRun> starts" << std::endl;
00381   
00382   //.   Set up RPC geometry
00383   
00384   evtSetup.get<MuonGeometryRecord>().get( m_rpcGeometry );
00385   
00386   //..  Get Board Specifications (hardware configuration)
00387   
00388   if ( m_useEventSetup >= 1 ) {
00389     
00390     edm::ESHandle<RBCBoardSpecs> pRBCSpecs;
00391     evtSetup.get<RBCBoardSpecsRcd>().get(pRBCSpecs);
00392 
00393     edm::ESHandle<TTUBoardSpecs> pTTUSpecs;
00394     evtSetup.get<TTUBoardSpecsRcd>().get(pTTUSpecs);
00395     
00396     if ( !pRBCSpecs.isValid() ||  !pTTUSpecs.isValid() ) {
00397       edm::LogError("RPCTechnicalTrigger") << "can't find RBC/TTU BoardSpecsRcd" << '\n';
00398       m_hasConfig = false;
00399     }
00400     else  {
00401       m_rbcspecs = pRBCSpecs.product();
00402       m_ttuspecs = pTTUSpecs.product();
00403       m_hasConfig = true;
00404     }
00405     
00406   } else {
00407     
00408     // read hardware configuration from file
00409     m_readConfig = new TTUConfigurator( m_configFile );
00410     
00411     if ( m_readConfig->m_hasConfig ) {
00412       m_readConfig->process();
00413       m_rbcspecs = m_readConfig->getRbcSpecs();
00414       m_ttuspecs = m_readConfig->getTtuSpecs();
00415       m_hasConfig = true;
00416     }
00417     
00418     else m_hasConfig = false;
00419     
00420   }
00421   
00422   if ( m_hasConfig ) {
00423     
00424     //... Initialize all
00425     
00426     for (int k=0; k < m_maxTtuBoards; ++k ) {
00427 
00428       m_ttu[k]->SetLineId ( 1 );
00429       m_ttuRbcLine[k]->SetLineId( 2 );
00430       
00431       m_ttu[k]->setSpecifications( m_ttuspecs, m_rbcspecs );
00432       m_ttuRbcLine[k]->setSpecifications( m_ttuspecs, m_rbcspecs );
00433       
00434       m_ttu[k]->initialise();
00435       m_ttuRbcLine[k]->initialise();
00436     }
00437   
00438   }
00439     
00440 }
00441 
00442 //
00443 int RPCTechnicalTrigger::convertToMap( const std::vector<TTUResults*> & ttuResults )
00444 {
00445   
00446   std::vector<TTUResults*>::const_iterator itr = ttuResults.begin();
00447   
00448   while ( itr != ttuResults.end() ) {
00449     
00450     if ( (*itr)->m_bx != 0 ) {
00451       ++itr;
00452       continue;
00453     }
00454     
00455     int key(0);
00456     key = 1000 * ( (*itr)->m_ttuidx + 1 ) + 1*(*itr)->m_wedge;
00457     m_ttuResultsByQuadrant[ key ] = (*itr);
00458     ++itr;
00459     
00460   }
00461   
00462   return m_ttuResultsByQuadrant.size();
00463     
00464 }
00465 
00466 //...RBC pointing logic to tracker bit 25: hardwired
00467 bool RPCTechnicalTrigger::searchCoincidence( int wheel1, int wheel2 )
00468 {
00469   
00470   std::map<int, TTUResults*>::iterator itr;
00471   bool topRight(false);
00472   bool botLeft(false);
00473   
00474   int indxW1 = m_WheelTtu[wheel1];
00475   int indxW2 = m_WheelTtu[wheel2];
00476   
00477   int k(0);
00478   int key(0);
00479   bool finalTrigger(false);
00480   int maxTopQuadrants = 4;
00481   
00482   //work out Pointing Logic to Tracker
00483   
00484   for( m_firstSector = m_quadrants.begin(); m_firstSector != m_quadrants.end(); ++m_firstSector) {
00485     
00486     key = 1000 * ( indxW1 ) + (*m_firstSector);
00487     
00488     itr = m_ttuResultsByQuadrant.find( key );
00489     if ( itr != m_ttuResultsByQuadrant.end() )
00490       topRight  =  (*itr).second->getTriggerForWheel(wheel1);
00491 
00492     //std::cout << "W1: " << wheel1 << " " << "sec: " << (*m_firstSector) << " dec: " << topRight << '\n';
00493     
00494     key = 1000 * ( indxW2 ) + (*m_firstSector) + 5;
00495     
00496     itr = m_ttuResultsByQuadrant.find( key );
00497     
00498     if ( itr != m_ttuResultsByQuadrant.end() )
00499       botLeft   = (*itr).second->getTriggerForWheel(wheel2);
00500     
00501     //std::cout << "W2: " << wheel2 << " " << "sec: " << (*m_firstSector) + 5 << " dec: " << botLeft << '\n';
00502     
00503     finalTrigger |= ( topRight && botLeft );
00504     
00505     ++k;
00506     
00507     if ( k > maxTopQuadrants)
00508       break;
00509         
00510   }
00511   
00512   //Try the opposite now
00513 
00514   k=0;
00515   
00516   for( m_firstSector = m_quadrants.begin(); m_firstSector != m_quadrants.end(); ++m_firstSector) {
00517     
00518     key = 1000 * ( indxW2 ) + (*m_firstSector);
00519     
00520     itr = m_ttuResultsByQuadrant.find( key );
00521     if ( itr != m_ttuResultsByQuadrant.end() )
00522       topRight  =  (*itr).second->getTriggerForWheel(wheel1);
00523 
00524     //std::cout << "W1: " << wheel1 << " " << "sec: " << (*m_firstSector) << " dec: " << topRight << '\n';
00525     
00526     key = 1000 * ( indxW1 ) + (*m_firstSector) + 5;
00527     
00528     itr = m_ttuResultsByQuadrant.find( key );
00529     
00530     if ( itr != m_ttuResultsByQuadrant.end() )
00531       botLeft   = (*itr).second->getTriggerForWheel(wheel2);
00532     
00533     //std::cout << "W2: " << wheel2 << " " << "sec: " << (*m_firstSector) + 5 << " dec: " << botLeft << '\n';
00534     
00535     finalTrigger |= ( topRight && botLeft );
00536     
00537     ++k;
00538     
00539     if ( k > maxTopQuadrants)
00540       break;
00541         
00542   }
00543   
00544   return finalTrigger;
00545   
00546 }
00547 
00548 // ------------ method called once each job just after ending the event loop  ------------
00549 
00550 void RPCTechnicalTrigger::endJob() 
00551 {
00552   
00553   LogDebug("RPCTechnicalTrigger") << "RPCTechnicalTrigger::endJob>" << std::endl;
00554   
00555 }
00556 
00557 void RPCTechnicalTrigger::printinfo()
00558 {
00559   
00560   LogDebug("RPCTechnicalTrigger") << "RPCTechnicalTrigger::Printing TTU emulators info>" << std::endl;
00561   
00562   for (int k=0; k < m_maxTtuBoards; ++k ) {
00563     m_ttu[k]->printinfo();
00564     m_ttuRbcLine[k]->printinfo();
00565   }
00566   
00567     
00568 }
00569 
00570 
00571 //define this as a plug-in
00572 DEFINE_FWK_MODULE(RPCTechnicalTrigger);