Skip to content

Commit e4204ff

Browse files
committed
Speedup event generation by caching TF1
Improves speed of event generation by (a) caching TF1 objects (b) avoiding call to TF1::SetParameter (because this invalidates the cache for TF1::GetRandom)) + minor change of pow(x, 0.333) --> std::cbrt(x)
1 parent b0a3f02 commit e4204ff

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

GeneratorParam/GeneratorParam.cxx

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ void GeneratorParam::Init() {
149149
if (fdNdPhi)
150150
fdNdPhi->Delete();
151151
fdNdPhi = new TF1(name, "1+2*[0]*TMath::Cos(2*(x-[1]))", fPhiMin, fPhiMax);
152+
152153
//
153154
//
154155
snprintf(name, 256, "pt-for-%s", GetName());
@@ -238,8 +239,9 @@ void GeneratorParam::GenerateEvent() {
238239
Int_t iTemp = pdg;
239240

240241
// custom pdg codes to destinguish direct photons
241-
if ((pdg >= 220000) && (pdg <= 220001))
242+
if ((pdg >= 220000) && (pdg <= 220001)) {
242243
pdg = 22;
244+
}
243245
fChildWeight=(fDecayer->GetPartialBranchingRatio(pdg))*fParentWeight;
244246
TParticlePDG *particle = pDataBase->GetParticle(pdg);
245247
Float_t am = particle->Mass();
@@ -248,12 +250,21 @@ void GeneratorParam::GenerateEvent() {
248250
// --- For Exodus -------------------------------
249251
Double_t awidth = particle->Width();
250252
if (awidth > 0) {
251-
TF1 rbw("rbw",
252-
"pow([1],2)*pow([0],2)/(pow(x*x-[0]*[0],2)+pow(x*x*[1]/[0],2))",
253-
am - 5 * awidth, am + 5 * awidth);
254-
rbw.SetParameter(0, am);
255-
rbw.SetParameter(1, awidth);
256-
am = rbw.GetRandom();
253+
TF1* rbw = nullptr;
254+
auto iter = fPDGtoTF1.find(pdg);
255+
if (iter != fPDGtoTF1.end()) {
256+
// see if we have cached TF1 for this pdg
257+
rbw = iter->second.get();
258+
}
259+
else {
260+
// otherwise create it
261+
fPDGtoTF1[pdg] = std::make_unique<TF1>("rbw",
262+
"pow([1],2)*pow([0],2)/(pow(x*x-[0]*[0],2)+pow(x*x*[1]/[0],2))", am - 5 * awidth, am + 5 * awidth);
263+
fPDGtoTF1[pdg]->SetParameter(0, am);
264+
fPDGtoTF1[pdg]->SetParameter(1, awidth);
265+
rbw = fPDGtoTF1[pdg].get();
266+
}
267+
am = rbw->GetRandom();
257268
}
258269
// -----------------------------------------------//
259270

@@ -280,9 +291,17 @@ void GeneratorParam::GenerateEvent() {
280291
}
281292
//
282293
// phi
294+
295+
// set the right parameters for fdNdPhi
283296
double v2 = fV2Para->Eval(pt);
284-
fdNdPhi->SetParameter(0, v2);
285-
fdNdPhi->SetParameter(1, fEvPlane);
297+
if (fdNdPhi->GetParameter(0) != v2) {
298+
// we should avoid calling SetParam as this invalidates the
299+
// internal integral cache of TF1
300+
fdNdPhi->SetParameter(0, v2);
301+
}
302+
if (fdNdPhi->GetParameter(1) != fEvPlane) {
303+
fdNdPhi->SetParameter(1, fEvPlane);
304+
}
286305
phi = fdNdPhi->GetRandom();
287306
pl = xmt * ty / sqrt((1. - ty) * (1. + ty));
288307
theta = TMath::ATan2(pt, pl);
@@ -816,7 +835,7 @@ double GeneratorParam::RandomEnergyFraction(double Z, double photonEnergy) {
816835
fZ += 8 * fcZ;
817836

818837
// Limits of the screening variable
819-
double screenFactor = 136. * epsilon0Local / pow(Z, 1 / 3);
838+
double screenFactor = 136. * epsilon0Local / std::cbrt(Z);
820839
double screenMax = exp((42.24 - fZ) / 8.368) - 0.952;
821840
double screenMin = std::min(4. * screenFactor, screenMax);
822841

@@ -836,7 +855,7 @@ double GeneratorParam::RandomEnergyFraction(double Z, double photonEnergy) {
836855

837856
do {
838857
if (normF1 / (normF1 + normF2) > gRandom->Rndm()) {
839-
epsilon = 0.5 - epsilonRange * pow(gRandom->Rndm(), 0.333333);
858+
epsilon = 0.5 - epsilonRange * std::cbrt(gRandom->Rndm());
840859
screen = screenFactor / (epsilon * (1. - epsilon));
841860
gReject = (ScreenFunction1(screen) - fZ) / f10;
842861
} else {

GeneratorParam/GeneratorParam.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <TMath.h>
2424
#include <TVector3.h>
2525
#include <TVirtualMCDecayer.h>
26+
#include <map>
2627
class TF1;
2728
typedef enum { kNoSmear, kPerEvent, kPerTrack } VertexSmear_t;
2829
typedef enum { kAnalog, kNonAnalog } Weighting_t;
@@ -215,6 +216,8 @@ class GeneratorParam : public TGenerator {
215216
kEtaRange = BIT(20)
216217
};
217218

219+
std::map<int, std::unique_ptr<TF1>> fPDGtoTF1; //! map of cache TF1 objects for "exodus"
220+
218221
private:
219222
void InitChildSelect();
220223
GeneratorParam(const GeneratorParam &Param);

0 commit comments

Comments
 (0)