10 #include "CLHEP/Random/RandomEngine.h"
11 #include "CLHEP/Random/RandGaussZiggurat.h"
40 return StatusCode::SUCCESS;
50 ATH_MSG_ERROR(
"Could not get background CscRawDataContainer called " << bkgContainer.
name() <<
" from store " << bkgContainer.
store());
51 return StatusCode::FAILURE;
53 ATH_MSG_DEBUG(
"Found background CscRawDataContainer called " << bkgContainer.
name() <<
" in store " << bkgContainer.
store());
56 if(!signalContainer.
isValid()) {
57 ATH_MSG_ERROR(
"Could not get signal CscRawOverlayContainer called " << signalContainer.
name() <<
" from store " << signalContainer.
store());
58 return StatusCode::FAILURE;
60 ATH_MSG_DEBUG(
"Found signal CscRawOverlayContainer called " << signalContainer.
name() <<
" in store " << signalContainer.
store());
63 ATH_CHECK(outputContainer.
record(std::make_unique<CscRawDataContainer>(bkgContainer->
size())));
64 if (!outputContainer.
isValid()) {
65 ATH_MSG_ERROR(
"Could not record output CscRawOverlayContainer called " << outputContainer.
name() <<
" to store " << outputContainer.
store());
66 return StatusCode::FAILURE;
68 ATH_MSG_DEBUG(
"Recorded output CscRawOverlayContainer called " << outputContainer.
name() <<
" in store " << outputContainer.
store());
75 return StatusCode::SUCCESS;
88 std::vector < std::pair<IdentifierHash, bool> > overlapMap;
91 overlapMap.emplace_back(hashId,
false);
96 auto search = std::lower_bound( overlapMap.begin(), overlapMap.end(), hashId,
97 [](
const std::pair<IdentifierHash, bool> &lhs,
IdentifierHash rhs) ->
bool { return lhs.first < rhs; } );
98 if (
search == overlapMap.end() ||
search->first != hashId) {
102 if (outputContainer->
addCollection(bkgCollection.get(), hashId).isFailure()) {
103 ATH_MSG_ERROR(
"Adding background Collection with hashId " << hashId <<
" failed");
104 return StatusCode::FAILURE;
106 (void)bkgCollection.release();
116 rngWrapper->
setSeed(
name(), Gaudi::Hive::currentContext() );
117 CLHEP::HepRandomEngine *rndmEngine(*rngWrapper);
121 for (
const auto &[hashId, overlap] : overlapMap) {
151 std::vector<uint16_t> samples;
158 uint32_t stripHash = hashOffset + j;
161 std::vector<uint16_t> stripSamples;
162 bool extractSamplesStatus =
data->samples(j, numSamples, stripSamples);
163 if (!extractSamplesStatus) {
165 <<
" Online Cluster width = " <<
width
166 <<
" for number of Samples = " << numSamples
167 <<
" continuing ...");
170 double sampleNoise = CLHEP::RandGaussZiggurat::shoot(rndmEngine, 0.0, stripNoise);
173 float adcCount =
sample + sampleNoise;
175 ATH_MSG_DEBUG(
"value out of range (copying over signal): " << adcCount <<
" "
176 <<
" Setting it to max value = " <<
MAX_AMPL
177 <<
" IdentifierHash is " << stripHash);
182 samples.push_back( (
uint16_t) rint(adcCount) );
188 auto rdo = std::make_unique<CscRawData>(samples,
data->address(),
data->identify(),
data->time(),
data->rpuID(),
data->width(),
data->hashId());
207 ATH_MSG_ERROR(
"Adding overlaid Collection with hashId " << hashId <<
" failed");
208 return StatusCode::FAILURE;
215 return StatusCode::SUCCESS;
219 std::unique_ptr<CscRawDataCollection>
221 bool propertiesOnly)
const
239 if (propertiesOnly) {
243 for (
const CscRawData *existingDatum : *collection) {
245 auto *datumCopy =
new CscRawData(*existingDatum);
254 data.clear();
if ( !coll )
return;
257 for ( ; idata != edata; ++idata ) {
258 if ( (*idata)->rpuID() == spuID )
data.push_back( *idata );
260 ATH_MSG_DEBUG(
"spuData(): made data vector of size "<<
data.size()<<
" for SPU "<<spuID);
265 int measuresPhi = ( (
address & 0x00000100) >> 8);
266 if (
address<2147483640 && measuresPhi) {
278 CLHEP::HepRandomEngine *rndmEngine)
const
283 unsigned int nSigSamples = bkgCollection->
numSamples();
284 unsigned int nOvlSamples = signalCollection->
numSamples();
287 unsigned int dataSamplingTime = bkgCollection->
rate();
288 unsigned int ovlSamplingTime = signalCollection->
rate();
290 if ( dataSamplingTime != ovlSamplingTime ) {
291 ATH_MSG_ERROR(
"Overlay of inconsistent data - sampling times not the same "
292 << dataSamplingTime <<
" ns " << ovlSamplingTime <<
" ns");
293 throw std::runtime_error(
"mergeCollections(): sampling time mismatch");
296 if ( nSigSamples != nOvlSamples ) {
297 ATH_MSG_ERROR(
"Overlay of inconsistent data - number of samples not the same "
298 << nSigSamples <<
" " << nOvlSamples);
299 throw std::runtime_error(
"mergeCollections(): number of samples mismatch");
304 uint16_t clusterCounts[] = {0,0,0,0,0,0,0,0,0,0};
306 for (
uint16_t spuID=0; spuID<10; ++spuID) {
308 std::vector<const CscRawData*> sigData;
309 this->
spuData(bkgCollection, spuID, sigData);
311 std::vector<const CscRawData*> ovlData;
312 this->
spuData(signalCollection, spuID, ovlData);
317 if ( spuID == 4 || spuID == 9 )
layer=4;
318 for (
int j=0; j<=
layer; ++j ) {
319 std::map< int,std::vector<uint16_t> > sigSamples;
320 std::map< int,std::vector<uint16_t> > ovlSamples;
324 uint32_t ovlAddress = this->
stripData( ovlData, nOvlSamples, ovlSamples, ovlHash, spuID, j ,
false);
325 if (sigSamples.size()==0 && ovlSamples.size()==0)
continue;
330 ATH_MSG_DEBUG(
"Looking for overlap of hashes and addresses within widths because needtoflip");
332 std::set<int>
sig;
int lastindex=-1;
333 for (std::map<
int,std::vector<uint16_t> >::const_iterator si=sigSamples.begin(); si!=sigSamples.end(); ++si) {
334 if (si!=sigSamples.begin() && si->first-lastindex!=1)
break;
336 sig.insert(si->first);
msg() << si->first <<
" ";
341 for (std::map<
int,std::vector<uint16_t> >::const_iterator so=ovlSamples.begin(); so!=ovlSamples.end(); ++so) {
343 msg() << (so->first)-1 <<
" ";
344 if (
sig.find((so->first)-1)!=
sig.end()) {overlap=
true;
msg() <<
"!!";}
345 msg() << (so->first) <<
" ";
346 if (
sig.find((so->first))!=
sig.end()) {overlap=
true;
msg() <<
"!!";}
347 msg() << (so->first)+1 <<
" ";
348 if (
sig.find((so->first)+1)!=
sig.end()) {overlap=
true;
msg() <<
"!!";}
352 ATH_MSG_DEBUG(
"Taking max of hashes and addresses because needtoflip and no overlap");
359 std::set<int> insertedstrips, readstrips;
360 for (std::map<
int,std::vector<uint16_t> >::const_iterator
s=sigSamples.begin();
s!=sigSamples.end(); ++
s){readstrips.insert(
s->first);}
361 for (std::map<
int,std::vector<uint16_t> >::const_iterator si=ovlSamples.begin(); si!=ovlSamples.end(); ++si){readstrips.insert(si->first);}
365 for (
unsigned int di=0; di<datums.size(); ++di){
369 int stripstart = (
address & 0x000000FF) + 1 + 0;
370 ATH_MSG_DEBUG(
"Datum in layer="<<j<<
" has hash="<<
hash<<
" address="<<
address<<
" stripstart="<<stripstart<<
", "<< *datum );
371 if (datum->
width()==0) {
383 for (
unsigned int j=0; j<datum->
width(); ++j) {
384 int chamberLayer = ( (
address & 0x00000800) >> 11) + 1;
385 int wireLayer = ( (
address & 0x00000600) >> 9) + 1;
386 int measuresPhi = ( (
address & 0x00000100) >> 8);
388 ATH_MSG_VERBOSE(
"chamberlayer,wirelayer,measuresphi,strip="<<chamberLayer<<
","<<wireLayer<<
","<<measuresPhi<<
","<<
strip);
397 insertedstrips.insert(
strip);
411 if (spuID <10) clusterCounts[spuID] += 1;
412 if ( spuID <= 4 ) rpuCount[0] = 5;
413 else if ( spuID > 4 && spuID <= 9 ) rpuCount[1] = 11;
417 if (readstrips!=insertedstrips){
419 std::ostringstream readstream;
420 for (std::set<int>::const_iterator
i = readstrips.begin();
i!=readstrips.end(); ++
i){readstream<<*
i<<
" ";}
422 std::ostringstream insertstream;
423 for (std::set<int>::const_iterator
i = insertedstrips.begin();
i!=insertedstrips.end(); ++
i){insertstream<<*
i<<
" ";}
436 const unsigned int numSamples,
437 std::map<
int,std::vector<uint16_t> >& samples,
440 const int gasLayer,
bool isdata)
const
442 ATH_MSG_DEBUG(
"stripData<>() begin: gasLayer="<<gasLayer<<
" spuID="<<spuID<<
" isdata="<<isdata);
452 std::vector<const CscRawData*>::const_iterator idata =
data.begin();
453 std::vector<const CscRawData*>::const_iterator edata =
data.end();
454 for ( ; idata != edata; ++idata ) {
460 m_idHelperSvc->cscIdHelper().get_id(hashOffset, stripId, &context);
461 unsigned int strip =
static_cast<unsigned int> (
m_idHelperSvc->cscIdHelper().strip( stripId ) );
468 bool non_precision = (gasLayer==
layer) && (spuID==4 || spuID==9);
469 bool precision = (gasLayer==0) && (!(spuID==4 || spuID==9));
470 bool check = precision || non_precision;
477 unsigned int newaddress = datum->
address();
480 ATH_MSG_VERBOSE(
"needtoflip in stripdata, newaddress was = "<<newaddress<<
", strip was = "<<
strip);
488 newaddress=newaddress - oldFirstStrip + newFirstStrip;
494 ATH_MSG_VERBOSE(
"needtoflip in stripdata, newaddress now = "<<newaddress<<
", strip now = "<<
strip);
500 if ( hashOffset >
hash )
hash = hashOffset;
504 if ( hashOffset <
hash )
hash = hashOffset;
510 for (
unsigned int j=0; j<
width; ++j) {
511 std::vector<uint16_t> adcs;
512 bool extractSamples = datum->
samples(j, numSamples, adcs);
513 if ( !extractSamples ) {
515 <<
" Online Cluster width = " <<
width <<
" for number of Samples = " << numSamples);
518 int newstrip = (
strip+j);
523 samples.insert ( std::make_pair( newstrip, adcs) );
533 const std::map<
int,std::vector<uint16_t> >& ovlSamples,
538 CLHEP::HepRandomEngine *rndmEngine)
const
541 std::vector<CscRawData*> datas;
544 if ( spuID == 4 || spuID == 9 )
max = 48;
545 std::vector<uint16_t> samples;
546 std::map< int,std::vector<uint16_t> >::const_iterator
sig;
547 std::map< int,std::vector<uint16_t> >::const_iterator ovl;
552 for (
int i=1;
i<=
max; ++
i) {
553 sig = sigSamples.find(
i);
554 ovl = ovlSamples.find(
i);
557 if (
sig != sigSamples.end() && ovl == ovlSamples.end() ) {
559 for (
unsigned int j=0; j<(*sig).second.size(); ++j ) {
560 samples.push_back( (*sig).second.at(j) );
561 assert((*sig).second.at(j)<=
MAX_AMPL);
565 else if (
sig == sigSamples.end() && ovl != ovlSamples.end() ) {
569 for (
unsigned int j=0; j<(*ovl).second.size(); ++j ) {
570 double theNoise = CLHEP::RandGaussZiggurat::shoot(rndmEngine, 0.0,
noise);
571 float adcCount = (*ovl).second.at(j) + theNoise ;
573 ATH_MSG_DEBUG(
"value out of range (adding noise): " << adcCount <<
" "
574 <<
" Setting it to max value = " <<
MAX_AMPL
575 <<
" IdentifierHash is " << (myhashw));
578 samples.push_back( (
uint16_t) rint(adcCount) );
582 else if (
sig != sigSamples.end() && ovl != ovlSamples.end() ) {
585 double pedestal =
m_cscCalibTool->stripPedestal( (myhashw),
false );
586 for (
unsigned int j=0; j<(*sig).second.size(); ++j ) {
587 float adcCount = (*sig).second.at(j) + (*ovl).second.at(j) - pedestal ;
589 ATH_MSG_DEBUG(
"value out of range (adding data+MC samples - pedestal): " << adcCount <<
" "
590 <<
" Setting it to max value = " <<
MAX_AMPL
591 <<
" IdentifierHash is " << (myhashw));
594 samples.push_back( (
uint16_t) rint(adcCount) );
599 if (
used==
false && datas.size()>0 ){
600 if (
needtoflip(myaddress)) {myhash-=1; myaddress-=1;}
601 else {myhash+=1; myaddress+=1;}
606 if ( (
used==
false||
i==
max) && samples.size()>0){
611 datas.push_back(rawData);
612 ATH_MSG_DEBUG(
"overlay<>() add datum: hash="<<myhash<<
" address="<<myaddress<<
" width="<<
width);
619 ATH_MSG_DEBUG(
"overlay<>() end: CscRawDatas size="<<datas.size());