Skip to content

Commit 1af9293

Browse files
mhemmer-cernsawenzel
authored andcommitted
[ATO-581] Speed up of createDigitsFromSim()
- Merged two for loops two increase performance - `localPadRow` in createCluster is now a const unsigned int as it should be when comparing with `Mapper::ROWSPERREGION[region]` - remove variables that are only used once directly afterwards - move constructor of digitContainer outside of loop and add reset call inside loop. The reset call is very expansice but meassures with TStopwatch show that it is still faster.
1 parent 5498912 commit 1af9293

File tree

1 file changed

+29
-44
lines changed

1 file changed

+29
-44
lines changed

Detectors/TPC/simulation/macro/toyCluster.C

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "TPCBase/Mapper.h"
4545
#include "TPCBase/ParameterDetector.h"
4646
#include "TPCBase/ParameterElectronics.h"
47+
#include "TPCBase/CDBInterface.h"
4748
#include "TPCSimulation/ElectronTransport.h"
4849
#include "TPCSimulation/SAMPAProcessing.h"
4950
#include "TPCSimulation/Point.h"
@@ -125,7 +126,7 @@ void simulateTracks(const int maxEvents = 1, const char* outputFolder = "./", co
125126
/// fill the hitGroupSector object which will be written to the tree (create the track)
126127
/// \param theta theta of the track
127128
/// \param phi phi of the track
128-
/// \parma dedx dedx of the track
129+
/// \param dedx dedx of the track
129130
/// \param hitGroupSector output vector containing the hits of the track
130131
void fillTPCHits(const float theta, const float phi, const float dedx, std::vector<HitGroup>& hitGroupSector, std::pair<GlobalPosition3D, GlobalPosition3D>& trackInfo)
131132
{
@@ -352,37 +353,34 @@ void createDigitsFromSim(const char* inpFileSim = "o2sim_HitsTPC.root", const st
352353
TBranch* brlabel = tree.Branch(fmt::format("TPCDigitMCTruth_{}", mSector).data(), &labels);
353354
TBranch* brmCommon = tree.Branch(fmt::format("TPCCommonMode_{}", mSector).data(), &commonMode);
354355

356+
DigitContainer digitContainer;
357+
const auto firstTimeBinForCurrentEvent = sampaProcessing.getTimeBinFromTime(0.f);
355358
// loop over hits
356359
const int nEvents = hitTree->GetEntries(); // number of simulated events per hit file
357360
for (int iev = 0; iev < nEvents; ++iev) {
358-
const double eventTime = 0;
359361
if (iev % 50 == false) {
360362
LOG(info) << "event: " << iev + 1 << " from " << nEvents << " events";
361363
}
362364
hitTree->GetEntry(iev);
363365

364-
DigitContainer digitContainer;
365-
const auto firstTimeBinForCurrentEvent = sampaProcessing.getTimeBinFromTime(eventTime);
366+
digitContainer.reset();
366367
digitContainer.setStartTime(firstTimeBinForCurrentEvent);
367368
digitContainer.reserve(firstTimeBinForCurrentEvent);
368369

369370
const float maxEleTime = (int(digitContainer.size()) - nShapedPoints) * zbinWidth;
370-
auto vecTracks = arrSectors;
371-
for (auto& hitGroup : *vecTracks) {
371+
for (auto& hitGroup : *arrSectors) {
372372
const int MCTrackID = hitGroup.GetTrackID();
373373
for (size_t ihit = 0; ihit < hitGroup.getSize(); ++ihit) {
374374
const auto& eh = hitGroup.getHit(ihit);
375375
GlobalPosition3D posEle(eh.GetX(), eh.GetY(), eh.GetZ());
376376

377377
const int nPrimaryElectrons = static_cast<int>(eh.GetEnergyLoss());
378-
const float hitTime = eh.GetTime() * 0.001f;
379378
if (nPrimaryElectrons <= 0) {
380379
continue;
381380
}
381+
const float hitTime = eh.GetTime() * 0.001f;
382382

383383
for (int iele = 0; iele < nPrimaryElectrons; iele++) {
384-
const GlobalPosition3D posEleDiff = getElectronDrift(posEle);
385-
386384
// add secondaries
387385
int nSecondaries = (maxSecondaries == 0) ? 0 : getNsec();
388386

@@ -394,24 +392,20 @@ void createDigitsFromSim(const char* inpFileSim = "o2sim_HitsTPC.root", const st
394392
// storage for all electrons (primary + secondaries)
395393
std::vector<GlobalPosition3D> posTotElectrons;
396394
posTotElectrons.reserve(nSecondaries + 1);
395+
const GlobalPosition3D posEleDiff = getElectronDrift(posEle);
397396
posTotElectrons.emplace_back(posEleDiff);
398397

399398
// apply some diffusion to secondaries
400399
for (int isec = 0; isec < nSecondaries; ++isec) {
401400
// electrons are created randomly
402-
const double x = gRandom->Gaus(0, std::abs(posEle.X() - posEleDiff.X())) + posEle.X();
403-
const double y = gRandom->Gaus(0, std::abs(posEle.Y() - posEleDiff.Y())) + posEle.Y();
404-
const double z = gRandom->Gaus(0, std::abs(posEle.Z() - posEleDiff.Z())) + posEle.Z();
405-
posTotElectrons.emplace_back(GlobalPosition3D(x, y, z));
406-
}
407-
408-
// loop over all electrons
409-
for (unsigned int j = 0; j < posTotElectrons.size(); ++j) {
410-
auto posEleTmp = posTotElectrons[j];
411-
const float driftTime = (detParam.TPClength - posEleTmp.Z()) / gasParam.DriftV;
412-
413-
const float eleTime = driftTime + hitTime; /// in us
414-
if (eleTime > maxEleTime) {
401+
posTotElectrons.emplace_back(GlobalPosition3D(
402+
gRandom->Gaus(0, std::abs(posEle.X() - posEleDiff.X())) + posEle.X(),
403+
gRandom->Gaus(0, std::abs(posEle.Y() - posEleDiff.Y())) + posEle.Y(),
404+
gRandom->Gaus(0, std::abs(posEle.Z() - posEleDiff.Z())) + posEle.Z()));
405+
// auto posEleTmp = posTotElectrons[j];
406+
const float driftTime = (detParam.TPClength - posTotElectrons.at(isec).Z()) / gasParam.DriftV;
407+
408+
if ((driftTime + hitTime) > maxEleTime) {
415409
LOG(warning) << "Skipping electron with driftTime " << driftTime << " from hit at time " << hitTime;
416410
continue;
417411
}
@@ -422,13 +416,13 @@ void createDigitsFromSim(const char* inpFileSim = "o2sim_HitsTPC.root", const st
422416
}
423417

424418
// Remove electrons that end up outside the active volume
425-
if (std::abs(posEleTmp.Z()) > detParam.TPClength) {
419+
if (std::abs(posTotElectrons.at(isec).Z()) > detParam.TPClength) {
426420
continue;
427421
}
428422

429423
// When the electron is not in the mSector we're processing, abandon
430424
// create dummy pos at A-Side
431-
auto posEleTmpTmp = posEleTmp;
425+
auto posEleTmpTmp = posTotElectrons.at(isec);
432426
posEleTmpTmp.SetZ(1);
433427
if (mapper.isOutOfSector(posEleTmpTmp, mSector)) {
434428
continue;
@@ -440,35 +434,28 @@ void createDigitsFromSim(const char* inpFileSim = "o2sim_HitsTPC.root", const st
440434
}
441435

442436
const CRU cru = digiPadPos.getCRU();
443-
int sectorTmp = cru.sector();
444-
if (sectorTmp != mSector) {
437+
if (static_cast<int>(cru.sector()) != mSector) {
445438
continue;
446439
}
447440

448-
// fixed GEM amplification for gaussian response (could be changed)
449-
const float nElectronsGEM = (nEleGEM == -1) ? static_cast<int>(gemAmplification.getEffectiveStackAmplification()) : nEleGEM;
450-
451441
// convert electrons to ADC signal
452442
const GlobalPadNumber globalPad = mapper.globalPadNumber(digiPadPos.getGlobalPadPos());
453-
const float adcsignal = sampaProcessing.getADCvalue(static_cast<float>(nElectronsGEM));
443+
const float adcsignal = sampaProcessing.getADCvalue(static_cast<float>((nEleGEM == -1) ? static_cast<int>(gemAmplification.getEffectiveStackAmplification()) : nEleGEM));
454444
sampaProcessing.getShapedSignal(adcsignal, driftTime, signalArray);
455445

456446
// set up MC label
457-
const int eventID = iev;
458-
const int sourceID = 0; // TPC
459-
const o2::MCCompLabel label(MCTrackID, eventID, sourceID, false);
447+
const o2::MCCompLabel label(MCTrackID, iev, 0, false);
460448

461449
for (int i = 0; i < nShapedPoints; ++i) {
462-
const float timebin = driftTime / eleParam.ZbinWidth + i;
463-
digitContainer.addDigit(label, cru, timebin, globalPad, signalArray[i]);
450+
digitContainer.addDigit(label, cru, driftTime / eleParam.ZbinWidth + i, globalPad, signalArray[i]);
464451
}
465-
}
466-
} // electron loop
467-
} // hit loop
468-
} // track loop
452+
} // secondary loop
453+
} // electron loop
454+
} // hit loop
455+
} // track loop
469456

470457
// dump digits for each event to a file
471-
dumpDigits(digitContainer, eventTime, digits, labels, commonMode, brdigits, brlabel, brmCommon);
458+
dumpDigits(digitContainer, 0.l, digits, labels, commonMode, brdigits, brlabel, brmCommon);
472459
} // event loop
473460

474461
fOut.cd();
@@ -489,10 +476,8 @@ void dumpDigits(DigitContainer& digitContainer, const float eventtime, std::vect
489476
sampaProcessing.updateParameters();
490477

491478
Sector sec(mSector);
492-
const bool isContinuous = false;
493-
const bool finalFlush = false;
494479
o2::dataformats::MCTruthContainer<o2::MCCompLabel> labelsTmp;
495-
digitContainer.fillOutputContainer(digits, labelsTmp, commonMode, sec, sampaProcessing.getTimeBinFromTime(eventtime), isContinuous, finalFlush);
480+
digitContainer.fillOutputContainer(digits, labelsTmp, commonMode, sec, sampaProcessing.getTimeBinFromTime(eventtime), false, false);
496481

497482
// flatten labels
498483
std::vector<char> buffer;
@@ -659,7 +644,7 @@ void createCluster(const char* inpHits = "o2sim_HitsTPC.root", const char* outFi
659644
// check for mSector edge pad
660645
const int off = 2;
661646
const int offPad = 2;
662-
const int localPadRow = Mapper::getLocalRowFromGlobalRow(padrow);
647+
const unsigned int localPadRow = Mapper::getLocalRowFromGlobalRow(padrow);
663648
bool isEdge = false;
664649
if (pad < offPad || pad >= (Mapper::PADSPERROW[region][localPadRow] - offPad - 1) || localPadRow < off || localPadRow >= Mapper::ROWSPERREGION[region] - off) {
665650
isEdge = true;

0 commit comments

Comments
 (0)