CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/IORawData/HcalTBInputService/src/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::ProducerSourceFromFiles(pset,desc, true),
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         if (!m_quiet)
00097           edm::LogInfo("HCAL") << "Also reading branch " << b->GetName();
00098       }
00099       
00100       m_chunks[n_chunks]=0; // allow ROOT to allocate 
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 bool HcalTBSource::setRunAndEventInfo(EventID& id, TimeValue_t& time) {
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 false; // nothing good
00120     openFile(fileNames()[m_fileCounter]);
00121     is_new=true;
00122   }
00123 
00124   if (m_tree==0 || m_i==m_tree->GetEntries()) return false; //nothing good
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     // ZERO is unacceptable for a run number from a technical point of view
00135     id = EventID((m_eventInfo->getRunNumber()==0 ? 1 : m_eventInfo->getRunNumber()), id.luminosityBlock(), m_eventInfo->getEventNumber()+m_eventNumberOffset); 
00136   } else {
00137     id = EventID(m_fileCounter+10, id.luminosityBlock(), m_i+1);
00138   }  
00139   // time is a hack
00140   edm::TimeValue_t present_time = presentTime();
00141   unsigned long time_between_events = timeBetweenEvents();
00142 
00143   time = present_time + time_between_events;
00144   return true;
00145 }
00146 
00147 void HcalTBSource::produce(edm::Event& e) {
00148 
00149   std::auto_ptr<FEDRawDataCollection> bare_product(new  FEDRawDataCollection());
00150   for (int i=0; i<n_chunks; i++) {
00151     const unsigned char* data=(const unsigned char*)m_chunks[i]->getData();
00152     int len=m_chunks[i]->getDataLength()*8;
00153 
00154     int natId=m_chunks[i]->getSourceId(); 
00155     int id=(m_chunkIds[i]>0)?(m_chunkIds[i]):(natId);
00156     
00157     FEDRawData& fed=bare_product->FEDData(id);
00158     fed.resize(len);
00159     memcpy(fed.data(),data,len);
00160 
00161     // patch the SourceId...
00162     if (natId!=id) {
00163       unsigned int* header=(unsigned int*)fed.data();
00164       header[0]=(header[0]&0xFFF000FFu)|(id<<8);
00165       // TODO: patch CRC after this change!
00166     }
00167     if (!m_quiet) 
00168       edm::LogInfo("HCAL") << "Reading " << len << " bytes for FED " << id << std::endl;
00169   }
00170 
00171   e.put(bare_product);
00172 }
00173 
00174