CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/src/IORawData/HcalTBInputService/src/HcalTBWriter.cc

Go to the documentation of this file.
00001 #include <TFile.h>
00002 #include <TTree.h>
00003 #include "IORawData/HcalTBInputService/src/CDFChunk.h"
00004 #include "IORawData/HcalTBInputService/src/CDFEventInfo.h"
00005 #include "IORawData/HcalTBInputService/src/HcalTBWriter.h"
00006 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00007 #include "DataFormats/FEDRawData/interface/FEDHeader.h"
00008 #include <unistd.h>
00009 #include <boost/cstdint.hpp>
00010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00011 
00012 
00013 HcalTBWriter::HcalTBWriter(const edm::ParameterSet & pset) : 
00014   namePattern_(pset.getUntrackedParameter<std::string>("FilenamePattern","/tmp/HTB_%06d.root"))
00015 {
00016 
00017   std::vector<edm::ParameterSet> names=pset.getUntrackedParameter<std::vector<edm::ParameterSet> >("ChunkNames");
00018   std::vector<edm::ParameterSet>::iterator j;
00019   for (j=names.begin(); j!=names.end(); j++) {
00020     std::string name=j->getUntrackedParameter<std::string>("Name");
00021     int num=j->getUntrackedParameter<int>("Number");
00022     blockToName_[num]=name;
00023   }
00024 
00025   file_=0;
00026   tree_=0;
00027   eventInfo_=0;
00028 }
00029 
00030 void HcalTBWriter::endJob() {
00031   char buffer[1024];
00032   if (file_!=0) {
00033     file_->Write();
00034 
00035     ri_.setInfo("DAQSofwareRelease","UNKNOWN -- HcalTBWriter");
00036     gethostname(buffer,1024);
00037     ri_.setInfo("WriterHostname",buffer);
00038     ri_.store(file_);
00039 
00040     file_->Close();
00041     file_=0;
00042     tree_=0;
00043     chunkMap_.clear();
00044     eventInfo_=0;
00045   }
00046 }
00047 
00048 void HcalTBWriter::analyze(const edm::Event& e, const edm::EventSetup& es) {
00049   edm::Handle<FEDRawDataCollection> raw;
00050   e.getByType(raw); // assume just one!
00051 
00052   if (file_==0) {
00053     char fname[4096];
00054     snprintf(fname,4096, namePattern_.c_str(),e.id().run());
00055     edm::LogInfo("HCAL") << "Opening " << fname << " for writing HCAL-format file.";
00056     file_=new TFile(fname,"RECREATE");
00057     ri_.setInfo("OriginalFile",fname);
00058     buildTree(*raw);
00059   }
00060 
00061   // adopt the buffers for writing
00062   for (std::map<int,int>::const_iterator i=chunkMap_.begin(); i!=chunkMap_.end(); i++) {
00063     CDFChunk* c=chunkList_[i->second];
00064     const FEDRawData& frd=raw->FEDData(i->first);    
00065     c->adoptBuffer((ULong64_t*)frd.data(),frd.size()/8);
00066   }
00067 
00068   // copy the event info bits
00069   extractEventInfo(*raw,e.id());
00070 
00071   // fill the tree
00072   tree_->Fill();
00073   // release all the buffers
00074   for (std::map<int,int>::const_iterator i=chunkMap_.begin(); i!=chunkMap_.end(); i++) {
00075     CDFChunk* c=chunkList_[i->second];
00076     c->releaseBuffer();
00077   }
00078 
00079 }
00080 
00081 void HcalTBWriter::buildTree(const FEDRawDataCollection& raw) {
00082   tree_=new TTree("CMSRAW","CMS Common Data Format Tree");
00083   chunkMap_.clear();
00084   trigChunk_=-1;
00085   int j=0;
00086   for (int i=0; i<2048; i++) {
00087     const FEDRawData& frd=raw.FEDData(i);
00088     if (frd.size()<16) continue; // it's empty... like
00089     
00090     std::string name;
00091     if (blockToName_.find(i)!=blockToName_.end()) name=blockToName_[i];
00092     else {
00093       char sname[64];
00094       snprintf(sname,64,"Chunk%03d",i);
00095       name=sname;
00096     }
00097     
00098     CDFChunk* c=new CDFChunk(name.c_str());
00099     chunkList_[j]=c;
00100     tree_->Branch(name.c_str(),"CDFChunk",&(chunkList_[j]));
00101     chunkMap_[i]=j;
00102 
00103     if (name=="HCAL_Trigger" || name=="SliceTest_Trigger") trigChunk_=j;
00104 
00105     j++;
00106   }
00107   eventInfo_=new CDFEventInfo();
00108   tree_->Branch("CDFEventInfo","CDFEventInfo",&eventInfo_,16000,2);
00109 }
00110 
00111 typedef struct StandardTrgMsgBlkStruct {
00112   uint32_t orbitNumber;
00113   uint32_t eventNumber;
00114   uint32_t flags_daq_ttype;
00115   uint32_t algo_bits_3;
00116   uint32_t algo_bits_2;
00117   uint32_t algo_bits_1;
00118   uint32_t algo_bits_0;
00119   uint32_t tech_bits;
00120   uint32_t gps_1234;
00121   uint32_t gps_5678;
00122 } StandardTrgMsgBlk;
00123 
00124 typedef struct newExtendedTrgMsgBlkStruct {
00125   StandardTrgMsgBlk stdBlock;
00126   uint32_t triggerWord;
00127   uint32_t triggerTime_usec;
00128   uint32_t triggerTime_base;
00129   uint32_t spillNumber;
00130   uint32_t runNumber;
00131   char     runNumberSequenceId[16];
00132   uint32_t eventStatus;
00133 } newExtendedTrgMsgBlk;
00134 
00135 void HcalTBWriter::extractEventInfo(const FEDRawDataCollection& raw, const edm::EventID& id) {
00136   int runno=id.run();
00137   const char* seqid="";
00138   int eventNo=id.event();
00139   int l1aNo=eventNo;
00140   int orbitNo=0;
00141   int bunchNo=0;
00142 
00143   if (trigChunk_>=0) {
00144     const newExtendedTrgMsgBlk* tinfo=(const newExtendedTrgMsgBlk*)(chunkList_[trigChunk_]->getData()+2); // assume 2 64-bit words for the CDF header
00145     orbitNo=tinfo->stdBlock.orbitNumber;
00146     seqid=tinfo->runNumberSequenceId;
00147     FEDHeader head((const unsigned char*)chunkList_[trigChunk_]->getData());
00148     bunchNo=head.bxID();
00149     l1aNo=head.lvl1ID();
00150   }
00151   
00152   eventInfo_->Set(runno,seqid,eventNo,l1aNo,orbitNo,bunchNo); 
00153 }