CMS 3D CMS Logo

HcalPatternSource.cc

Go to the documentation of this file.
00001 #include "IORawData/CaloPatterns/src/HcalPatternSource.h"
00002 #include "IORawData/CaloPatterns/interface/HcalPatternXMLParser.h"
00003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00004 #include "FWCore/Framework/interface/Event.h"
00005 #include "FWCore/Framework/interface/EventSetup.h"
00006 #include "FWCore/Framework/interface/ESHandle.h"
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008 #include "CondFormats/DataRecord/interface/HcalElectronicsMapRcd.h"
00009 #include "CondFormats/HcalObjects/interface/HcalElectronicsMap.h"
00010 #include "DataFormats/HcalDigi/interface/HcalDigiCollections.h"
00011 #include <wordexp.h>
00012 
00013 HcalPatternSource::HcalPatternSource(const edm::ParameterSet & pset, edm::InputSourceDescription const& desc) : 
00014   edm::ConfigurableInputSource(pset,desc),
00015   bunches_(pset.getUntrackedParameter<std::vector<int> >("Bunches",std::vector<int>())),
00016   presamples_(pset.getUntrackedParameter<int>("Presamples",4)),
00017   samples_(pset.getUntrackedParameter<int>("Samples",10))
00018 {
00019   loadPatterns(pset.getUntrackedParameter<std::string>("Patterns"));
00020   produces<HBHEDigiCollection>();
00021   produces<HODigiCollection>();
00022   produces<HFDigiCollection>();
00023 }
00024 
00025 void HcalPatternSource::beginJob(edm::EventSetup const& es) {
00026   edm::ESHandle<HcalElectronicsMap> item;
00027   es.get<HcalElectronicsMapRcd>().get(item);
00028   elecmap_=item.product();
00029 }
00030 
00031 bool HcalPatternSource::produce(edm::Event& e) {
00032   if (e.id().event()>bunches_.size()) return false;
00033 
00034   std::auto_ptr<HBHEDigiCollection> hbhe(new HBHEDigiCollection());
00035   std::auto_ptr<HFDigiCollection> hf(new HFDigiCollection());
00036   std::auto_ptr<HODigiCollection> ho(new HODigiCollection());
00037 
00038   int bc=bunches_[e.id().event()-1];
00039   for (std::vector<HcalFiberPattern>::iterator i=patterns_.begin(); i!=patterns_.end(); i++) {
00040     std::vector<HcalQIESample> samples;
00041     for (int fc=0; fc<3; fc++) {
00042       samples=i->getSamples(bc,presamples_, samples_, fc);
00043       HcalElectronicsId eid=i->getId(fc);
00044       HcalDetId did(elecmap_->lookup(eid));
00045 
00046       if (did.null()) {
00047         edm::LogWarning("HCAL") << "No electronics map match for id " << eid;
00048         continue;
00049       }
00050 
00051       switch (did.subdet()) {
00052       case (HcalBarrel):
00053       case (HcalEndcap):
00054         hbhe->push_back(HBHEDataFrame(did));
00055         hbhe->back().setSize(samples_);
00056         hbhe->back().setPresamples(presamples_);
00057         for (int i=0; i<samples_; i++) hbhe->back().setSample(i,samples[i]);
00058         hbhe->back().setReadoutIds(eid);
00059         break;
00060       case (HcalForward):
00061         hf->push_back(HFDataFrame(did));
00062         hf->back().setSize(samples_);
00063         hf->back().setPresamples(presamples_);
00064         for (int i=0; i<samples_; i++) hf->back().setSample(i,samples[i]);
00065         hf->back().setReadoutIds(eid);
00066         break;
00067       case (HcalOuter) :
00068         ho->push_back(HODataFrame(did));
00069         ho->back().setSize(samples_);
00070         ho->back().setPresamples(presamples_);
00071         for (int i=0; i<samples_; i++) ho->back().setSample(i,samples[i]);
00072         ho->back().setReadoutIds(eid);
00073         break;
00074       default: continue;
00075       }
00076     }        
00077   }
00078   hbhe->sort();
00079   ho->sort();
00080   hf->sort();
00081   
00082   e.put(hbhe);
00083   e.put(ho);
00084   e.put(hf);
00085   return true;
00086 }
00087 
00088 void HcalPatternSource::loadPatterns(const std::string& patspec) {
00089   wordexp_t p;
00090   char** files;
00091 
00092   wordexp(patspec.c_str(),&p, WRDE_NOCMD); // do not run shell commands!
00093   files=p.we_wordv;
00094   for (unsigned int i=0; i<p.we_wordc; i++) {
00095     LogDebug ("HCAL") << "Reading pattern file '" << files[i] << "'";
00096     loadPatternFile(files[i]);
00097     LogDebug ("HCAL") << "Fibers so far " << patterns_.size();
00098   }
00099   wordfree(&p);
00100 }
00101 
00102 void HcalPatternSource::loadPatternFile(const std::string& filename) {
00103   HcalPatternXMLParser parser;
00104   std::string buffer, element;
00105   std::map<std::string,std::string> params;
00106   std::vector<uint32_t> data;
00107 
00108   FILE* f=fopen(filename.c_str(), "r");
00109   if (f==0) return;
00110   else {
00111     char block[4096];
00112     while (!feof(f)) {
00113       int read=fread(block,1,4096,f);
00114       buffer.append(block,block+read);
00115     }  
00116     fclose(f);
00117   }
00118   if (buffer.find("<?xml")!=0) {
00119     throw cms::Exception("InvalidFormat") << "Not a valid XML file: " << filename;
00120   }
00121   std::string::size_type i=0,j;
00122   while (buffer.find("<CFGBrick>",i)!=std::string::npos) {
00123     i=buffer.find("<CFGBrick>",i);
00124     j=buffer.find("</CFGBrick>",i);
00125     element="<?xml version='1.0'?>\n";
00126     element.append(buffer,i,j-i);
00127     element.append("</CFGBrick>");
00128     //    LogDebug("HCAL") << element;
00129     params.clear();
00130     data.clear();
00131     parser.parse(element,params,data);
00132     patterns_.push_back(HcalFiberPattern(params,data));
00133     i=j+5;
00134   }
00135   
00136   
00137 }

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