Go to the documentation of this file.00001 #include "TFile.h"
00002 #include "TTree.h"
00003 #include "IORawData/HcalTBInputService/interface/HcalTBSource.h"
00004 #include "CDFChunk.h"
00005 #include "CDFEventInfo.h"
00006 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00007 #include "FWCore/PluginManager/interface/PluginCapabilities.h"
00008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00009 #include <iostream>
00010
00011 ClassImp(CDFChunk)
00012 ClassImp(CDFEventInfo)
00013
00014 using namespace edm;
00015 using namespace std;
00016
00017 HcalTBSource::HcalTBSource(const edm::ParameterSet & pset, edm::InputSourceDescription const& desc) :
00018 edm::ExternalInputSource(pset,desc),
00019 m_quiet( pset.getUntrackedParameter<bool>("quiet",true)),
00020 m_onlyRemapped( pset.getUntrackedParameter<bool>("onlyRemapped",false))
00021 {
00022 m_tree=0;
00023 m_fileCounter=-1;
00024 m_file=0;
00025 m_i=0;
00026
00027 unpackSetup(pset.getUntrackedParameter<std::vector<std::string> >("streams",std::vector<std::string>()));
00028 produces<FEDRawDataCollection>();
00029 }
00030
00031 void HcalTBSource::unpackSetup(const std::vector<std::string>& params) {
00032 for (std::vector<std::string>::const_iterator i=params.begin(); i!=params.end(); i++) {
00033 unsigned long pos=i->find(':');
00034 std::string streamName=i->substr(0,pos);
00035 int remapTo=-1;
00036 if (pos!=std::string::npos)
00037 remapTo=atoi(i->c_str()+pos+1);
00038
00039 m_sourceIdRemap.insert(std::pair<std::string,int>(streamName,remapTo));
00040 if (remapTo!=-1)
00041 edm::LogInfo("HCAL") << streamName << " --> " << remapTo << endl;
00042 else
00043 edm::LogInfo("HCAL") << streamName << " using fedid in file" << endl;
00044 }
00045 }
00046
00047 HcalTBSource::~HcalTBSource() {
00048 if (m_file!=0) {
00049 m_file->Close();
00050 m_file=0;
00051 m_tree=0;
00052 }
00053 }
00054
00055 void HcalTBSource::openFile(const std::string& filename) {
00056 if (m_file!=0) {
00057 m_file->Close();
00058 m_file=0;
00059 m_tree=0;
00060 }
00061
00062
00063 m_file=TFile::Open(filename.c_str());
00064 if (m_file==0) {
00065 edm::LogError("HCAL") << "Unable to open " << filename << endl;
00066 m_tree=0;
00067 return;
00068 }
00069
00070 m_tree=(TTree*)m_file->Get("CMSRAW");
00071
00072 if (m_tree==0) {
00073 m_file->Close();
00074 m_file=0;
00075 edm::LogError("HCAL") << "Unable to find CMSRAW tree" << endl;
00076 return;
00077 }
00078
00079 if (!m_quiet) {
00080 edm::LogInfo("HCAL") << "Opening '" << filename << "' with " << m_tree->GetEntries() << " events.\n";
00081 }
00082
00083 TObjArray* lb=m_tree->GetListOfBranches();
00084 n_chunks=0;
00085 for (int i=0; i<lb->GetSize(); i++) {
00086 TBranch* b=(TBranch*)lb->At(i);
00087 if (b==0) continue;
00088 if (!strcmp(b->GetClassName(),"CDFEventInfo")) {
00089 m_eventInfo=0;
00090 b->SetAddress(&m_eventInfo);
00091 } else {
00092 if (strcmp(b->GetClassName(),"CDFChunk")) continue;
00093 if (m_sourceIdRemap.find(b->GetName())==m_sourceIdRemap.end()) {
00094 if (m_onlyRemapped) continue;
00095 m_sourceIdRemap.insert(std::pair<std::string,int>(b->GetName(),-1));
00096 if (!m_quiet)
00097 edm::LogInfo("HCAL") << "Also reading branch " << b->GetName();
00098 }
00099
00100 m_chunks[n_chunks]=0;
00101 b->SetAddress(&(m_chunks[n_chunks]));
00102 m_chunkIds[n_chunks]=m_sourceIdRemap[b->GetName()];
00103 n_chunks++;
00104 }
00105 }
00106 m_i=0;
00107 }
00108
00109 void HcalTBSource::setRunAndEventInfo() {
00110 bool is_new=false;
00111
00112 while (m_tree==0 || m_i==m_tree->GetEntries()) {
00113 m_fileCounter++;
00114 if (m_file!=0) {
00115 m_file->Close();
00116 m_file=0;
00117 m_tree=0;
00118 }
00119 if (m_fileCounter>=int(fileNames().size())) return;
00120 openFile(fileNames()[m_fileCounter]);
00121 is_new=true;
00122 }
00123
00124 if (m_tree==0 || m_i==m_tree->GetEntries()) return;
00125
00126 m_tree->GetEntry(m_i);
00127 m_i++;
00128
00129 if (m_eventInfo!=0) {
00130 if (is_new) {
00131 if (m_eventInfo->getEventNumber()==0) m_eventNumberOffset=1;
00132 else m_eventNumberOffset=0;
00133 }
00134 if (m_eventInfo->getRunNumber()==0) setRunNumber(1);
00135 else setRunNumber(m_eventInfo->getRunNumber());
00136 setEventNumber(m_eventInfo->getEventNumber()+m_eventNumberOffset);
00137 } else {
00138 setRunNumber(m_fileCounter+10);
00139 setEventNumber(m_i+1);
00140 }
00141
00142 edm::TimeValue_t present_time = presentTime();
00143 unsigned long time_between_events = timeBetweenEvents();
00144
00145 setTime(present_time + time_between_events);
00146 }
00147
00148 bool HcalTBSource::produce(edm::Event& e) {
00149
00150 if (m_fileCounter>=int(fileNames().size())) return false;
00151
00152 if (m_tree==0) {
00153 edm::LogError("HCAL") << "Null TTree";
00154 return false;
00155 }
00156
00157 std::auto_ptr<FEDRawDataCollection> bare_product(new FEDRawDataCollection());
00158 for (int i=0; i<n_chunks; i++) {
00159 const unsigned char* data=(const unsigned char*)m_chunks[i]->getData();
00160 int len=m_chunks[i]->getDataLength()*8;
00161
00162 int natId=m_chunks[i]->getSourceId();
00163 int id=(m_chunkIds[i]>0)?(m_chunkIds[i]):(natId);
00164
00165 FEDRawData& fed=bare_product->FEDData(id);
00166 fed.resize(len);
00167 memcpy(fed.data(),data,len);
00168
00169
00170 if (natId!=id) {
00171 unsigned int* header=(unsigned int*)fed.data();
00172 header[0]=(header[0]&0xFFF000FFu)|(id<<8);
00173
00174 }
00175 if (!m_quiet)
00176 edm::LogInfo("HCAL") << "Reading " << len << " bytes for FED " << id << std::endl;
00177 }
00178
00179 e.put(bare_product);
00180
00181 return true;
00182 }
00183
00184