CMS 3D CMS Logo

RPCTechnicalTrigger.cc
Go to the documentation of this file.
1 // $Id:
2 
3 //-----------------------------------------------------------------------------
4 // Implementation file for class : RPCTechnicalTrigger
5 //
6 // 2008-10-15 : Andres Osorio
7 //-----------------------------------------------------------------------------
8 
9 //=============================================================================
10 // Standard constructor, initializes variables
11 //=============================================================================
12 
13 // Include files
14 
15 // local
20 
23 
24 //=============================================================================
25 // Standard constructor, initializes variables
26 //=============================================================================
27 
28 namespace {
29  //...........................................................................
30  //For the pointing Logic: declare here the first sector of each quadrant
31  //
32  constexpr std::array<int,10> s_quadrants = { {2,3,4,5,6,7,8,9,10,11} };
33 
34  //The wheelTtu is addressed using -2, -1, 0, 1, 2
35  constexpr unsigned int kWheelOffset = 2;
36  constexpr std::array<int, 5> wheelTtu = { {3,3,2,1,1} };
37 }
38 
40  m_verbosity{ iConfig.getUntrackedParameter<int>("Verbosity", 0)},
41  m_useEventSetup{iConfig.getUntrackedParameter<int>("UseEventSetup", 0)},
42  m_ttBits{iConfig.getParameter< std::vector<unsigned> >("BitNumbers")},
43  m_ttNames{iConfig.getParameter< std::vector<std::string> >("BitNames")},
44  m_rpcDigiLabel{iConfig.getParameter<edm::InputTag>("RPCDigiLabel")},
45  m_rpcDigiToken{consumes<RPCDigiCollection>(m_rpcDigiLabel)},
46  m_useRPCSimLink{iConfig.getUntrackedParameter<int>("UseRPCSimLink", 0)}
47 {
48 
49  //...........................................................................
50 
51  std::string configFile = iConfig.getParameter<std::string>("ConfigFile");
52 
53  edm::FileInPath f1("L1Trigger/RPCTechnicalTrigger/data/" + configFile);
54  m_configFile = f1.fullPath();
55 
56  if ( m_verbosity ) {
57  LogTrace("RPCTechnicalTrigger")
58  << m_rpcDigiLabel << '\n'
59  << std::endl;
60 
61  LogTrace("RPCTechnicalTrigger")
62  << "\nConfiguration file used for UseEventSetup = 0 \n" << m_configFile << '\n'
63  << std::endl;
64  }
65 
66  //...........................................................................
67  //... There are three Technical Trigger Units Boards: 1 can handle 2 Wheels
68  //... n_Wheels sets the number of wheels attached to board with index boardIndex
69 
70  constexpr std::array<int,3> boardIndex={{1,2,3}};
71  constexpr std::array<int,3> nWheels = { {2,1,2} };
72 
73  m_ttu[0] = TTUEmulator( boardIndex[0] , nWheels[0] );
74  m_ttu[1] = TTUEmulator( boardIndex[1] , nWheels[1] );
75  m_ttu[2] = TTUEmulator( boardIndex[2] , nWheels[2] );
76 
77  //... This is second line that delivers in parallel a second trigger
78  m_ttuRbcLine[0] = TTUEmulator( boardIndex[0] , nWheels[0] );
79  m_ttuRbcLine[1] = TTUEmulator( boardIndex[1] , nWheels[1] );
80  m_ttuRbcLine[2] = TTUEmulator( boardIndex[2] , nWheels[2] );
81 
82  //...........................................................................
83 
84  m_hasConfig = false;
85  produces<L1GtTechnicalTriggerRecord>();
86  consumes<edm::DetSetVector<RPCDigiSimLink> >(edm::InputTag("simMuonRPCDigis", "RPCDigiSimLink",""));
87 }
88 
89 
91 {
92 }
93 
94 //=============================================================================
96 
97 
98  bool status(false);
99 
101 
103 
104  std::unique_ptr<L1GtTechnicalTriggerRecord> output(new L1GtTechnicalTriggerRecord());
105 
106  //. Set up RPC geometry
107  edm::ESHandle<RPCGeometry> rpcGeometry;
108  iSetup.get<MuonGeometryRecord>().get( rpcGeometry );
109 
110  std::unique_ptr<ProcessInputSignal> signal;
111  if ( m_useRPCSimLink == 0 ) {
112  iEvent.getByToken(m_rpcDigiToken, pIn);
113  if ( ! pIn.isValid() ) {
114  edm::LogError("RPCTechnicalTrigger") << "can't find RPCDigiCollection with label: "
115  << m_rpcDigiLabel << '\n';
116  iEvent.put(std::move(output));
117  return;
118  }
119 
120  signal = std::make_unique<RBCProcessRPCDigis>( rpcGeometry, pIn );
121 
122  } else {
123 
124  iEvent.getByLabel("simMuonRPCDigis", "RPCDigiSimLink", simIn);
125 
126  if ( ! simIn.isValid() ) {
127  edm::LogError("RPCTechnicalTrigger") << "can't find RPCDigiCollection with label: "
128  << m_rpcDigiLabel << '\n';
129  iEvent.put(std::move(output));
130  return;
131  }
132  signal = std::make_unique<RBCProcessRPCSimDigis>( rpcGeometry, simIn );
133  }
134 
135  LogDebug("RPCTechnicalTrigger") << "signal object created" << '\n';
136 
137  if ( ! m_hasConfig ) {
138  edm::LogError("RPCTechnicalTrigger") << "cannot read hardware configuration \n";
139  iEvent.put(std::move(output));
140  return;
141  }
142 
143  status = signal->next();
144 
145  if ( !status) {
146  iEvent.put(std::move(output));
147  return;
148  }
149 
150  auto* input = signal->retrievedata();
151 
152  std::vector<L1GtTechnicalTrigger> ttVec( m_ttBits.size() );
153 
154  //. distribute data to different TTU emulator instances and process it
155  std::bitset<5> triggerbits;
156 
157  std::vector<std::unique_ptr<TTUResults>> serializedInfoLine1;
158  std::vector<std::unique_ptr<TTUResults>> serializedInfoLine2;
159 
160  for(int k=0; k < kMaxTtuBoards; ++k) {
161 
162  m_ttu[k].processTtu( input );
163 
164  //work out Pointing Logic to Tracker
165  for( auto firstSector : s_quadrants)
166  m_ttuRbcLine[k].processTtu( input , firstSector );
167 
168  //...for trigger 1
169  for( auto const& out : m_ttu[k].m_triggerBxVec)
170  serializedInfoLine1.emplace_back( std::make_unique<TTUResults>( k, out.m_bx, out.m_trigger[0], out.m_trigger[1] ) );
171  m_ttu[k].clearTriggerResponse();
172 
173  //...for trigger 2
174  for( auto const& out : m_ttuRbcLine[k].m_triggerBxVec)
175  serializedInfoLine2.push_back( std::make_unique<TTUResults>( k,
176  out.m_bx,
177  out.m_trigger[0],
178  out.m_trigger[1],
179  out.m_wedge ) );
180 
181  m_ttuRbcLine[k].clearTriggerResponse();
182 
183  }
184 
185  //.. write results to technical trigger bits
186  int bx(0);
187  int infoSize(0);
188 
189  infoSize = serializedInfoLine1.size();
190 
191  auto sortByBx = [](auto& iLHS, auto& iRHS) {
192  return iLHS->m_bx < iRHS->m_bx;
193  };
194  std::sort( serializedInfoLine1.begin(), serializedInfoLine1.end(), sortByBx );
195 
196  if( m_verbosity ) {
197  for( auto& ttu : serializedInfoLine1) {
198  if ( abs( ttu->m_bx ) <= 1 )
199  std::cout << "RPCTechnicalTrigger> "
200  << ttu->m_ttuidx << '\t'
201  << ttu->m_bx << '\t'
202  << ttu->m_trigWheel1 << '\t'
203  << ttu->m_trigWheel2 << '\n';
204  }
205  }
206 
207  bool has_bx0 = false;
208 
209  for(int k = 0; k < infoSize; k+=kMaxTtuBoards) {
210 
211  bx = serializedInfoLine1[k]->m_bx;
212 
213  if ( bx == 0 ) {
214 
215  triggerbits.set(0, serializedInfoLine1[k]->m_trigWheel2);
216  triggerbits.set(1, serializedInfoLine1[k]->m_trigWheel1);
217  triggerbits.set(2, serializedInfoLine1[k+1]->m_trigWheel1);
218  triggerbits.set(3, serializedInfoLine1[k+2]->m_trigWheel1);
219  triggerbits.set(4, serializedInfoLine1[k+2]->m_trigWheel2);
220 
221  bool five_wheels_OR = triggerbits.any();
222 
223  ttVec.at(0)=L1GtTechnicalTrigger(m_ttNames.at(0), m_ttBits.at(0), bx, five_wheels_OR ) ; // bit 24 = Or 5 wheels in TTU mode
224  ttVec.at(2)=L1GtTechnicalTrigger(m_ttNames.at(2), m_ttBits.at(2), bx, triggerbits[0] ) ; // bit 26
225  ttVec.at(3)=L1GtTechnicalTrigger(m_ttNames.at(3), m_ttBits.at(3), bx, triggerbits[1] ) ; // bit 27
226  ttVec.at(4)=L1GtTechnicalTrigger(m_ttNames.at(4), m_ttBits.at(4), bx, triggerbits[2] ) ; // bit 28
227  ttVec.at(5)=L1GtTechnicalTrigger(m_ttNames.at(5), m_ttBits.at(5), bx, triggerbits[3] ) ; // bit 29
228  ttVec.at(6)=L1GtTechnicalTrigger(m_ttNames.at(6), m_ttBits.at(6), bx, triggerbits[4] ) ; // bit 30
229 
230  triggerbits.reset();
231 
232  has_bx0 = true;
233 
234  break;
235 
236  } else continue;
237 
238  }
239 
240  infoSize = serializedInfoLine2.size();
241 
242  std::sort( serializedInfoLine2.begin(), serializedInfoLine2.end(), sortByBx );
243 
244  if(m_verbosity) {
245  for( auto& ttu : serializedInfoLine2) {
246  if (abs ( ttu->m_bx ) <= 1 )
247  std::cout << "RPCTechnicalTrigger> "
248  << ttu->m_ttuidx << '\t'
249  << ttu->m_bx << '\t'
250  << ttu->m_trigWheel1 << '\t'
251  << ttu->m_trigWheel2 << '\t'
252  << ttu->m_wedge << '\n';
253  }
254  }
255 
256  auto ttuResultsByQuadrant = convertToMap( serializedInfoLine2 );
257 
258  std::bitset<8> triggerCoincidence;
259  triggerCoincidence.reset();
260 
261  // searchCoincidence( W-2 , W0 )
262  bool result = searchCoincidence( -2, 0, ttuResultsByQuadrant );
263  triggerCoincidence.set(0, result );
264 
265  // searchCoincidence( W-2 , W+1 )
266  result = searchCoincidence( -2, 1, ttuResultsByQuadrant );
267  triggerCoincidence.set(1, result );
268 
269  // searchCoincidence( W-1 , W0 )
270  result = searchCoincidence( -1, 0, ttuResultsByQuadrant );
271  triggerCoincidence.set(2, result );
272 
273  // searchCoincidence( W-1 , W+1 )
274  result = searchCoincidence( -1, 1, ttuResultsByQuadrant );
275  triggerCoincidence.set(3, result );
276 
277  // searchCoincidence( W-1 , W+2 )
278  result = searchCoincidence( -1, 2, ttuResultsByQuadrant );
279  triggerCoincidence.set(4, result );
280 
281  // searchCoincidence( W0 , W0 )
282  result = searchCoincidence( 0 , 0, ttuResultsByQuadrant );
283  triggerCoincidence.set(5, result );
284 
285  // searchCoincidence( W+1 , W0 )
286  result = searchCoincidence( 1, 0, ttuResultsByQuadrant );
287  triggerCoincidence.set(6, result );
288 
289  // searchCoincidence( W+2 , W0 )
290  result = searchCoincidence( 2, 0, ttuResultsByQuadrant );
291  triggerCoincidence.set(7, result );
292 
293  bool five_wheels_OR = triggerCoincidence.any();
294 
295  if ( m_verbosity ) std::cout << "RPCTechnicalTrigger> pointing trigger: " << five_wheels_OR << '\n';
296 
297  ttVec.at(1)=L1GtTechnicalTrigger(m_ttNames.at(1), m_ttBits.at(1), bx, five_wheels_OR ) ; // bit 25 = Or 5 wheels in RBC mode
298 
299  triggerCoincidence.reset();
300 
301  //...check that data appeared at bx=0
302 
303  if ( ! has_bx0 ) {
304  iEvent.put(std::move(output));
305  LogDebug("RPCTechnicalTrigger") << "RPCTechnicalTrigger> end of event loop" << std::endl;
306  return;
307 
308  }
309 
310  output->setGtTechnicalTrigger(ttVec);
311  iEvent.put(std::move(output));
312 
313  //.... all done
314 
315  LogDebug("RPCTechnicalTrigger") << "RPCTechnicalTrigger> end of event loop" << std::endl;
316 
317 }
318 // ------------ method called once each job just before starting event loop ------------
319 void RPCTechnicalTrigger::beginRun(edm::Run const& iRun, const edm::EventSetup& evtSetup)
320 {
321  LogDebug("RPCTechnicalTrigger") << "RPCTechnicalTrigger::beginRun> starts" << std::endl;
322 
323  //.. Get Board Specifications (hardware configuration)
324 
325  if ( m_useEventSetup >= 1 ) {
326 
328  evtSetup.get<RBCBoardSpecsRcd>().get(pRBCSpecs);
329 
331  evtSetup.get<TTUBoardSpecsRcd>().get(pTTUSpecs);
332 
333  if ( !pRBCSpecs.isValid() || !pTTUSpecs.isValid() ) {
334  edm::LogError("RPCTechnicalTrigger") << "can't find RBC/TTU BoardSpecsRcd" << '\n';
335  m_hasConfig = false;
336  }
337  else {
338  m_rbcspecs = pRBCSpecs.product();
339  m_ttuspecs = pTTUSpecs.product();
340  m_hasConfig = true;
341  }
342 
343  } else {
344 
345  // read hardware configuration from file
346  m_readConfig = std::make_unique<TTUConfigurator>( m_configFile );
347 
348  if ( m_readConfig->m_hasConfig ) {
349  m_readConfig->process();
350  m_rbcspecs = m_readConfig->getRbcSpecs();
351  m_ttuspecs = m_readConfig->getTtuSpecs();
352  m_hasConfig = true;
353  }
354 
355  else m_hasConfig = false;
356 
357  }
358 
359  if ( m_hasConfig ) {
360 
361  //... Initialize all
362 
363  for (int k=0; k < kMaxTtuBoards; ++k ) {
364 
365  m_ttu[k].SetLineId ( 1 );
366  m_ttuRbcLine[k].SetLineId( 2 );
367 
368  m_ttu[k].setSpecifications( m_ttuspecs, m_rbcspecs );
369  m_ttuRbcLine[k].setSpecifications( m_ttuspecs, m_rbcspecs );
370 
371  m_ttu[k].initialise();
372  m_ttuRbcLine[k].initialise();
373  }
374 
375  }
376 
377 }
378 
379 //
380 std::map<int,RPCTechnicalTrigger::TTUResults*>
381 RPCTechnicalTrigger::convertToMap( const std::vector<std::unique_ptr<TTUResults>> & ttuResults ) const
382 {
383  std::map<int,TTUResults*> returnValue;
384  auto itr = ttuResults.begin();
385 
386  while ( itr != ttuResults.end() ) {
387 
388  if ( (*itr)->m_bx != 0 ) {
389  ++itr;
390  continue;
391  }
392 
393  int key(0);
394  key = 1000 * ( (*itr)->m_ttuidx + 1 ) + 1*(*itr)->m_wedge;
395  returnValue[ key ] = itr->get();
396  ++itr;
397  }
398 
399  return returnValue;
400 
401 }
402 
403 //...RBC pointing logic to tracker bit 25: hardwired
404 bool RPCTechnicalTrigger::searchCoincidence( int wheel1, int wheel2, std::map<int, TTUResults*> const& ttuResultsByQuadrant) const
405 {
406 
407  std::map<int, TTUResults*>::const_iterator itr;
408  bool topRight(false);
409  bool botLeft(false);
410 
411  int indxW1 = wheelTtu[wheel1+kWheelOffset];
412  int indxW2 = wheelTtu[wheel2+kWheelOffset];
413 
414  int k(0);
415  int key(0);
416  bool finalTrigger(false);
417  int maxTopQuadrants = 4;
418 
419  //work out Pointing Logic to Tracker
420 
421  for( auto firstSector : s_quadrants) {
422 
423  key = 1000 * ( indxW1 ) + firstSector;
424 
425  itr = ttuResultsByQuadrant.find( key );
426  if ( itr != ttuResultsByQuadrant.end() )
427  topRight = (*itr).second->getTriggerForWheel(wheel1);
428 
429  //std::cout << "W1: " << wheel1 << " " << "sec: " << firstSector << " dec: " << topRight << '\n';
430 
431  key = 1000 * ( indxW2 ) + firstSector + 5;
432 
433  itr = ttuResultsByQuadrant.find( key );
434 
435  if ( itr != ttuResultsByQuadrant.end() )
436  botLeft = (*itr).second->getTriggerForWheel(wheel2);
437 
438  //std::cout << "W2: " << wheel2 << " " << "sec: " << firstSector + 5 << " dec: " << botLeft << '\n';
439 
440  finalTrigger |= ( topRight && botLeft );
441 
442  ++k;
443 
444  if ( k > maxTopQuadrants)
445  break;
446 
447  }
448 
449  //Try the opposite now
450 
451  k=0;
452 
453  for( auto firstSector : s_quadrants) {
454 
455  key = 1000 * ( indxW2 ) + firstSector;
456 
457  itr = ttuResultsByQuadrant.find( key );
458  if ( itr != ttuResultsByQuadrant.end() )
459  topRight = (*itr).second->getTriggerForWheel(wheel1);
460 
461  //std::cout << "W1: " << wheel1 << " " << "sec: " << firstSector << " dec: " << topRight << '\n';
462 
463  key = 1000 * ( indxW1 ) + firstSector + 5;
464 
465  itr = ttuResultsByQuadrant.find( key );
466 
467  if ( itr != ttuResultsByQuadrant.end() )
468  botLeft = (*itr).second->getTriggerForWheel(wheel2);
469 
470  //std::cout << "W2: " << wheel2 << " " << "sec: " << firstSector + 5 << " dec: " << botLeft << '\n';
471 
472  finalTrigger |= ( topRight && botLeft );
473 
474  ++k;
475 
476  if ( k > maxTopQuadrants)
477  break;
478 
479  }
480 
481  return finalTrigger;
482 
483 }
484 
485 // ------------ method called once each job just after ending the event loop ------------
486 
488 {
489 
490  LogDebug("RPCTechnicalTrigger") << "RPCTechnicalTrigger::Printing TTU emulators info>" << std::endl;
491 
492  for (int k=0; k < kMaxTtuBoards; ++k ) {
493  m_ttu[k].printinfo();
494  m_ttuRbcLine[k].printinfo();
495  }
496 
497 
498 }
499 
500 
501 //define this as a plug-in
#define LogDebug(id)
void beginRun(edm::Run const &, const edm::EventSetup &) final
T getUntrackedParameter(std::string const &, T const &) const
bool searchCoincidence(int, int, std::map< int, TTUResults * > const &ttuResultsByQuandrant) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
std::unique_ptr< TTUConfigurator > m_readConfig
const edm::InputTag m_rpcDigiLabel
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
std::array< TTUEmulator, kMaxTtuBoards > m_ttuRbcLine
static std::string const input
Definition: EdmProvDump.cc:48
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
RPCTechnicalTrigger(const edm::ParameterSet &)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool isValid() const
Definition: HandleBase.h:74
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:480
#define LogTrace(id)
int k[5][pyjets_maxn]
const TTUBoardSpecs * m_ttuspecs
std::array< TTUEmulator, kMaxTtuBoards > m_ttu
const edm::EDGetTokenT< RPCDigiCollection > m_rpcDigiToken
const std::vector< unsigned > m_ttBits
T get() const
Definition: EventSetup.h:71
const RBCBoardSpecs * m_rbcspecs
const std::vector< std::string > m_ttNames
bool isValid() const
Definition: ESHandle.h:44
T const * product() const
Definition: ESHandle.h:86
def move(src, dest)
Definition: eostools.py:511
#define constexpr
Definition: Run.h:45
std::map< int, TTUResults * > convertToMap(const std::vector< std::unique_ptr< TTUResults >> &) const
void produce(edm::Event &, const edm::EventSetup &) override