11 #include "CLHEP/Random/RandomEngine.h"
12 #include "CLHEP/Random/RandGaussZiggurat.h"
14 #include "Identifier/Identifier.h"
50 return StatusCode::SUCCESS;
60 ATH_MSG_ERROR(
"Could not get background CscRawDataContainer called " << bkgContainer.
name() <<
" from store " << bkgContainer.
store());
61 return StatusCode::FAILURE;
63 ATH_MSG_DEBUG(
"Found background CscRawDataContainer called " << bkgContainer.
name() <<
" in store " << bkgContainer.
store());
66 if(!signalContainer.
isValid()) {
67 ATH_MSG_ERROR(
"Could not get signal CscRawOverlayContainer called " << signalContainer.
name() <<
" from store " << signalContainer.
store());
68 return StatusCode::FAILURE;
70 ATH_MSG_DEBUG(
"Found signal CscRawOverlayContainer called " << signalContainer.
name() <<
" in store " << signalContainer.
store());
73 ATH_CHECK(outputContainer.
record(std::make_unique<CscRawDataContainer>(bkgContainer->
size())));
74 if (!outputContainer.
isValid()) {
75 ATH_MSG_ERROR(
"Could not record output CscRawOverlayContainer called " << outputContainer.
name() <<
" to store " << outputContainer.
store());
76 return StatusCode::FAILURE;
78 ATH_MSG_DEBUG(
"Recorded output CscRawOverlayContainer called " << outputContainer.
name() <<
" in store " << outputContainer.
store());
85 return StatusCode::SUCCESS;
98 std::vector < std::pair<IdentifierHash, bool> > overlapMap;
101 overlapMap.emplace_back(hashId,
false);
106 auto search = std::lower_bound( overlapMap.begin(), overlapMap.end(), hashId,
107 [](
const std::pair<IdentifierHash, bool> &lhs,
IdentifierHash rhs) ->
bool { return lhs.first < rhs; } );
108 if (
search == overlapMap.end() ||
search->first != hashId) {
112 if (outputContainer->
addCollection(bkgCollection.get(), hashId).isFailure()) {
113 ATH_MSG_ERROR(
"Adding background Collection with hashId " << hashId <<
" failed");
114 return StatusCode::FAILURE;
116 (void)bkgCollection.release();
126 rngWrapper->
setSeed(
name(), Gaudi::Hive::currentContext() );
127 CLHEP::HepRandomEngine *rndmEngine(*rngWrapper);
131 for (
const auto &[hashId, overlap] : overlapMap) {
161 std::vector<uint16_t> samples;
168 uint32_t stripHash = hashOffset + j;
171 std::vector<uint16_t> stripSamples;
172 bool extractSamplesStatus =
data->samples(j, numSamples, stripSamples);
173 if (!extractSamplesStatus) {
175 <<
" Online Cluster width = " <<
width
176 <<
" for number of Samples = " << numSamples
177 <<
" continuing ...");
180 double sampleNoise = CLHEP::RandGaussZiggurat::shoot(rndmEngine, 0.0, stripNoise);
181 float adcCount =
sample + sampleNoise;
183 ATH_MSG_DEBUG(
"value out of range (copying over signal): " << adcCount <<
" "
184 <<
" Setting it to max value = " <<
MAX_AMPL
185 <<
" IdentifierHash is " << stripHash);
188 samples.push_back( (
uint16_t) std::rint(adcCount) );
194 auto rdo = std::make_unique<CscRawData>(samples,
data->address(),
data->identify(),
data->time(),
data->rpuID(),
data->width(),
data->hashId());
213 ATH_MSG_ERROR(
"Adding overlaid Collection with hashId " << hashId <<
" failed");
214 return StatusCode::FAILURE;
221 return StatusCode::SUCCESS;
225 std::unique_ptr<CscRawDataCollection>
227 bool propertiesOnly)
const
245 if (propertiesOnly) {
249 for (
const CscRawData *existingDatum : *collection) {
251 auto *datumCopy =
new CscRawData(*existingDatum);
260 data.clear();
if ( !coll )
return;
263 for ( ; idata != edata; ++idata ) {
264 if ( (*idata)->rpuID() == spuID )
data.push_back( *idata );
266 ATH_MSG_DEBUG(
"spuData(): made data vector of size "<<
data.size()<<
" for SPU "<<spuID);
271 int measuresPhi = ( (
address & 0x00000100) >> 8);
272 if (
address<2147483640 && measuresPhi) {
284 CLHEP::HepRandomEngine *rndmEngine)
const
289 unsigned int nSigSamples = bkgCollection->
numSamples();
290 unsigned int nOvlSamples = signalCollection->
numSamples();
293 unsigned int dataSamplingTime = bkgCollection->
rate();
294 unsigned int ovlSamplingTime = signalCollection->
rate();
296 if ( dataSamplingTime != ovlSamplingTime ) {
297 ATH_MSG_ERROR(
"Overlay of inconsistent data - sampling times not the same "
298 << dataSamplingTime <<
" ns " << ovlSamplingTime <<
" ns");
299 throw std::runtime_error(
"mergeCollections(): sampling time mismatch");
302 if ( nSigSamples != nOvlSamples ) {
303 ATH_MSG_ERROR(
"Overlay of inconsistent data - number of samples not the same "
304 << nSigSamples <<
" " << nOvlSamples);
305 throw std::runtime_error(
"mergeCollections(): number of samples mismatch");
310 uint16_t clusterCounts[] = {0,0,0,0,0,0,0,0,0,0};
312 for (
uint16_t spuID=0; spuID<10; ++spuID) {
314 std::vector<const CscRawData*> sigData;
315 this->
spuData(bkgCollection, spuID, sigData);
317 std::vector<const CscRawData*> ovlData;
318 this->
spuData(signalCollection, spuID, ovlData);
323 if ( spuID == 4 || spuID == 9 )
layer=4;
324 for (
int j=0; j<=
layer; ++j ) {
325 std::map< int,std::vector<uint16_t> > sigSamples;
326 std::map< int,std::vector<uint16_t> > ovlSamples;
330 uint32_t ovlAddress = this->
stripData( ovlData, nOvlSamples, ovlSamples, ovlHash, spuID, j ,
false);
331 if (sigSamples.size()==0 && ovlSamples.size()==0)
continue;
336 ATH_MSG_DEBUG(
"Looking for overlap of hashes and addresses within widths because needtoflip");
338 std::set<int>
sig;
int lastindex=-1;
339 for (std::map<
int,std::vector<uint16_t> >::const_iterator si=sigSamples.begin(); si!=sigSamples.end(); ++si) {
340 if (si!=sigSamples.begin() && si->first-lastindex!=1)
break;
342 sig.insert(si->first);
msg() << si->first <<
" ";
347 for (std::map<
int,std::vector<uint16_t> >::const_iterator so=ovlSamples.begin(); so!=ovlSamples.end(); ++so) {
349 msg() << (so->first)-1 <<
" ";
350 if (
sig.find((so->first)-1)!=
sig.end()) {overlap=
true;
msg() <<
"!!";}
351 msg() << (so->first) <<
" ";
352 if (
sig.find((so->first))!=
sig.end()) {overlap=
true;
msg() <<
"!!";}
353 msg() << (so->first)+1 <<
" ";
354 if (
sig.find((so->first)+1)!=
sig.end()) {overlap=
true;
msg() <<
"!!";}
358 ATH_MSG_DEBUG(
"Taking max of hashes and addresses because needtoflip and no overlap");
365 std::set<int> insertedstrips, readstrips;
366 for (std::map<
int,std::vector<uint16_t> >::const_iterator
s=sigSamples.begin();
s!=sigSamples.end(); ++
s){readstrips.insert(
s->first);}
367 for (std::map<
int,std::vector<uint16_t> >::const_iterator si=ovlSamples.begin(); si!=ovlSamples.end(); ++si){readstrips.insert(si->first);}
371 for (
unsigned int di=0; di<datums.size(); ++di){
375 int stripstart = (
address & 0x000000FF) + 1 + 0;
376 ATH_MSG_DEBUG(
"Datum in layer="<<j<<
" has hash="<<
hash<<
" address="<<
address<<
" stripstart="<<stripstart<<
", "<< *datum );
377 if (datum->
width()==0) {
389 for (
unsigned int j=0; j<datum->
width(); ++j) {
390 int chamberLayer = ( (
address & 0x00000800) >> 11) + 1;
391 int wireLayer = ( (
address & 0x00000600) >> 9) + 1;
392 int measuresPhi = ( (
address & 0x00000100) >> 8);
394 ATH_MSG_VERBOSE(
"chamberlayer,wirelayer,measuresphi,strip="<<chamberLayer<<
","<<wireLayer<<
","<<measuresPhi<<
","<<
strip);
403 insertedstrips.insert(
strip);
417 if (spuID <10) clusterCounts[spuID] += 1;
418 if ( spuID <= 4 ) rpuCount[0] = 5;
419 else if ( spuID > 4 && spuID <= 9 ) rpuCount[1] = 11;
423 if (readstrips!=insertedstrips){
425 std::ostringstream readstream;
426 for (std::set<int>::const_iterator
i = readstrips.begin();
i!=readstrips.end(); ++
i){readstream<<*
i<<
" ";}
428 std::ostringstream insertstream;
429 for (std::set<int>::const_iterator
i = insertedstrips.begin();
i!=insertedstrips.end(); ++
i){insertstream<<*
i<<
" ";}
442 const unsigned int numSamples,
443 std::map<
int,std::vector<uint16_t> >& samples,
446 const int gasLayer,
bool isdata)
const
448 ATH_MSG_DEBUG(
"stripData<>() begin: gasLayer="<<gasLayer<<
" spuID="<<spuID<<
" isdata="<<isdata);
458 std::vector<const CscRawData*>::const_iterator idata =
data.begin();
459 std::vector<const CscRawData*>::const_iterator edata =
data.end();
460 for ( ; idata != edata; ++idata ) {
466 m_idHelperSvc->cscIdHelper().get_id(hashOffset, stripId, &context);
467 unsigned int strip =
static_cast<unsigned int> (
m_idHelperSvc->cscIdHelper().strip( stripId ) );
474 bool non_precision = (gasLayer==
layer) && (spuID==4 || spuID==9);
475 bool precision = (gasLayer==0) && (!(spuID==4 || spuID==9));
476 bool check = precision || non_precision;
483 unsigned int newaddress = datum->
address();
486 ATH_MSG_VERBOSE(
"needtoflip in stripdata, newaddress was = "<<newaddress<<
", strip was = "<<
strip);
494 newaddress=newaddress - oldFirstStrip + newFirstStrip;
500 ATH_MSG_VERBOSE(
"needtoflip in stripdata, newaddress now = "<<newaddress<<
", strip now = "<<
strip);
506 if ( hashOffset >
hash )
hash = hashOffset;
510 if ( hashOffset <
hash )
hash = hashOffset;
516 for (
unsigned int j=0; j<
width; ++j) {
517 std::vector<uint16_t> adcs;
518 bool extractSamples = datum->
samples(j, numSamples, adcs);
519 if ( !extractSamples ) {
521 <<
" Online Cluster width = " <<
width <<
" for number of Samples = " << numSamples);
524 int newstrip = (
strip+j);
529 samples.insert ( std::make_pair( newstrip, adcs) );
539 const std::map<
int,std::vector<uint16_t> >& ovlSamples,
544 CLHEP::HepRandomEngine *rndmEngine)
const
547 std::vector<CscRawData*> datas;
550 if ( spuID == 4 || spuID == 9 )
max = 48;
551 std::vector<uint16_t> samples;
552 std::map< int,std::vector<uint16_t> >::const_iterator
sig;
553 std::map< int,std::vector<uint16_t> >::const_iterator ovl;
558 for (
int i=1;
i<=
max; ++
i) {
559 sig = sigSamples.find(
i);
560 ovl = ovlSamples.find(
i);
563 if (
sig != sigSamples.end() && ovl == ovlSamples.end() ) {
565 for (
unsigned int j=0; j<(*sig).second.size(); ++j ) {
566 samples.push_back( (*sig).second.at(j) );
567 assert((*sig).second.at(j)<=
MAX_AMPL);
571 else if (
sig == sigSamples.end() && ovl != ovlSamples.end() ) {
575 for (
unsigned int j=0; j<(*ovl).second.size(); ++j ) {
576 double theNoise = CLHEP::RandGaussZiggurat::shoot(rndmEngine, 0.0,
noise);
577 float adcCount = (*ovl).second.at(j) + theNoise ;
579 ATH_MSG_DEBUG(
"value out of range (adding noise): " << adcCount <<
" "
580 <<
" Setting it to max value = " <<
MAX_AMPL
581 <<
" IdentifierHash is " << (myhashw));
584 samples.push_back( (
uint16_t) rint(adcCount) );
588 else if (
sig != sigSamples.end() && ovl != ovlSamples.end() ) {
591 double pedestal =
m_cscCalibTool->stripPedestal( (myhashw),
false );
592 for (
unsigned int j=0; j<(*sig).second.size(); ++j ) {
593 float adcCount = (*sig).second.at(j) + (*ovl).second.at(j) - pedestal ;
595 ATH_MSG_DEBUG(
"value out of range (adding data+MC samples - pedestal): " << adcCount <<
" "
596 <<
" Setting it to max value = " <<
MAX_AMPL
597 <<
" IdentifierHash is " << (myhashw));
600 samples.push_back( (
uint16_t) rint(adcCount) );
605 if (
used==
false && datas.size()>0 ){
606 if (
needtoflip(myaddress)) {myhash-=1; myaddress-=1;}
607 else {myhash+=1; myaddress+=1;}
612 if ( (
used==
false||
i==
max) && samples.size()>0){
617 datas.push_back(rawData);
618 ATH_MSG_DEBUG(
"overlay<>() add datum: hash="<<myhash<<
" address="<<myaddress<<
" width="<<
width);
625 ATH_MSG_DEBUG(
"overlay<>() end: CscRawDatas size="<<datas.size());