00001 using namespace std;
00002 #include "EventFilter/HcalRawToDigi/plugins/HcalRawToDigi.h"
00003 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00004 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00005 #include "DataFormats/HcalDigi/interface/HcalDigiCollections.h"
00006 #include "FWCore/Framework/interface/ESHandle.h"
00007 #include "CalibFormats/HcalObjects/interface/HcalDbService.h"
00008 #include "CalibFormats/HcalObjects/interface/HcalDbRecord.h"
00009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00010 #include <iostream>
00011
00012 HcalRawToDigi::HcalRawToDigi(edm::ParameterSet const& conf):
00013 dataTag_(conf.getParameter<edm::InputTag>("InputLabel")),
00014 unpacker_(conf.getUntrackedParameter<int>("HcalFirstFED",int(FEDNumbering::MINHCALFEDID)),conf.getParameter<int>("firstSample"),conf.getParameter<int>("lastSample")),
00015 filter_(conf.getParameter<bool>("FilterDataQuality"),conf.getParameter<bool>("FilterDataQuality"),
00016 false,
00017 0, 0,
00018 -1),
00019 fedUnpackList_(conf.getUntrackedParameter<std::vector<int> >("FEDs", std::vector<int>())),
00020 firstFED_(conf.getUntrackedParameter<int>("HcalFirstFED",FEDNumbering::MINHCALFEDID)),
00021 unpackCalib_(conf.getUntrackedParameter<bool>("UnpackCalib",false)),
00022 unpackZDC_(conf.getUntrackedParameter<bool>("UnpackZDC",false)),
00023 unpackTTP_(conf.getUntrackedParameter<bool>("UnpackTTP",false)),
00024 silent_(conf.getUntrackedParameter<bool>("silent",true)),
00025 complainEmptyData_(conf.getUntrackedParameter<bool>("ComplainEmptyData",false)),
00026 unpackerMode_(conf.getUntrackedParameter<int>("UnpackerMode",0)),
00027 expectedOrbitMessageTime_(conf.getUntrackedParameter<int>("ExpectedOrbitMessageTime",-1))
00028 {
00029 if (fedUnpackList_.empty()) {
00030 for (int i=FEDNumbering::MINHCALFEDID; i<=FEDNumbering::MAXHCALFEDID; i++)
00031 fedUnpackList_.push_back(i);
00032 }
00033
00034 unpacker_.setExpectedOrbitMessageTime(expectedOrbitMessageTime_);
00035 unpacker_.setMode(unpackerMode_);
00036 std::ostringstream ss;
00037 for (unsigned int i=0; i<fedUnpackList_.size(); i++)
00038 ss << fedUnpackList_[i] << " ";
00039 edm::LogInfo("HCAL") << "HcalRawToDigi will unpack FEDs ( " << ss.str() << ")";
00040
00041
00042 produces<HBHEDigiCollection>();
00043 produces<HFDigiCollection>();
00044 produces<HODigiCollection>();
00045 produces<HcalTrigPrimDigiCollection>();
00046 produces<HOTrigPrimDigiCollection>();
00047 produces<HcalUnpackerReport>();
00048 if (unpackCalib_)
00049 produces<HcalCalibDigiCollection>();
00050 if (unpackZDC_)
00051 produces<ZDCDigiCollection>();
00052 if (unpackTTP_)
00053 produces<HcalTTPDigiCollection>();
00054
00055 memset(&stats_,0,sizeof(stats_));
00056
00057 }
00058
00059
00060 HcalRawToDigi::~HcalRawToDigi() { }
00061
00062
00063 void HcalRawToDigi::produce(edm::Event& e, const edm::EventSetup& es)
00064 {
00065
00066 edm::Handle<FEDRawDataCollection> rawraw;
00067 e.getByLabel(dataTag_,rawraw);
00068
00069 edm::ESHandle<HcalDbService> pSetup;
00070 es.get<HcalDbRecord>().get( pSetup );
00071 const HcalElectronicsMap* readoutMap=pSetup->getHcalMapping();
00072
00073
00074 std::vector<HBHEDataFrame> hbhe;
00075 std::vector<HODataFrame> ho;
00076 std::vector<HFDataFrame> hf;
00077 std::vector<HcalTriggerPrimitiveDigi> htp;
00078 std::vector<HcalCalibDataFrame> hc;
00079 std::vector<ZDCDataFrame> zdc;
00080 std::vector<HcalTTPDigi> ttp;
00081 std::vector<HOTriggerPrimitiveDigi> hotp;
00082 std::auto_ptr<HcalUnpackerReport> report(new HcalUnpackerReport);
00083
00084
00085 if (stats_.max_hbhe>0) hbhe.reserve(stats_.ave_hbhe+(stats_.max_hbhe-stats_.ave_hbhe)/8);
00086 if (stats_.max_ho>0) ho.reserve(stats_.ave_ho+(stats_.max_ho-stats_.ave_ho)/8);
00087 if (stats_.max_hf>0) hf.reserve(stats_.ave_hf+(stats_.max_hf-stats_.ave_hf)/8);
00088 if (stats_.max_calib>0) hc.reserve(stats_.ave_calib+(stats_.max_calib-stats_.ave_calib)/8);
00089 if (stats_.max_tp>0) htp.reserve(stats_.ave_tp+(stats_.max_tp-stats_.ave_tp)/8);
00090 if (stats_.max_tpho>0) hotp.reserve(stats_.ave_tpho+(stats_.max_tpho-stats_.ave_tpho)/8);
00091
00092 if (unpackZDC_) zdc.reserve(24);
00093
00094
00095 HcalUnpacker::Collections colls;
00096 colls.hbheCont=&hbhe;
00097 colls.hoCont=&ho;
00098 colls.hfCont=&hf;
00099 colls.tpCont=&htp;
00100 colls.tphoCont=&hotp;
00101 colls.calibCont=&hc;
00102 colls.zdcCont=&zdc;
00103 if (unpackTTP_) colls.ttp=&ttp;
00104
00105
00106 for (std::vector<int>::const_iterator i=fedUnpackList_.begin(); i!=fedUnpackList_.end(); i++) {
00107 const FEDRawData& fed = rawraw->FEDData(*i);
00108 if (fed.size()==0) {
00109 if (complainEmptyData_) {
00110 if (!silent_) edm::LogWarning("EmptyData") << "No data for FED " << *i;
00111 report->addError(*i);
00112 }
00113 } else if (fed.size()<8*3) {
00114 if (!silent_) edm::LogWarning("EmptyData") << "Tiny data " << fed.size() << " for FED " << *i;
00115 report->addError(*i);
00116 } else {
00117 try {
00118 unpacker_.unpack(fed,*readoutMap,colls, *report,silent_);
00119 report->addUnpacked(*i);
00120 } catch (cms::Exception& e) {
00121 if (!silent_) edm::LogWarning("Unpacking error") << e.what();
00122 report->addError(*i);
00123 } catch (...) {
00124 if (!silent_) edm::LogWarning("Unpacking exception");
00125 report->addError(*i);
00126 }
00127 }
00128 }
00129
00130
00131
00132 stats_.max_hbhe=std::max(stats_.max_hbhe,(int)hbhe.size());
00133 stats_.ave_hbhe=(stats_.ave_hbhe*stats_.n+hbhe.size())/(stats_.n+1);
00134 stats_.max_ho=std::max(stats_.max_ho,(int)ho.size());
00135 stats_.ave_ho=(stats_.ave_ho*stats_.n+ho.size())/(stats_.n+1);
00136 stats_.max_hf=std::max(stats_.max_hf,(int)hf.size());
00137 stats_.ave_hf=(stats_.ave_hf*stats_.n+hf.size())/(stats_.n+1);
00138 stats_.max_tp=std::max(stats_.max_tp,(int)htp.size());
00139 stats_.ave_tp=(stats_.ave_tp*stats_.n+htp.size())/(stats_.n+1);
00140 stats_.max_tpho=std::max(stats_.max_tpho,(int)hotp.size());
00141 stats_.ave_tpho=(stats_.ave_tpho*stats_.n+hotp.size())/(stats_.n+1);
00142 stats_.max_calib=std::max(stats_.max_calib,(int)hc.size());
00143 stats_.ave_calib=(stats_.ave_calib*stats_.n+hc.size())/(stats_.n+1);
00144
00145
00146 stats_.n++;
00147
00148
00149 std::auto_ptr<HBHEDigiCollection> hbhe_prod(new HBHEDigiCollection());
00150 std::auto_ptr<HFDigiCollection> hf_prod(new HFDigiCollection());
00151 std::auto_ptr<HODigiCollection> ho_prod(new HODigiCollection());
00152 std::auto_ptr<HcalTrigPrimDigiCollection> htp_prod(new HcalTrigPrimDigiCollection());
00153 std::auto_ptr<HOTrigPrimDigiCollection> hotp_prod(new HOTrigPrimDigiCollection());
00154
00155 hbhe_prod->swap_contents(hbhe);
00156 hf_prod->swap_contents(hf);
00157 ho_prod->swap_contents(ho);
00158 htp_prod->swap_contents(htp);
00159 hotp_prod->swap_contents(hotp);
00160
00161
00162 if (filter_.active()) {
00163 HBHEDigiCollection filtered_hbhe=filter_.filter(*hbhe_prod,*report);
00164 HODigiCollection filtered_ho=filter_.filter(*ho_prod,*report);
00165 HFDigiCollection filtered_hf=filter_.filter(*hf_prod,*report);
00166
00167 hbhe_prod->swap(filtered_hbhe);
00168 ho_prod->swap(filtered_ho);
00169 hf_prod->swap(filtered_hf);
00170 }
00171
00172
00173
00174
00175 hbhe_prod->sort();
00176 ho_prod->sort();
00177 hf_prod->sort();
00178 htp_prod->sort();
00179 hotp_prod->sort();
00180
00181 e.put(hbhe_prod);
00182 e.put(ho_prod);
00183 e.put(hf_prod);
00184 e.put(htp_prod);
00185 e.put(hotp_prod);
00186
00188 if (unpackCalib_) {
00189 std::auto_ptr<HcalCalibDigiCollection> hc_prod(new HcalCalibDigiCollection());
00190 hc_prod->swap_contents(hc);
00191
00192 if (filter_.active()) {
00193 HcalCalibDigiCollection filtered_calib=filter_.filter(*hc_prod,*report);
00194 hc_prod->swap(filtered_calib);
00195 }
00196
00197 hc_prod->sort();
00198 e.put(hc_prod);
00199 }
00200
00202 if (unpackZDC_) {
00203 std::auto_ptr<ZDCDigiCollection> prod(new ZDCDigiCollection());
00204 prod->swap_contents(zdc);
00205
00206 if (filter_.active()) {
00207 ZDCDigiCollection filtered_zdc=filter_.filter(*prod,*report);
00208 prod->swap(filtered_zdc);
00209 }
00210
00211 prod->sort();
00212 e.put(prod);
00213 }
00214
00215 if (unpackTTP_) {
00216 std::auto_ptr<HcalTTPDigiCollection> prod(new HcalTTPDigiCollection());
00217 prod->swap_contents(ttp);
00218
00219 prod->sort();
00220 e.put(prod);
00221 }
00222 e.put(report);
00223
00224
00225 }
00226
00227