LCOV - code coverage report
Current view: top level - mu2e-pcie-utils/dtcInterfaceLib - binary_dump_to_DTC_file.cc (source / functions) Coverage Total Hit
Test: mu2edaq.info.cleaned Lines: 0.0 % 84 0
Test Date: 2026-07-30 01:56:44 Functions: 0.0 % 10 0

            Line data    Source code
       1              : // This file (util_main.cc) was created by Ron Rechenmacher <ron@fnal.gov> on
       2              : // May 13, 2015. "TERMS AND CONDITIONS" governing this file are in the README
       3              : // or COPYING file. If you do not have such a file, one can be obtained by
       4              : // contacting Ron or Fermi Lab in Batavia IL, 60510, phone: 630-840-3000.
       5              : // $RCSfile: .emacs.gnu,v $
       6              : // rev="$Revision: 1.23 $$Date: 2012/01/23 15:32:40 $";
       7              : 
       8              : #include <filesystem>
       9              : 
      10              : #include "dtcInterfaceLib/DTC_Data_Verifier.h"
      11              : 
      12              : #include "TRACE/tracemf.h"
      13              : #define TRACE_NAME "binary_dump_to_DTC"
      14              : 
      15            0 : void printHelpMsg()
      16              : {
      17            0 :         std::cout << "Converts binary data dump file(s) to DTC binary file(s)" << std::endl;
      18            0 :         std::cout << "Usage: binary_dump_to_DTC_file [options] file.."
      19            0 :                           << std::endl;
      20              :         std::cout
      21            0 :                 << "Options are:" << std::endl
      22            0 :                 << "    -h, --help: This message." << std::endl
      23            0 :                 << std::endl;
      24            0 :         exit(0);
      25              : }
      26              : 
      27            0 : int main(int argc, char* argv[])
      28              : {
      29            0 :         std::vector<std::string> binaryFiles;
      30            0 :         for (auto optind = 1; optind < argc; ++optind)
      31              :         {
      32            0 :                 if (argv[optind][0] == '-')
      33              :                 {
      34            0 :                         switch (argv[optind][1])
      35              :                         {
      36            0 :                                 case '-':  // Long option
      37              :                                 {
      38            0 :                                         auto option = DTCLib::Utilities::getLongOptionOption(&optind, &argv);
      39            0 :                                         if (option == "--help")
      40              :                                         {
      41            0 :                                                 printHelpMsg();
      42              :                                         }
      43            0 :                                         break;
      44            0 :                                 }
      45            0 :                                 default:
      46            0 :                                         TLOG(TLVL_ERROR) << "Unknown option: " << argv[optind] << std::endl;
      47            0 :                                         printHelpMsg();
      48            0 :                                         break;
      49            0 :                                 case 'h':
      50            0 :                                         printHelpMsg();
      51            0 :                                         break;
      52              :                         }
      53              :                 }
      54              :                 else
      55              :                 {
      56            0 :                         binaryFiles.push_back(std::string(argv[optind]));
      57              :                 }
      58              :         }
      59              : 
      60            0 :         for (auto& file : binaryFiles)
      61              :         {
      62            0 :                 std::ifstream is(file);
      63            0 :                 std::filesystem::path p(file);
      64            0 :                 std::string ofilename = p.parent_path().string() + "/DTC_packets_" + p.filename().string();
      65            0 :                 std::ofstream of(ofilename, std::ios::trunc | std::ios::binary);
      66            0 :                 DTCLib::DTC_Data_Verifier verifier;
      67              : 
      68            0 :                 if (is.bad() || !is)
      69              :                 {
      70            0 :                         TLOG(TLVL_ERROR) << "Cannot read file " << file;
      71            0 :                         continue;
      72            0 :                 }
      73            0 :                 TLOG(TLVL_INFO) << "Reading binary file " << file << " and writing " << ofilename;
      74              : 
      75              :                 mu2e_databuff_t buf;
      76              : 
      77            0 :                 bool success = true;
      78            0 :                 size_t total_size_read = 0;
      79            0 :                 while (is)
      80              :                 {
      81              :                         uint64_t dmaSize;  // DMA buffer size
      82            0 :                         is.read((char*)&dmaSize, sizeof(dmaSize));
      83            0 :                         if (!is || is.eof()) break;
      84              : 
      85            0 :                         uint64_t dmaReadSize = static_cast<uint16_t>(dmaSize);
      86            0 :                         memcpy(buf, &dmaSize, sizeof(dmaSize));
      87            0 :                         is.read(((char*)buf) + 8, dmaReadSize - 8);
      88              : 
      89            0 :                         TLOG(TLVL_DEBUG + 1) << "Verifying event at offset 0x" << std::hex << total_size_read;
      90            0 :                         auto thisEvent = std::make_unique<DTCLib::DTC_Event>(buf);
      91              : 
      92            0 :                         auto eventByteCount = thisEvent->GetEventByteCount();
      93            0 :                         if (eventByteCount == 0)
      94              :                         {
      95            0 :                                 TLOG(TLVL_ERROR) << "Event byte count cannot be 0! Aborting file read at " << std::hex << total_size_read;
      96            0 :                                 break;
      97              :                         }
      98              : 
      99              :                         // Check for continued DMA
     100            0 :                         if (eventByteCount > dmaReadSize)
     101              :                         {
     102            0 :                                 auto inmem = std::make_unique<DTCLib::DTC_Event>(eventByteCount);
     103            0 :                                 memcpy(const_cast<void*>(inmem->GetRawBufferPointer()), thisEvent->GetRawBufferPointer(), dmaReadSize);
     104              : 
     105            0 :                                 auto bytes_read = dmaReadSize;
     106              : 
     107            0 :                                 while (bytes_read < eventByteCount)
     108              :                                 {
     109            0 :                                         total_size_read += dmaReadSize;  // Add here before we read the next DMA size
     110            0 :                                         TLOG(TLVL_DEBUG) << "Obtaining next DMA, bytes_read=" << bytes_read << ", eventByteCount=" << eventByteCount;
     111              : 
     112            0 :                                         is.read((char*)&dmaSize, sizeof(dmaSize));
     113            0 :                                         if (!is || is.eof()) break;
     114              : 
     115            0 :                                         dmaReadSize = static_cast<uint16_t>(dmaSize);
     116            0 :                                         memcpy(buf, &dmaSize, sizeof(dmaSize));
     117            0 :                                         is.read(((char*)buf) + 8, dmaReadSize - 8);
     118              : 
     119            0 :                                         size_t remainingEventSize = eventByteCount - bytes_read;
     120            0 :                                         size_t copySize = remainingEventSize < dmaReadSize ? remainingEventSize : dmaReadSize;
     121            0 :                                         memcpy(const_cast<uint8_t*>(static_cast<const uint8_t*>(inmem->GetRawBufferPointer()) + bytes_read), buf, copySize);
     122            0 :                                         bytes_read += dmaReadSize;
     123              :                                 }
     124              : 
     125            0 :                                 thisEvent.swap(inmem);
     126            0 :                         }
     127            0 :                         thisEvent->SetupEvent();
     128              : 
     129            0 :                         success = verifier.VerifyEvent(*thisEvent);
     130              : 
     131            0 :                         if (success)
     132              :                         {
     133            0 :                                 thisEvent->WriteEvent(of, true);
     134              :                         }
     135              :                         else
     136              :                         {
     137            0 :                                 TLOG(TLVL_ERROR) << "Error verifying event data. Aborting file read at " << std::hex << total_size_read;
     138            0 :                                 break;
     139              :                         }
     140              : 
     141            0 :                         total_size_read += dmaReadSize;
     142            0 :                 }
     143            0 :                 of.flush();
     144            0 :                 of.close();
     145            0 :                 if (success)
     146              :                 {
     147            0 :                         TLOG(TLVL_INFO) << "File " << file << " successfully written to " << ofilename;
     148              :                 }
     149            0 :         }
     150            0 : }
        

Generated by: LCOV version 2.0-1