Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:19:15

0001 #include <TFile.h>
0002 #include <TTree.h>
0003 #include "IORawData/HcalTBInputService/src/CDFChunk.h"
0004 #include "IORawData/HcalTBInputService/src/CDFEventInfo.h"
0005 #include "IORawData/HcalTBInputService/plugins/HcalTBWriter.h"
0006 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
0007 #include "DataFormats/FEDRawData/interface/FEDHeader.h"
0008 #include <unistd.h>
0009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0010 
0011 HcalTBWriter::HcalTBWriter(const edm::ParameterSet& pset)
0012     : namePattern_(pset.getUntrackedParameter<std::string>("FilenamePattern", "/tmp/HTB_%06d.root")) {
0013   tok_raw_ = consumes<FEDRawDataCollection>(pset.getParameter<edm::InputTag>("fedRawDataCollectionTag"));
0014 
0015   std::vector<edm::ParameterSet> names = pset.getUntrackedParameter<std::vector<edm::ParameterSet> >("ChunkNames");
0016   std::vector<edm::ParameterSet>::iterator j;
0017   for (j = names.begin(); j != names.end(); j++) {
0018     std::string name = j->getUntrackedParameter<std::string>("Name");
0019     int num = j->getUntrackedParameter<int>("Number");
0020     blockToName_[num] = name;
0021   }
0022 
0023   file_ = nullptr;
0024   tree_ = nullptr;
0025   eventInfo_ = nullptr;
0026 }
0027 
0028 void HcalTBWriter::endJob() {
0029   char buffer[1024];
0030   if (file_ != nullptr) {
0031     file_->Write();
0032 
0033     ri_.setInfo("DAQSofwareRelease", "UNKNOWN -- HcalTBWriter");
0034     gethostname(buffer, 1024);
0035     ri_.setInfo("WriterHostname", buffer);
0036     ri_.store(file_);
0037 
0038     file_->Close();
0039     file_ = nullptr;
0040     tree_ = nullptr;
0041     chunkMap_.clear();
0042     eventInfo_ = nullptr;
0043   }
0044 }
0045 
0046 void HcalTBWriter::analyze(const edm::Event& e, const edm::EventSetup& es) {
0047   edm::Handle<FEDRawDataCollection> raw;
0048   e.getByToken(tok_raw_, raw);
0049 
0050   if (file_ == nullptr) {
0051     char fname[4096];
0052     snprintf(fname, 4096, namePattern_.c_str(), e.id().run());
0053     edm::LogInfo("HCAL") << "Opening " << fname << " for writing HCAL-format file.";
0054     file_ = new TFile(fname, "RECREATE");
0055     ri_.setInfo("OriginalFile", fname);
0056     buildTree(*raw);
0057   }
0058 
0059   // adopt the buffers for writing
0060   for (std::map<int, int>::const_iterator i = chunkMap_.begin(); i != chunkMap_.end(); i++) {
0061     CDFChunk* c = chunkList_[i->second];
0062     const FEDRawData& frd = raw->FEDData(i->first);
0063     c->adoptBuffer((ULong64_t*)frd.data(), frd.size() / 8);
0064   }
0065 
0066   // copy the event info bits
0067   extractEventInfo(*raw, e.id());
0068 
0069   // fill the tree
0070   tree_->Fill();
0071   // release all the buffers
0072   for (std::map<int, int>::const_iterator i = chunkMap_.begin(); i != chunkMap_.end(); i++) {
0073     CDFChunk* c = chunkList_[i->second];
0074     c->releaseBuffer();
0075   }
0076 }
0077 
0078 void HcalTBWriter::buildTree(const FEDRawDataCollection& raw) {
0079   tree_ = new TTree("CMSRAW", "CMS Common Data Format Tree");
0080   chunkMap_.clear();
0081   trigChunk_ = -1;
0082   int j = 0;
0083   for (int i = 0; i < 2048; i++) {
0084     const FEDRawData& frd = raw.FEDData(i);
0085     if (frd.size() < 16)
0086       continue;  // it's empty... like
0087 
0088     std::string name;
0089     if (blockToName_.find(i) != blockToName_.end())
0090       name = blockToName_[i];
0091     else {
0092       char sname[64];
0093       snprintf(sname, 64, "Chunk%03d", i);
0094       name = sname;
0095     }
0096 
0097     CDFChunk* c = new CDFChunk(name.c_str());
0098     chunkList_[j] = c;
0099     tree_->Branch(name.c_str(), "CDFChunk", &(chunkList_[j]));
0100     chunkMap_[i] = j;
0101 
0102     if (name == "HCAL_Trigger" || name == "SliceTest_Trigger")
0103       trigChunk_ = j;
0104 
0105     j++;
0106   }
0107   eventInfo_ = new CDFEventInfo();
0108   tree_->Branch("CDFEventInfo", "CDFEventInfo", &eventInfo_, 16000, 2);
0109 }
0110 
0111 typedef struct StandardTrgMsgBlkStruct {
0112   uint32_t orbitNumber;
0113   uint32_t eventNumber;
0114   uint32_t flags_daq_ttype;
0115   uint32_t algo_bits_3;
0116   uint32_t algo_bits_2;
0117   uint32_t algo_bits_1;
0118   uint32_t algo_bits_0;
0119   uint32_t tech_bits;
0120   uint32_t gps_1234;
0121   uint32_t gps_5678;
0122 } StandardTrgMsgBlk;
0123 
0124 typedef struct newExtendedTrgMsgBlkStruct {
0125   StandardTrgMsgBlk stdBlock;
0126   uint32_t triggerWord;
0127   uint32_t triggerTime_usec;
0128   uint32_t triggerTime_base;
0129   uint32_t spillNumber;
0130   uint32_t runNumber;
0131   char runNumberSequenceId[16];
0132   uint32_t eventStatus;
0133 } newExtendedTrgMsgBlk;
0134 
0135 void HcalTBWriter::extractEventInfo(const FEDRawDataCollection& raw, const edm::EventID& id) {
0136   int runno = id.run();
0137   const char* seqid = "";
0138   int eventNo = id.event();
0139   int l1aNo = eventNo;
0140   int orbitNo = 0;
0141   int bunchNo = 0;
0142 
0143   if (trigChunk_ >= 0) {
0144     const newExtendedTrgMsgBlk* tinfo = (const newExtendedTrgMsgBlk*)(chunkList_[trigChunk_]->getData() +
0145                                                                       2);  // assume 2 64-bit words for the CDF header
0146     orbitNo = tinfo->stdBlock.orbitNumber;
0147     seqid = tinfo->runNumberSequenceId;
0148     FEDHeader head((const unsigned char*)chunkList_[trigChunk_]->getData());
0149     bunchNo = head.bxID();
0150     l1aNo = head.lvl1ID();
0151   }
0152 
0153   eventInfo_->Set(runno, seqid, eventNo, l1aNo, orbitNo, bunchNo);
0154 }
0155 
0156 #include "FWCore/Framework/interface/MakerMacros.h"
0157 #include <cstdint>
0158 
0159 DEFINE_FWK_MODULE(HcalTBWriter);