11 #include "FWCore/Framework/interface/InputSourceMacros.h"
12 #include "HGCal/TBStandaloneSimulator/plugins/HGCSimDigiSource.h"
13 #include "HGCal/DataFormats/interface/HGCalTBDetId.h"
19 const size_t debugCount = 1;
23 (
const edm::ParameterSet& pset, edm::InputSourceDescription
const& desc)
24 : edm::ProducerSourceFromFiles(pset, desc,
true),
25 _run(pset.getUntrackedParameter<
int>(
"runNumber", 101)),
26 _maxevents(pset.getUntrackedParameter<
int>(
"maxEvents", -1)),
27 _minadccount(pset.getUntrackedParameter<
int>(
"minADCCount", 1)),
28 _adcpermev(pset.getUntrackedParameter<
double>(
"ADCperMeV", 200)),
29 _filenames(pset.getUntrackedParameter<vector<string> >
31 _noisefilenames(pset.getUntrackedParameter<vector<string> >
45 _noise(vector<map<uint32_t, uint16_t> >())
48 produces<SKIROC2DigiCollection>();
49 produces<HGCSSSimHitVec>();
54 _chain =
new TChain(
"HGCSSTree");
56 throw cms::Exception(
"ChainCreationFailed",
"chain: HGCSSTree");
58 for(
size_t c = 0; c < _filenames.size(); c++)
59 _chain->Add(_filenames[c].c_str());
62 _entries = _chain->GetEntries();
63 _entries = _maxevents < 0
65 : (_entries < (size_t)_maxevents ? _entries : _maxevents);
67 <<
"==> Number of simulated events to read: "
72 _tree = (TTree*)_chain;
73 _tree->SetBranchAddress(
"HGCSSSimHitVec", &_simhits);
78 if ( _noisefilenames.size() > (size_t)0 ) {
79 _noisechain =
new TChain(
"Pedestals");
81 throw cms::Exception(
"ChainCreationFailed",
84 for(
size_t c = 0; c < _noisefilenames.size(); c++)
85 _noisechain->Add(_noisefilenames[c].c_str());
88 _noiseentries = _noisechain->GetEntries();
91 vector<uint32_t>* vkey = 0;
92 vector<uint16_t>* vadc = 0;
93 _noisetree = (TTree*)_noisechain;
94 _noisetree->SetBranchAddress(
"vkey", &vkey);
95 _noisetree->SetBranchAddress(
"vadc", &vadc);
97 cout <<
"==> Creating noise model from file with "
105 for(
size_t entry = 0; entry < _noiseentries; entry++) {
106 long localentry = _noisechain->LoadTree(entry);
107 _noisetree->GetEntry(localentry);
108 _noise.push_back(map<uint32_t, uint16_t>());
109 if ( entry % 500 == 0 )
110 cout << entry <<
"\t" << vkey->size() << endl;
112 for(
size_t c = 0; c < vkey->size(); c++) {
113 long key = (*vkey)[c];
114 uint16_t adc = (*vadc)[c];
115 _noise[entry][key] = adc;
123 a2 = sqrt(a2 - a1 * a1);
125 sprintf(rec,
"==> pedestal = %8.1f +/-%-8.1f", a1, a2);
127 cout <<
"==> Done loading noise model"
130 cout <<
"==> No noise will be added"
138 if ( _chain )
delete _chain;
139 if ( _noisechain )
delete _noisechain;
142 bool HGCSimDigiSource::
143 setRunAndEventInfo(edm::EventID&
id,
144 edm::TimeValue_t& time,
145 edm::EventAuxiliary::ExperimentType&)
148 throw cms::Exception(
"ChainNotFound")
149 <<
"sim file chain not open";
151 if ( _entry >= _entries )
return false;
154 long localentry = _chain->LoadTree(_entry);
155 _tree->GetEntry(localentry);
159 id = edm::EventID(_run, 1, _entry);
162 edm::TimeValue_t present_time = presentTime();
163 unsigned long time_between_events = timeBetweenEvents();
164 time = present_time + time_between_events;
169 void HGCSimDigiSource::produce(edm::Event& event)
171 if ( _entry < debugCount ) {
173 <<
"==> entry number: " << _entry
181 std::auto_ptr<HGCSSSimHitVec> simhits(
new HGCSSSimHitVec());
182 for(
size_t c = 0; c < _simhits->size(); c++)
183 simhits->push_back((*_simhits)[c]);
187 std::auto_ptr<SKIROC2DigiCollection>
192 vector<HGCSimDigiSource::Cell> channels;
196 if ( _entry < debugCount ) {
197 cout <<
" number of digi hits: " << channels.size() << endl;
200 for(
size_t c = 0; c < channels.size(); c++) {
202 digis->addDataFrame(cell.
detid);
203 digis->backDataFrame().setSample(0,
207 if ( _entry < debugCount ) {
210 "%6d (ski,chan)=(%2d,%2d)"
211 " (u,v)=(%2d,%2d), (x,y)=(%6.2f,%6.2f), ADC=(%d)",
215 cout << record << endl;
222 void HGCSimDigiSource::digitize(std::vector<HGCSimDigiSource::Cell>& channels)
231 if ( _entry < debugCount ) {
233 <<
" number of sim hits: "
234 << _simhits->size() << endl;
238 map<uint32_t, HGCSimDigiSource::Cell> hits;
240 for(
size_t c = 0; c < _simhits->size(); c++) {
241 HGCSSSimHit& simhit = (*_simhits)[c];
242 int cellid = simhit.cellid();
243 int layer = simhit.silayer() + 1;
247 pair<int, int> uv = _cellmap(cellid);
252 int celltype = _cellmap.celltype(layer,
256 HGCalTBDetId detid(layer, sensor_u, sensor_v, u, v, celltype);
257 uint32_t key = detid.rawId();
259 if ( hits.find(key) == hits.end() ) {
272 pair<double, double> xy = _cellmap.uv2xy(cell.
u,
277 pair<int, int> eid = _cellmap.uv2eid(layer,
290 hits[key].
energy += simhit.energy();
294 for(map<uint32_t, HGCSimDigiSource::Cell>::iterator it = hits.begin();
295 it != hits.end(); it++) {
296 uint32_t key = it->first;
297 double energy = hits[key].energy;
298 uint16_t adc =
static_cast<uint16_t
>(_adcpermev * energy);
299 hits[key].ADChigh = adc;
306 int index = _random.Integer(_noiseentries - 1);
307 map<uint32_t, uint16_t>& noise = _noise[index];
308 for(map<uint32_t, uint16_t>::iterator it = noise.begin();
309 it != noise.end(); it++) {
310 uint32_t key = it->first;
311 uint16_t adc = it->second;
312 if ( hits.find(key) != hits.end() )
313 hits[key].ADChigh += adc;
322 cell.
layer = detid.layer();
330 pair<int, int> eid = _cellmap.uv2eid(cell.
layer,
338 pair<double, double> xy = _cellmap.uv2xy(cell.
u, cell.
v);
348 for(map<uint32_t, HGCSimDigiSource::Cell>::iterator it = hits.begin();
349 it != hits.end(); it++) {
350 long key = it->first;
353 if ( cell.
ADChigh < _minadccount )
continue;
354 channels.push_back(cell);
359 sort(channels.begin(), channels.end());
363 (edm::ConfigurationDescriptions& descriptions)
365 edm::ParameterSetDescription desc;
366 desc.setComment(
"Test Beam 2016");
367 desc.addUntracked<
int>(
"runNumber", 101);
368 desc.addUntracked<
int>(
"maxEvents", -1);
369 desc.addUntracked<
int>(
"minADCCount", 1);
370 desc.addUntracked<
double>(
"ADCperMeV", 200);
371 desc.addUntracked<std::vector<std::string> >(
"fileNames");
372 desc.addUntracked<std::vector<std::string> >(
"noiseFileNames");
373 descriptions.add(
"source", desc);
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
HGCSimDigiSource(const edm::ParameterSet &pset, edm::InputSourceDescription const &desc)
virtual ~HGCSimDigiSource()
DEFINE_FWK_INPUT_SOURCE(HGCalTBTextSource)
HGCalDataFrameContainer< SKIROC2DataFrame > SKIROC2DigiCollection