From 356cf36eb1d61b2dc2195769a65a0af1b2b29eb1 Mon Sep 17 00:00:00 2001
From: Gerhard Raven <gerhard.raven@nikhef.nl>
Date: Tue, 25 Oct 2022 14:05:21 +0200
Subject: [PATCH] simplify fillRawBank

- avoid spurious copies
- avoid more complicated data structures than actually needed
- avoid spurious loops
---
 .../src/CaloFutureFillRawBuffer.cpp           | 88 +++++++------------
 1 file changed, 31 insertions(+), 57 deletions(-)

diff --git a/CaloFuture/CaloFutureDAQ/src/CaloFutureFillRawBuffer.cpp b/CaloFuture/CaloFutureDAQ/src/CaloFutureFillRawBuffer.cpp
index 10764004c14..6d0a6475b13 100644
--- a/CaloFuture/CaloFutureDAQ/src/CaloFutureFillRawBuffer.cpp
+++ b/CaloFuture/CaloFutureDAQ/src/CaloFutureFillRawBuffer.cpp
@@ -12,6 +12,7 @@
 #include "CaloFutureUtils/CaloFutureAlgUtils.h"
 #include "Event/CaloAdc.h"
 #include "Event/RawEvent.h"
+#include "Kernel/STLExtensions.h"
 #include "LHCbAlgs/Consumer.h"
 
 //-----------------------------------------------------------------------------
