ATLAS Offline Software
LB_analyze.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include <map>
6 #include <TRandom3.h>
7 
8 
9 struct lbn_info
10 {
11  int nevt{};
12  float intlumi{};
13  int nevtwanted{};
14 };
15 
16 struct run_info
17 {
18  int nevt{};
19  float intlumi{};
20  std::map<int, lbn_info> lbnmap;
21  int nevtwanted{};
22 };
23 
24 
26  std::map<int, int> &eventcounts)
27 {
28  eventcounts.clear();
29  char buf[50];
30  sprintf(buf, "lbnevents_%d.txt", run);
31  //cppcheck-suppress invalidPrintfArgType_uint
32  printf("Opening %s, eventcounts size is %lu\n", buf, eventcounts.size());
33  FILE *fp = fopen(buf, "r");
34  if (!fp) {
35  fprintf(stderr, "Failed to open %s!!!\n", buf);
36  return 1;
37  }
38 
39  char *line = new char[500];
40  while (fgets(line, 480, fp)) {
41  int lbn, ne, nf, runn;
42  float mb;
43  int s = sscanf(&line[0], " ... Run %d, LB %d has %d events, %d RAW files and %f MB", &runn, &lbn, &ne, &nf, &mb);
44  if (s > 4) {
45  if (run != runn) {
46  delete[] line;
47  fclose(fp);
48  return 20;
49  }
50  printf("run %d lbn %d has %d events\n", run, lbn, ne);
51  eventcounts[lbn] = ne;
52  } else {
53  fprintf(stderr, "s=%d, bad read?\n", s);
54  delete[] line;
55  fclose(fp);
56  return 20;
57  }
58  }
59  delete[] line;
60  //cppcheck-suppress invalidPrintfArgType_uint
61  printf("Closing %s, eventcounts size is %lu\n", buf, eventcounts.size());
62  fclose(fp);
63 
64  return 0;
65 }
66 
68  int nwanted,
69  FILE *outfile)
70 {
71  FILE *fp = fopen("lbn", "r");
72  if (!fp) {
73  fprintf(stderr, "Failed to open lbn input file!!!\n");
74  return 1;
75  }
76 
77  char *line = new char[500];
78  int run, lbn, L1Acc, beforeps, afterps, valid;
79  int liveL1, livebp, liveap;
80  float instlumi, dt, avevtperbx, live, L1p, HLTp, LARp;
81  int grun{};
82  float gtotaltotallumi{}, gtotaltotallumiprescaled{};
83  float gtotallumi{}, gtotallumiprescaled{};
84 
85  TRandom myrand;
86  myrand.SetSeed(0);
87  printf("- myrand seed is %u\n", myrand.GetSeed());
88  myrand.SetSeed(myrand.Integer(10000000) + stream);
89  printf("- myrand seed is now %u\n", myrand.GetSeed());
90 
91  std::map<int, run_info> runmap;
92  std::map<int, int> eventcounts;
93  while (fgets(line, 480, fp)) {
94  if (line[0] != '-') {
95  continue;
96  }
97 
98  int s = sscanf(&line[0],
99  "--- LumiCalculator : %d[%d]: L1Acc: %d, Livetime trigger L1Acc: %d, InstLumi: %f, deltaT: %f, AvEvtsPerBX: %f, BeforePrescale: %d, AfterPrescale: %d, Livetime trigger BeforePrescale: %d Livetime trigger AfterPrescale: %d, Livefrac: %f, L1Presc: %f, HLTPresc: %f, Valid: %d, LAr ready fraction: %f",
100  &run, &lbn, &L1Acc, &liveL1, &instlumi, &dt, &avevtperbx, &beforeps, &afterps, &livebp, &liveap, &live, &L1p, &HLTp, &valid, &LARp);
101 
102  if (s > 8) {
103  printf("Got %d values : run=%d, lbn=%d, L1Acc=%d, instlumi=%f, L1p=%f, dt=%f, live=%f, afterps=%d \n", s, run, lbn, L1Acc, instlumi, L1p, dt, live, afterps);
104 
105  if (run != grun) {
106  if (grun > 0) { //change of run
107  runmap[grun].intlumi = gtotallumi;
108  printf("Setting lumi for run %d to %f\n", grun, gtotallumi);
109  gtotaltotallumi += gtotallumi;
110  gtotaltotallumiprescaled += gtotallumiprescaled;
111  // reset the int lumi calculation for this run
112  gtotallumi = 0;
113  gtotallumiprescaled = 0;
114  }
115 
116  grun = run; //save the run number
117  printf("Setting grun to %d\n", run);
118 
119  // read in event counts from runquery file for run
120  int status = readeventcounts(run, eventcounts);
121  if (status != 0) {
122  delete[] line;
123  fclose(fp);
124  return status;
125  }
126  } //new run
127 
128  if (L1p < 0) {
129  runmap[run].lbnmap[lbn].nevt = 0;
130  if (eventcounts[lbn] !=0 ) {
131  printf("For lbn %d, L1p<0 but eventcounts is %d\n",lbn,eventcounts[lbn]);
132  }
133  } else {
134  int ne = L1Acc;//afterps
135  if (ne != eventcounts[lbn]) {
136  if (stream == 0) {
137  printf("For lbn %d, ne from lumicalc is %d but we will trust runquery value of %d\n", lbn, ne, eventcounts[lbn]);
138  }
139  ne = eventcounts[lbn];
140  }
141  if (ne == 0) {
142  dt = 0;
143  printf("For lbn %d, setting lumi to 0 since 0 events were recorded\n", lbn);
144  }
145  runmap[run].lbnmap[lbn].nevt = ne;
146  runmap[run].lbnmap[lbn].intlumi = instlumi * dt * live;
147  runmap[run].nevt += ne;
148  gtotallumiprescaled += instlumi * dt * live / L1p;
149  gtotallumi += instlumi * dt * live;
150  }
151  printf("grun=%d, gtotallumi=%f, gtotallumiprescaled=%f\n", grun, gtotallumi, gtotallumiprescaled);
152  } //good line
153  } //loop over lines in file
154  delete[] line;
155 
156  // after last run
157  runmap[grun].intlumi = gtotallumi;
158  printf("Setting lumi for run %d to %f\n", grun, gtotallumi);
159  gtotaltotallumi += gtotallumi;
160  gtotaltotallumiprescaled += gtotallumiprescaled;
161 
162  fclose(fp);
163  //cppcheck-suppress invalidPrintfArgType_uint
164  printf("- %lu runs, gtotaltotallumi=%f, gtotaltotallumiprescaled=%f\n", runmap.size(), gtotaltotallumi, gtotaltotallumiprescaled);
165 
166  if (runmap.size() < 1) {
167  //cppcheck-suppress invalidPrintfArgType_uint
168  printf("- runmap size is %lu, quitting!\n", runmap.size());
169  return 0;
170  }
171 
172  //check the total lumi...
173  double tempr{};
174  for (const auto &[r, info] : runmap) {
175  tempr += info.intlumi;
176  }
177  if (fabs(tempr - gtotaltotallumi) / tempr > 0.001) {
178  fprintf(stderr, "tempr=%f and gtotaltotallumi=%f\n", tempr, gtotaltotallumi);
179  return 3;
180  }
181 
182  for (int e = 0; e < nwanted; ++e) {
183  // pick a random run, proportional to intlumi
184  double rnd = myrand.Uniform(tempr);
185  run = -1;
186  for (const auto &[r, info] : runmap) {
187  if (rnd < info.intlumi) {
188  run = r;
189  break;
190  }
191  rnd -= info.intlumi;
192  }
193  if (run < 0) { // error
194  return 4;
195  }
196 
197  // check the total run lumi...
198  double tempt{};
199  for (const auto &[l, info] : runmap[run].lbnmap) {
200  tempt += info.intlumi;
201  }
202  if (fabs(tempt - runmap[run].intlumi) / tempt > 0.001) {
203  fprintf(stderr, "tempt=%f and runmap[%d].intlumi=%f\n", tempt, run, runmap[run].intlumi);
204  return 5;
205  }
206 
207  //p ick a random lbn, proportional to intlumi
208  rnd = myrand.Uniform(tempt);
209  lbn = -1;
210  for (const auto &[l, info] : runmap[run].lbnmap) {
211  if (rnd < info.intlumi) {
212  lbn = l;
213  break;
214  }
215  rnd -= info.intlumi;
216  }
217  if (lbn < 0) { // error
218  return 6;
219  }
220 
221  runmap[run].nevtwanted++;
222  runmap[run].lbnmap[lbn].nevtwanted++;
223  printf("- stream %d, run %d, lbn %d, choose %d of out %d\n", stream, run, lbn, runmap[run].lbnmap[lbn].nevtwanted, runmap[run].lbnmap[lbn].nevt);
224  } //loop over nwanted
225 
226  for (const auto &[r, rinfo] : runmap) {
227  int totnevt{};
228  float totintlumi{};
229  printf("stream %d, run %d, has %d events and %f/ub, %f intlumi of total, and %d wanted\n", stream,
230  r, rinfo.nevt, rinfo.intlumi, rinfo.intlumi / gtotaltotallumi, rinfo.nevtwanted);
231  for (const auto &[l, linfo] : rinfo.lbnmap) {
232  fprintf(outfile, "stream %d, run %d, lbn %d, has %d events and %f/ub, %f intlumi of run, and %d wanted", stream, r,
233  l, linfo.nevt, linfo.intlumi, linfo.intlumi / rinfo.intlumi, linfo.nevtwanted);
234  if (linfo.nevtwanted > linfo.nevt) {
235  fprintf(outfile, " : WARNING, more than available, will be duplicates!\n");
236  } else {
237  fprintf(outfile,"\n");
238  }
239  totnevt += linfo.nevt;
240  totintlumi += linfo.intlumi;
241  }
242  if (totnevt != rinfo.nevt) {
243  fprintf(stderr, " XXX events do not agree !!! \n");
244  }
245  if (totintlumi > 0.0 && fabs(totintlumi - rinfo.intlumi) / totintlumi > .001) {
246  fprintf(stderr, " XXX intlumi does not agree !!! %f %f \n", totintlumi, rinfo.intlumi);
247  }
248  }
249 
250  printf("--\n\n");
251 
252  return 0;
253 }
254 
255 int main(int argc, const char* argv[])
256 {
257  int Nstreams{50}; // number of zerobias streams to make
258  int Nwanted{50000}; // number of events per zerobias stream
259 
260  if (argc == 3) {
261  Nstreams = std::atoi(argv[1]);
262  Nwanted = std::atoi(argv[2]);
263  }
264 
265  printf("Nstreams=%d, Nwanted=%d\n\n", Nstreams, Nwanted);
266 
267  FILE *outfile = fopen("lbn_anal_map.txt", "w");
268 
269  for (int i = 0; i < Nstreams; ++i) {
270  int status = lbn_analyze(i, Nwanted, outfile);
271  if (status != 0) {
272  return status;
273  }
274  }
275 
276  fclose(outfile);
277 
278  return 0;
279 }
grepfile.info
info
Definition: grepfile.py:38
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
beamspotman.r
def r
Definition: beamspotman.py:676
run_info::nevt
int nevt
Definition: LB_analyze.cxx:18
checkFileSG.line
line
Definition: checkFileSG.py:75
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
lbn_info::nevt
int nevt
Definition: LB_analyze.cxx:11
run_info
Definition: LB_analyze.cxx:17
run_info::intlumi
float intlumi
Definition: LB_analyze.cxx:19
lbn_info::intlumi
float intlumi
Definition: LB_analyze.cxx:12
run
int run(int argc, char *argv[])
Definition: ttree2hdf5.cxx:28
myrand
TRandom3 myrand
Definition: ByteStreamMultipleOutputStreamCopyTool.cxx:27
get_generator_info.stderr
stderr
Definition: get_generator_info.py:40
UploadAMITag.l
list l
Definition: UploadAMITag.larcaf.py:158
dqt_zlumi_display_z_rate.instlumi
instlumi
Definition: dqt_zlumi_display_z_rate.py:56
AthenaPoolTestWrite.stream
string stream
Definition: AthenaPoolTestWrite.py:12
lbn_analyze
int lbn_analyze(int stream, int nwanted, FILE *outfile)
Definition: LB_analyze.cxx:67
calibdata.valid
list valid
Definition: calibdata.py:45
lbn_info
Definition: LB_analyze.cxx:10
lumiFormat.i
int i
Definition: lumiFormat.py:85
trigmenu_modify_prescale_json.fp
fp
Definition: trigmenu_modify_prescale_json.py:53
LArCellNtuple.argv
argv
Definition: LArCellNtuple.py:152
CaloNoise_fillDB.dt
dt
Definition: CaloNoise_fillDB.py:58
lbn_info::nevtwanted
int nevtwanted
Definition: LB_analyze.cxx:13
run
Definition: run.py:1
DQHistogramMergeRegExp.argc
argc
Definition: DQHistogramMergeRegExp.py:20
readeventcounts
int readeventcounts(int run, std::map< int, int > &eventcounts)
Definition: LB_analyze.cxx:25
main
int main(int argc, const char *argv[])
Definition: LB_analyze.cxx:255
TRT_PAI_physicsConstants::mb
const double mb
1mb to cm2
Definition: TRT_PAI_physicsConstants.h:15
lumiFormat.intlumi
intlumi
Definition: lumiFormat.py:107
LB_AnalMapSplitter.linfo
linfo
Definition: LB_AnalMapSplitter.py:52
run_info::lbnmap
std::map< int, lbn_info > lbnmap
Definition: LB_analyze.cxx:20
CxxUtils::atoi
int atoi(std::string_view str)
Helper functions to unpack numbers decoded in string into integers and doubles The strings are requir...
Definition: Control/CxxUtils/Root/StringUtils.cxx:85
merge.status
status
Definition: merge.py:17
PrepareReferenceFile.outfile
outfile
Definition: PrepareReferenceFile.py:42
run_info::nevtwanted
int nevtwanted
Definition: LB_analyze.cxx:21
LB_AnalMapSplitter.lbn
lbn
Definition: LB_AnalMapSplitter.py:28