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 #include <cstdio>
00013
00014 HcalPatternSource::HcalPatternSource(const edm::ParameterSet & pset) :
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::produce(edm::Event& e, const edm::EventSetup& es) {
00026 if (e.id().event()>bunches_.size()) return;
00027
00028 edm::ESHandle<HcalElectronicsMap> item;
00029 es.get<HcalElectronicsMapRcd>().get(item);
00030 const HcalElectronicsMap *elecmap=item.product();
00031
00032 std::auto_ptr<HBHEDigiCollection> hbhe(new HBHEDigiCollection());
00033 std::auto_ptr<HFDigiCollection> hf(new HFDigiCollection());
00034 std::auto_ptr<HODigiCollection> ho(new HODigiCollection());
00035
00036 int bc=bunches_[e.id().event()-1];
00037 for (std::vector<HcalFiberPattern>::iterator i=patterns_.begin(); i!=patterns_.end(); i++) {
00038 std::vector<HcalQIESample> samples;
00039 for (int fc=0; fc<3; fc++) {
00040 samples=i->getSamples(bc,presamples_, samples_, fc);
00041 HcalElectronicsId eid=i->getId(fc);
00042
00043 HcalDetId did(elecmap->lookup(eid));
00044
00045 if (did.null()) {
00046 edm::LogWarning("HCAL") << "No electronics map match for id " << eid;
00047 continue;
00048 }
00049
00050 switch (did.subdet()) {
00051 case (HcalBarrel):
00052 case (HcalEndcap):
00053 hbhe->push_back(HBHEDataFrame(did));
00054 hbhe->back().setSize(samples_);
00055 hbhe->back().setPresamples(presamples_);
00056 for (int i=0; i<samples_; i++) hbhe->back().setSample(i,samples[i]);
00057 hbhe->back().setReadoutIds(eid);
00058 break;
00059 case (HcalForward):
00060 hf->push_back(HFDataFrame(did));
00061 hf->back().setSize(samples_);
00062 hf->back().setPresamples(presamples_);
00063 for (int i=0; i<samples_; i++) hf->back().setSample(i,samples[i]);
00064 hf->back().setReadoutIds(eid);
00065 break;
00066 case (HcalOuter) :
00067 ho->push_back(HODataFrame(did));
00068 ho->back().setSize(samples_);
00069 ho->back().setPresamples(presamples_);
00070 for (int i=0; i<samples_; i++) ho->back().setSample(i,samples[i]);
00071 ho->back().setReadoutIds(eid);
00072 break;
00073 default: continue;
00074 }
00075 }
00076 }
00077 hbhe->sort();
00078 ho->sort();
00079 hf->sort();
00080
00081 e.put(hbhe);
00082 e.put(ho);
00083 e.put(hf);
00084 }
00085
00086 void HcalPatternSource::loadPatterns(const std::string& patspec) {
00087 wordexp_t p;
00088 char** files;
00089 wordexp(patspec.c_str(),&p, WRDE_NOCMD);
00090 files=p.we_wordv;
00091 for (unsigned int i=0; i<p.we_wordc; i++) {
00092 LogDebug ("HCAL") << "Reading pattern file '" << files[i] << "'";
00093 loadPatternFile(files[i]);
00094 LogDebug ("HCAL") << "Fibers so far " << patterns_.size();
00095 }
00096 wordfree(&p);
00097 }
00098
00099 void HcalPatternSource::loadPatternFile(const std::string& filename) {
00100 HcalPatternXMLParser parser;
00101 std::string buffer, element;
00102 std::map<std::string,std::string> params;
00103 std::vector<uint32_t> data;
00104 FILE* f=fopen(filename.c_str(), "r");
00105 if (f==0) return;
00106 else {
00107 char block[4096];
00108 while (!feof(f)) {
00109 int read=fread(block,1,4096,f);
00110 buffer.append(block,block+read);
00111 }
00112 fclose(f);
00113 }
00114 if (buffer.find("<?xml")!=0) {
00115 throw cms::Exception("InvalidFormat") << "Not a valid XML file: " << filename;
00116 }
00117 std::string::size_type i=0,j;
00118 while (buffer.find("<CFGBrick>",i)!=std::string::npos) {
00119 i=buffer.find("<CFGBrick>",i);
00120 j=buffer.find("</CFGBrick>",i);
00121 element="<?xml version='1.0'?>\n";
00122 element.append(buffer,i,j-i);
00123 element.append("</CFGBrick>");
00124
00125 params.clear();
00126 data.clear();
00127 parser.parse(element,params,data);
00128 patterns_.push_back(HcalFiberPattern(params,data));
00129 i=j+5;
00130 }
00131
00132
00133 }