@@ -99,7 +100,6 @@ namespace LHCb::Calo {
     triggerBankType = ( calo.index() == Detector::Calo::CellCode::Index::EcalCalo ) ? LHCb::RawBank::EcalTrig
                                                                                     : LHCb::RawBank::HcalTrig;
 
-    std::vector<int>                       banks_sourceID;
     std::vector<std::vector<unsigned int>> banks;
     for ( int kk = 0; numberOfBanks > kk; kk++ ) { banks.emplace_back().reserve( 500 ); }
 
@@ -122,11 +122,10 @@ namespace LHCb::Calo {
       int m_numberOfBanks = calo.nTell1s();
 
       for ( int kTell1 = 0; m_numberOfBanks > kTell1; kTell1++ ) {
-        for ( auto cardNum : calo.tell1ToCards( kTell1 ) ) {
+        for ( auto const cardNum : calo.tell1ToCards( kTell1 ) ) {
           if ( calo.isPinCard( cardNum ) && !m_pin ) continue; // No sub-bank for PIN-FEB if not explicitely requested
           int sizeIndex = banks[kTell1].size();
           banks[kTell1].push_back( calo.cardCode( cardNum ) << 14 );
-          std::vector<LHCb::Detector::Calo::CellID> ids = calo.cardChannels( cardNum );
 
           //=== The trigger part is first
           int patternIndex = banks[kTell1].size();
@@ -153,7 +152,7 @@ namespace LHCb::Calo {
           offset = 0;
           bNum   = 0;
 
-          for ( LHCb::Detector::Calo::CellID id : ids ) {
+          for ( LHCb::Detector::Calo::CellID id : calo.cardChannels( cardNum ) ) {
             LHCb::CaloAdc* dig = digs.object( id );
             int adc = ( dig ? std::clamp( dig->adc() + 256, 0, 4095 ) : 256 ); //== Default if non existing cell.
             if ( 248 <= adc && adc < 264 ) {                                   //... store short
@@ -187,9 +186,9 @@ namespace LHCb::Calo {
         }
       }
 
-      for ( unsigned int kk = 0; banks.size() > kk; kk++ ) {
-        rawEvent.addBank( kk, bankType, m_dataCodingType, banks[kk] );
-        m_totDataSize += banks[kk].size();
+      for ( const auto& [kk, bank] : range::enumerate( banks ) ) {
+        rawEvent.addBank( kk, bankType, m_dataCodingType, bank );
+        m_totDataSize += bank.size();
       }
     }
   }
@@ -199,87 +198,62 @@ namespace LHCb::Calo {
                                 LHCb::RawEvent& rawEvent ) const {
 
     //== TELL40 coding format: packed data, starting from Full banks
-    int numberOfBanks = 2 * calo.nTell1s(); // 2 source_ids per tell40
-
     if ( msgLevel( MSG::DEBUG ) )
       debug() << "DataCodingType==" << m_dataCodingType << "  -  Processing " << calo.nCards() << " FE-Cards and "
               << calo.nTell1s() << " TELL40  -  BankType: LHCb::RawBank::Calo" << endmsg;
 
-    std::vector<int>                       banks_sourceID;
-    std::vector<std::vector<unsigned int>> banks;
-    for ( int kk = 0; numberOfBanks > kk; kk++ ) { banks.emplace_back().reserve( 500 ); }
-
-    bool isLittleEndian = m_dataCodingType == 5 ? true : false;
-
-    std::map<int, std::vector<int>>           map = calo.getSourceIDsMap();
-    std::map<int, std::vector<int>>::iterator itr;
-    int                                       kSourceID = 0;
-    for ( itr = map.begin(); itr != map.end(); ++itr ) {
+    std::vector<unsigned int> bank;
+    bank.reserve( 500 );
+    auto append = [isLittleEndian = ( m_dataCodingType == 5 )]( std::vector<unsigned int>& b, unsigned int w ) {
+      if ( !isLittleEndian )
+        w = ( ( w >> 24 ) & 0x000000FF ) | ( ( w >> 8 ) & 0x0000FF00 ) | ( ( w << 8 ) & 0x00FF0000 ) |
+            ( ( w << 24 ) & 0xFF000000 );
+      b.push_back( w );
+    };
 
-      banks_sourceID.push_back( itr->first );
+    for ( auto const& [sourceID, cardNums] : calo.getSourceIDsMap() ) {
 
-      //=== The LLT part is first
-      int LLT1 = 0, LLT2 = 0, LLT3 = 0;
-      banks[kSourceID].push_back( LLT1 );
-      banks[kSourceID].push_back( LLT2 );
-      banks[kSourceID].push_back( LLT3 );
+      // clear for current iteration by initializing the LLT part first
+      unsigned int LLT1 = 0, LLT2 = 0, LLT3 = 0;
+      bank.assign( {LLT1, LLT2, LLT3} );
 
-      for ( auto cardNum : itr->second ) {
+      for ( auto const& cardNum : cardNums ) {
         if ( cardNum == 0 ) {
           if ( msgLevel( MSG::DEBUG ) )
             debug() << "cardnum == 0 fill 12 empty words of 32 bits (32 cells  of 12 bits)" << endmsg;
-          for ( int i = 0; i < 12; i++ ) banks[kSourceID].push_back( 0 );
+          bank.insert( bank.end(), 12, 0 );
           continue;
         }
         if ( calo.isPinCard( cardNum ) && !m_pin ) {
-          for ( int i = 0; i < 12; i++ ) banks[kSourceID].push_back( 0 );
+          bank.insert( bank.end(), 12, 0 );
           continue; // No sub-bank for PIN-FEB if not explicitely requested
         }
 
-        std::vector<LHCb::Detector::Calo::CellID> ids = calo.cardChannels( cardNum );
-
         unsigned int word   = 0;
         int          offset = 0;
 
         //=== Now the ADCs
-        for ( LHCb::Detector::Calo::CellID id : ids ) {
-          LHCb::CaloAdc* dig = digs.object( id );
+        for ( auto cell_id : calo.cardChannels( cardNum ) ) {
+          LHCb::CaloAdc* dig = digs.object( cell_id );
           unsigned int adc = ( dig ? std::clamp( dig->adc() + 256, 0, 4095 ) : 256 ); //== Default if non existing cell.
 
           offset += 12;
           if ( offset < 32 ) {
             word |= adc << ( 32 - offset );
           } else {
-            word |= adc >> abs( 32 - offset );
-            if ( !isLittleEndian )
-              word = ( ( word >> 24 ) & 0x000000FF ) | ( ( word >> 8 ) & 0x0000FF00 ) | ( ( word << 8 ) & 0x00FF0000 ) |
-                     ( ( word << 24 ) & 0xFF000000 );
-            banks[kSourceID].push_back( word );
-            if ( offset > 32 )
-              word = adc << ( 32 - abs( 32 - offset ) ); //== upper bits if needed
-            else
-              word = 0;
-            offset = abs( 32 - offset );
+            offset -= 32;
+            word |= adc >> offset;
+            append( bank, word );
+            word = ( offset == 0 ? 0 : ( adc << ( 32 - offset ) ) ); // pick up the overflow upper bits
           }
         }
-        if ( 0 != offset ) {
-          if ( !isLittleEndian )
-            word = ( ( word >> 24 ) & 0x000000FF ) | ( ( word >> 8 ) & 0x0000FF00 ) | ( ( word << 8 ) & 0x00FF0000 ) |
-                   ( ( word << 24 ) & 0xFF000000 );
-          banks[kSourceID].push_back( word );
-        }
+        if ( offset != 0 ) append( bank, word );
 
-        if ( msgLevel( MSG::DEBUG ) ) debug() << format( "source_id %2d card %3d", kSourceID, cardNum ) << endmsg;
+        if ( msgLevel( MSG::DEBUG ) ) debug() << format( "source_id %2d card %3d", sourceID, cardNum ) << endmsg;
       }
-      kSourceID++;
-    }
-
-    if ( banks_sourceID.size() < banks.size() )
-      throw GaudiException( "Coding type doesn't match DB", this->name(), StatusCode::FAILURE );
 
-    for ( unsigned int kk = 0; banks.size() > kk; kk++ ) {
-      rawEvent.addBank( banks_sourceID[kk], LHCb::RawBank::Calo, m_dataCodingType, banks[kk] );
-      m_totDataSize += banks[kk].size();
+      rawEvent.addBank( sourceID, LHCb::RawBank::Calo, m_dataCodingType, bank );
+      m_totDataSize += bank.size();
     }
   }
 
-- 
GitLab