Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-09-22 02:39:44

0001 #include <iomanip>
0002 
0003 #include "CSCFileDumper.h"
0004 
0005 //Framework stuff
0006 #include "DataFormats/Common/interface/Handle.h"
0007 #include "FWCore/Framework/interface/Event.h"
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 
0010 //FEDRawData
0011 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
0012 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
0013 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
0014 
0015 #include "DataFormats/MuonDetId/interface/CSCDetId.h"
0016 
0017 #include "FWCore/ServiceRegistry/interface/Service.h"
0018 
0019 #include <cstdio>
0020 #include <iostream>
0021 #include <sstream>
0022 
0023 CSCFileDumper::CSCFileDumper(edm::ParameterSet const &pset) {
0024   i_token = consumes<FEDRawDataCollection>(pset.getParameter<edm::InputTag>("source"));
0025   // source_ = pset.getUntrackedParameter<std::string>("source","rawDataCollector");
0026   output = pset.getUntrackedParameter<std::string>("output");
0027   events = pset.getUntrackedParameter<std::string>("events", "");
0028 
0029   cscFEDids.clear();
0030   /*
0031     for (unsigned int id=FEDNumbering::MINCSCFEDID;
0032          id<=FEDNumbering::MAXCSCFEDID; ++id)   // loop over DCCs
0033       {
0034         cscFEDids.push_back(id);
0035       }
0036    */
0037   for (unsigned int id = FEDNumbering::MINCSCDDUFEDID; id <= FEDNumbering::MAXCSCDDUFEDID; ++id)  // loop over DDUs
0038   {
0039     cscFEDids.push_back(id);
0040   }
0041   /*
0042     for (unsigned int id=FEDNumbering::MINCSCTFFEDID; id<=FEDNumbering::MAXCSCTFFEDID; id++)
0043       {
0044          cscFEDids.push_back(id);
0045       }
0046    */
0047 
0048   if (events.length()) {
0049     for (size_t pos1 = 0, pos2 = events.find(',');; pos1 = pos2 + 1, pos2 = events.find(',', pos2 + 1)) {
0050       if (pos2 != std::string::npos) {
0051         long event = 0;
0052         if (sscanf(events.substr(pos1, pos2 - pos1).c_str(), "%ld", &event) == 1 && event >= 0)
0053           eventsToDump.insert((unsigned long)event);
0054         else
0055           edm::LogError("CSCFileDumper") << " cannot parse events (" << events
0056                                          << ") parameter: " << events.substr(pos1, pos2 - pos1);
0057       } else {
0058         long event = 0;
0059         if (sscanf(events.substr(pos1, events.length() - pos1).c_str(), "%ld", &event) == 1 && event >= 0)
0060           eventsToDump.insert((unsigned long)event);
0061         else
0062           edm::LogError("CSCFileDumper") << " cannot parse events (" << events
0063                                          << ") parameter: " << events.substr(pos1, events.length() - pos1);
0064         break;
0065       }
0066     }
0067     std::ostringstream tmp;
0068     for (std::set<unsigned long>::const_iterator evt = eventsToDump.begin(); evt != eventsToDump.end(); evt++)
0069       tmp << *evt << " ";
0070     edm::LogInfo("CSCFileDumper") << " Following events will be dumped: " << tmp.str();
0071   } else
0072     edm::LogInfo("CSCFileDumper") << " All events will be dumped";
0073 }
0074 
0075 CSCFileDumper::~CSCFileDumper(void) {
0076   std::map<int, FILE *>::const_iterator stream = dump_files.begin();
0077   while (stream != dump_files.end()) {
0078     fclose(stream->second);
0079     stream++;
0080   }
0081 }
0082 
0083 void CSCFileDumper::analyze(const edm::Event &e, const edm::EventSetup &c) {
0084   // Get a handle to the FED data collection
0085   edm::Handle<FEDRawDataCollection> rawdata;
0086   e.getByToken(i_token, rawdata);
0087 
0088   // Get a handle to the FED data collection
0089 
0090   for (unsigned int i = 0; i < cscFEDids.size(); i++)  //for each of our DCCs
0091   {
0092     unsigned int id = cscFEDids[i];
0093     std::map<int, FILE *>::const_iterator stream = dump_files.find(id);
0094     /// Take a reference to this FED's data
0095     const FEDRawData &fedData = rawdata->FEDData(id);
0096     unsigned short int length = fedData.size();
0097 
0098     if (length && (eventsToDump.empty() || (eventsToDump.find((unsigned long)e.id().event()) != eventsToDump.end()))) {
0099       if (stream == dump_files.end()) {
0100         std::ostringstream name;
0101         name << output << "_FED" << id << ".raw" << std::ends;
0102         FILE *file;
0103         if ((file = fopen(name.str().c_str(), "wt")) == nullptr) {
0104           edm::LogError("CSCFileDumper") << "Cannot open the file: " << name.str();
0105           continue;
0106         } else
0107           dump_files[id] = file;
0108         stream = dump_files.find(id);
0109       }
0110 
0111       // Event buffer
0112       size_t size = length / 2;
0113       const unsigned short *buf = (unsigned short *)fedData.data();
0114       fwrite(buf, 2, size, stream->second);
0115     }
0116   }
0117 }