Project
Loading...
Searching...
No Matches
InteractionSampler.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
13#include <fairlogger/Logger.h>
14
15using namespace o2::steer;
16
17//_________________________________________________
19{
20 // (re-)initialize sample and check parameters consistency
21
22 int nBCSet = mBCFilling.getNBunches();
23 if (!nBCSet) {
24 LOG(warning) << "No bunch filling provided, impose default one";
26 nBCSet = mBCFilling.getNBunches();
27 }
28
29 if (mMuBC < 0. && mIntRate < 0.) {
30 LOG(warning) << "No IR or muBC is provided, setting default IR";
32 }
33
34 if (mMuBC > 0.) {
36 LOG(info) << "Deducing IR=" << mIntRate << "Hz from " << nBCSet << " BCs at mu=" << mMuBC;
37 } else {
39 LOG(info) << "Deducing mu=" << mMuBC << " per BC from IR=" << mIntRate << " with " << nBCSet << " BCs";
40 }
41
42 mInteractingBCs.clear();
43 mInteractingBCs.reserve(nBCSet);
44 for (int i = 0; i < o2::constants::lhc::LHCMaxBunches; i++) {
45 if (mBCFilling.testBC(i)) {
46 mInteractingBCs.push_back(i);
47 }
48 }
49
50 auto mu = mMuBC;
51 // prob. of not having interaction in N consecutive BCs is P(N) = mu*exp(-(N-1)*mu), hence its cumulative distribution
52 // is T(N) = integral_1^N {P(N)} = 1. - exp(-(N-1)*mu)
53 // We generate random N using its inverse, N = 1 - log(1 - Rndm)/mu
54 mBCJumpGenerator.initialize([mu]() { return (1. - std::log(1. - gRandom->Rndm()) / mu); });
55
56 // Poisson distribution of number of collisions in the bunch excluding 0
58 int n = 0;
59 while ((n = gRandom->Poisson(mu)) == 0) {
60 ;
61 }
62 return n;
63 });
64
65 auto trms = mBCTimeRMS;
67 float t; // make sure it does not go outside half bunch
68 while (std::abs(t = gRandom->Gaus(0, trms)) > o2::constants::lhc::LHCBunchSpacingNS / 2.1) {
69 ;
70 }
71 return t;
72 });
73
74 mIntBCCache = 0;
75 mCurrBCIdx = 0;
76 mIR = mFirstIR;
78 mCurrBCIdx++;
79 }
80 // set the "current BC" right in front of the 1st BC to generate. There will be a jump by at least 1 during generation
81 mCurrBCIdx--;
82}
83
84//_________________________________________________
86{
87 if (mIntRate < 0) {
88 LOG(error) << "not yet initialized";
89 return;
90 }
91 LOG(info) << "InteractionSampler with " << mInteractingBCs.size() << " colliding BCs, mu(BC)= "
92 << getMuPerBC() << " -> total IR= " << getInteractionRate();
93 LOG(info) << "Current " << mIR << '(' << mIntBCCache << " coll left)";
94}
95
96//_________________________________________________
98{
99 // generate single interaction record
100 if (mIntRate < 0) {
101 init();
102 }
103
104 if (mIntBCCache < 1) { // do we still have interaction in current BC?
105 mIntBCCache = simulateInteractingBC(); // decide which BC interacts and N collisions
106 }
107 mIR.timeInBCNS = mTimeInBC.back();
108 mTimeInBC.pop_back();
109 mIntBCCache--;
110
111 return mIR;
112}
113
114//_________________________________________________
116{
117 // Returns number of collisions assigned to selected BC
118
120 // once BC is decided, enforce at least one interaction
121 int ncoll = mNCollBCGenerator.getNextValue();
122
123 // assign random time withing a bunch
124 for (int i = ncoll; i--;) {
126 }
127 if (ncoll > 1) { // sort in DECREASING time order (we are reading vector from the end)
128 std::sort(mTimeInBC.begin(), mTimeInBC.end(), [](const float a, const float b) { return a > b; });
129 }
130 return ncoll;
131}
132
133//_________________________________________________
134void InteractionSampler::setBunchFilling(const std::string& bcFillingFile)
135{
136 // load bunch filling from the file
137 auto* bc = o2::BunchFilling::loadFrom(bcFillingFile, "ccdb_object");
138 if (!bc) {
139 bc = o2::BunchFilling::loadFrom(bcFillingFile); // retry with default naming in case of failure
140 }
141 if (!bc) {
142 LOG(fatal) << "Failed to load bunch filling from " << bcFillingFile;
143 }
144 mBCFilling = *bc;
145 delete bc;
146}
uint64_t bc
Definition RawEventData.h:5
int32_t i
int getNBunches(int dir=-1) const
bool testBC(int bcID, int dir=-1) const
static BunchFilling * loadFrom(const std::string &fileName, const std::string &objName="")
void initialize(const RandomType randomType=RandomType::Gaus)
Definition RandomRing.h:145
o2::InteractionTimeRecord mFirstIR
static constexpr float DefIntRate
default interaction rate
std::vector< uint16_t > mInteractingBCs
o2::BunchFilling mBCFilling
patter of active BCs
o2::math_utils::RandomRing< 10000 > mBCJumpGenerator
float mBCTimeRMS
BC time spread in NANOSECONDS.
double mMuBC
interaction probability per BC
o2::math_utils::RandomRing< 1000 > mNCollBCGenerator
o2::math_utils::RandomRing< 1000 > mCollTimeGenerator
std::vector< float > mTimeInBC
interaction times within single BC
int mCurrBCIdx
counter for current interacting bunch
const o2::InteractionTimeRecord & generateCollisionTime()
o2::InteractionTimeRecord mIR
void setBunchFilling(const BunchFilling &bc)
float mIntRate
total interaction rate in Hz
int mIntBCCache
N interactions left for current BC.
GLdouble n
Definition glcorearb.h:1982
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
constexpr int LHCMaxBunches
constexpr double LHCBunchSpacingNS
constexpr double LHCRevFreq
uint16_t bc
bunch crossing ID of interaction
double timeInBCNS
time in NANOSECONDS relative to orbit/bc
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"