CMS 3D CMS Logo

HcalTBSource.cc

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   //  try {
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         edm::LogInfo("HCAL") << "Also reading branch " << b->GetName();
00097       }
00098       
00099       m_chunks[n_chunks]=0; // allow ROOT to allocate 
00100       b->SetAddress(&(m_chunks[n_chunks]));
00101       m_chunkIds[n_chunks]=m_sourceIdRemap[b->GetName()];
00102       n_chunks++;
00103     }
00104   }
00105   m_i=0;
00106 }
00107 
00108 void HcalTBSource::setRunAndEventInfo() {
00109   bool is_new=false;
00110 
00111   while (m_tree==0 || m_i==m_tree->GetEntries()) {
00112     m_fileCounter++;
00113     if (m_file!=0) {
00114        m_file->Close();
00115        m_file=0; 
00116        m_tree=0;
00117     }
00118     if (m_fileCounter>=int(fileNames().size())) return; // nothing good
00119     openFile(fileNames()[m_fileCounter]);
00120     is_new=true;
00121   }
00122 
00123   if (m_tree==0 || m_i==m_tree->GetEntries()) return; //nothing good
00124 
00125   m_tree->GetEntry(m_i);
00126   m_i++;
00127 
00128   if (m_eventInfo!=0) {
00129     if (is_new) {
00130       if (m_eventInfo->getEventNumber()==0) m_eventNumberOffset=1;
00131       else m_eventNumberOffset=0;
00132     }
00133     if (m_eventInfo->getRunNumber()==0) setRunNumber(1); // ZERO is unacceptable from a technical point of view
00134     else setRunNumber(m_eventInfo->getRunNumber());
00135     setEventNumber(m_eventInfo->getEventNumber()+m_eventNumberOffset);
00136   } else {
00137     setRunNumber(m_fileCounter+10);
00138     setEventNumber(m_i+1);
00139   }  
00140   // time is a hack
00141   edm::TimeValue_t present_time = presentTime();
00142   unsigned long time_between_events = timeBetweenEvents();
00143 
00144   setTime(present_time + time_between_events);
00145 }
00146 
00147 bool HcalTBSource::produce(edm::Event& e) {
00148 
00149   if (m_tree==0) {
00150     edm::LogError("HCAL") << "Null TTree";
00151     return false;
00152   }
00153 
00154   std::auto_ptr<FEDRawDataCollection> bare_product(new  FEDRawDataCollection());
00155   for (int i=0; i<n_chunks; i++) {
00156     const unsigned char* data=(const unsigned char*)m_chunks[i]->getData();
00157     int len=m_chunks[i]->getDataLength()*8;
00158 
00159     int natId=m_chunks[i]->getSourceId(); 
00160     int id=(m_chunkIds[i]>0)?(m_chunkIds[i]):(natId);
00161     
00162     FEDRawData& fed=bare_product->FEDData(id);
00163     fed.resize(len);
00164     memcpy(fed.data(),data,len);
00165 
00166     // patch the SourceId...
00167     if (natId!=id) {
00168       unsigned int* header=(unsigned int*)fed.data();
00169       header[0]=(header[0]&0xFFF000FFu)|(id<<8);
00170       // TODO: patch CRC after this change!
00171     }
00172     if (!m_quiet) 
00173       edm::LogInfo("HCAL") << "Reading " << len << " bytes for FED " << id << std::endl;
00174   }
00175 
00176   e.put(bare_product);
00177 
00178   return true;
00179 }
00180 
00181 

Generated on Tue Jun 9 17:39:25 2009 for CMSSW by  doxygen 1.5.4