Bug Summary

File:alld2/home/sdobbs/Software/jana/jana_0.8.2/Linux_CentOS7.7-x86_64-gcc4.8.5/include/JANA/JEventLoop.h
Warning:line 398, column 7
Null pointer passed to 2nd parameter expecting 'nonnull'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -main-file-name DL1MCTrigger_factory.cc -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /w/halld-scifs17exp/home/sdobbs/clang/llvm-project/install/lib/clang/12.0.0 -D HAVE_CCDB -D HAVE_RCDB -D HAVE_EVIO -D HAVE_TMVA=1 -D RCDB_MYSQL=1 -D RCDB_SQLITE=1 -D SQLITE_USE_LEGACY_STRUCT=ON -I .Linux_CentOS7.7-x86_64-gcc4.8.5/libraries/TRIGGER -I libraries/TRIGGER -I . -I libraries -I libraries/include -I /w/halld-scifs17exp/home/sdobbs/clang/halld_recon/Linux_CentOS7.7-x86_64-gcc4.8.5/include -I external/xstream/include -I /usr/include/tirpc -I /group/halld/Software/builds/Linux_CentOS7.7-x86_64-gcc4.8.5/root/root-6.08.06/include -I /w/halld-scifs17exp/halld2/home/sdobbs/Software/jana/jana_0.8.2/Linux_CentOS7.7-x86_64-gcc4.8.5/include -I /group/halld/Software/builds/Linux_CentOS7.7-x86_64-gcc4.8.5/ccdb/ccdb_1.06.06/include -I /group/halld/Software/builds/Linux_CentOS7.7-x86_64-gcc4.8.5/rcdb/rcdb_0.06.00/cpp/include -I /usr/include/mysql -I /group/halld/Software/builds/Linux_CentOS7.7-x86_64-gcc4.8.5/sqlitecpp/SQLiteCpp-2.2.0^bs130/include -I /group/halld/Software/builds/Linux_CentOS7.7-x86_64-gcc4.8.5/sqlite/sqlite-3.13.0^bs130/include -I /group/halld/Software/builds/Linux_CentOS7.7-x86_64-gcc4.8.5/hdds/hdds-4.9.0/Linux_CentOS7.7-x86_64-gcc4.8.5/src -I /group/halld/Software/builds/Linux_CentOS7.7-x86_64-gcc4.8.5/xerces-c/xerces-c-3.1.4/include -I /group/halld/Software/builds/Linux_CentOS7.7-x86_64-gcc4.8.5/evio/evio-4.4.6/Linux-x86_64/include -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../include/c++/4.8.5 -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../include/c++/4.8.5/x86_64-redhat-linux -internal-isystem /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../include/c++/4.8.5/backward -internal-isystem /usr/local/include -internal-isystem /w/halld-scifs17exp/home/sdobbs/clang/llvm-project/install/lib/clang/12.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /home/sdobbs/work/clang/halld_recon/src -ferror-limit 19 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -o /tmp/scan-build-2021-01-21-110224-160369-1 -x c++ libraries/TRIGGER/DL1MCTrigger_factory.cc

libraries/TRIGGER/DL1MCTrigger_factory.cc

1// Test version V0 of the L1 trigger simulation, Aug 18, 2017 (asomov)
2
3#include <iostream>
4#include <iomanip>
5#include <cmath>
6using namespace std;
7
8#include <JANA/JApplication.h>
9#include <DAQ/DCODAROCInfo.h>
10#include <DAQ/DL1Info.h>
11#include <DANA/DStatusBits.h>
12
13#include <HDDM/DEventSourceHDDM.h>
14
15using namespace jana;
16
17#include "DL1MCTrigger_factory.h"
18
19#if HAVE_RCDB1
20#include "RCDB/Connection.h"
21#include "RCDB/ConfigParser.h"
22#endif
23
24static bool print_data_message = true;
25
26//------------------
27// init
28//------------------
29jerror_t DL1MCTrigger_factory::init(void)
30{
31
32 debug = 0;
33
34 if(debug){
35 hfcal_gains = new TH1F("fcal_gains", "fcal_gains", 80, -1., 3.);
36 hfcal_gains2 = new TH2F("fcal_gains2","fcal_gains2", 71, -142., 142., 71, -142., 142.);
37 hfcal_ped = new TH1F("fcal_ped", "fcal_ped", 800, 0., 200.);
38 }
39
40 BYPASS = 0; // default is to use trigger emulation
41
42 // Default parameters for the main production trigger are taken from the
43 // spring run of 2017: 25 F + B > 45000
44
45 FCAL_ADC_PER_MEV = 3.73;
46 FCAL_CELL_THR = 65;
47 FCAL_EN_SC = 25;
48 FCAL_NSA = 10;
49 FCAL_NSB = 3;
50 FCAL_WINDOW = 10;
51
52 BCAL_ADC_PER_MEV = 34.48276; // Not corrected energy
53 BCAL_CELL_THR = 20;
54 BCAL_EN_SC = 1;
55 BCAL_NSA = 19;
56 BCAL_NSB = 3;
57 BCAL_WINDOW = 20;
58
59 FCAL_BCAL_EN = 45000;
60
61 ST_ADC_PER_MEV = 1.;
62 ST_CELL_THR = 40;
63 ST_NSA = 10;
64 ST_NSB = 3;
65 ST_WINDOW = 10;
66 ST_NHIT = 1;
67
68 BCAL_OFFSET = 2;
69
70 SIMU_BASELINE = 1;
71 SIMU_GAIN = 1;
72
73
74 simu_baseline_fcal = 1;
75 simu_baseline_bcal = 1;
76
77 simu_gain_fcal = 1;
78 simu_gain_bcal = 1;
79
80 gPARMS->SetDefaultParameter("TRIG:BYPASS", BYPASS,
81 "Bypass trigger by hard coding physics bit");
82 gPARMS->SetDefaultParameter("TRIG:FCAL_ADC_PER_MEV", FCAL_ADC_PER_MEV,
83 "FCAL energy calibration for the Trigger");
84 gPARMS->SetDefaultParameter("TRIG:FCAL_CELL_THR", FCAL_CELL_THR,
85 "FCAL energy threshold per cell");
86 gPARMS->SetDefaultParameter("TRIG:FCAL_EN_SC", FCAL_EN_SC,
87 "FCAL energy threshold");
88 gPARMS->SetDefaultParameter("TRIG:FCAL_NSA", FCAL_NSA,
89 "FCAL NSA");
90 gPARMS->SetDefaultParameter("TRIG:FCAL_NSB", FCAL_NSB,
91 "FCAL NSB");
92 gPARMS->SetDefaultParameter("TRIG:FCAL_WINDOW", FCAL_WINDOW,
93 "FCAL GTP integration window");
94
95 gPARMS->SetDefaultParameter("TRIG:BCAL_ADC_PER_MEV", BCAL_ADC_PER_MEV,
96 "BCAL energy calibration for the Trigger");
97 gPARMS->SetDefaultParameter("TRIG:BCAL_CELL_THR", BCAL_CELL_THR,
98 "BCAL energy threshold per cell");
99 gPARMS->SetDefaultParameter("TRIG:BCAL_EN_SC", BCAL_EN_SC,
100 "BCAL energy threshold");
101 gPARMS->SetDefaultParameter("TRIG:BCAL_NSA", BCAL_NSA,
102 "BCAL NSA");
103 gPARMS->SetDefaultParameter("TRIG:BCAL_NSB", BCAL_NSB,
104 "BCAL NSB");
105 gPARMS->SetDefaultParameter("TRIG:BCAL_WINDOW", BCAL_WINDOW,
106 "BCAL GTP integration window");
107
108 gPARMS->SetDefaultParameter("TRIG:ST_ADC_PER_MEV", ST_ADC_PER_MEV,
109 "ST energy calibration for the Trigger");
110 gPARMS->SetDefaultParameter("TRIG:ST_CELL_THR", ST_CELL_THR,
111 "ST energy threshold per cell");
112 gPARMS->SetDefaultParameter("TRIG:ST_NSA", ST_NSA,
113 "ST NSA");
114 gPARMS->SetDefaultParameter("TRIG:ST_NSB", ST_NSB,
115 "ST NSB");
116 gPARMS->SetDefaultParameter("TRIG:ST_WINDOW", ST_WINDOW,
117 "ST window for merging hits (GTP)");
118 gPARMS->SetDefaultParameter("TRIG:ST_NHIT", ST_NHIT,
119 "Number of hits in ST");
120
121 gPARMS->SetDefaultParameter("TRIG:FCAL_BCAL_EN", FCAL_BCAL_EN,
122 "Energy threshold for the FCAL & BCAL trigger");
123
124 gPARMS->SetDefaultParameter("TRIG:BCAL_OFFSET", BCAL_OFFSET,
125 "Timing offset between BCAL and FCAL energies at GTP (sampels)");
126
127
128 // Allows to switch off gain and baseline fluctuations
129 gPARMS->SetDefaultParameter("TRIG:SIMU_BASELINE", SIMU_BASELINE,
130 "Enable simulation of pedestal variations");
131
132 gPARMS->SetDefaultParameter("TRIG:SIMU_GAIN", SIMU_GAIN,
133 "Enable simulation of gain variations");
134
135
136 BCAL_ADC_PER_MEV_CORRECT = 22.7273;
137
138 pedestal_sigma = 1.2;
139
140 time_shift = 100;
141
142 time_min = 0;
143 time_max = (sample - 1)*max_adc_bins;
144
145 vector< vector<double > > fcal_gains_temp(DFCALGeometry::kBlocksTall,
146 vector<double>(DFCALGeometry::kBlocksWide));
147 vector< vector<double > > fcal_pedestals_temp(DFCALGeometry::kBlocksTall,
148 vector<double>(DFCALGeometry::kBlocksWide));
149
150 fcal_gains = fcal_gains_temp;
151 fcal_pedestals = fcal_pedestals_temp;
152
153 return NOERROR;
154}
155
156
157//------------------
158// brun
159//------------------
160jerror_t DL1MCTrigger_factory::brun(jana::JEventLoop *eventLoop, int32_t runnumber)
161{
162
163 int use_rcdb = 1;
164
165 int status = 0;
166
167 fcal_trig_mask.clear();
168 bcal_trig_mask.clear();
169
170 triggers_enabled.clear();
171
172 // Only print messages for one thread whenever run number change
173
174 static pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER{ { 0, 0, 0, 0, 0, 0, 0, { 0, 0 } } };
175 static set<int> runs_announced;
176 pthread_mutex_lock(&print_mutex);
177 bool print_messages = false;
178
179 if(runs_announced.find(runnumber) == runs_announced.end()){
180 print_messages = true;
181 runs_announced.insert(runnumber);
182 }
183
184 pthread_mutex_unlock(&print_mutex);
185
186 // Don't use RCDB for mc_generic: ideal MC simulation
187
188 string JANA_CALIB_CONTEXT = "";
189
190 if(getenv("JANA_CALIB_CONTEXT") != NULL__null ){
191 JANA_CALIB_CONTEXT = getenv("JANA_CALIB_CONTEXT");
192 if(JANA_CALIB_CONTEXT.find("mc_generic") != string::npos){
193 use_rcdb = 0;
194 // Don't simulate baseline fluctuations for mc_generic
195 simu_baseline_fcal = 0;
196 simu_baseline_bcal = 0;
197 // Don't simulate gain fluctuations for mc_generic
198 simu_gain_fcal = 0;
199 simu_gain_bcal = 0;
200 }
201 }
202
203 // runnumber = 30942;
204
205 if(use_rcdb == 1){
206 status = Read_RCDB(runnumber, print_messages);
207 if(print_messages) PrintTriggers();
208 }
209
210
211 if( (use_rcdb == 0) || (status > 0) || (triggers_enabled.size() == 0)){
212
213 // Simulate FCAL & BCAL main production trigger only
214
215 trigger_conf trig_tmp;
216 trig_tmp.bit = 0;
217 trig_tmp.gtp.fcal = FCAL_EN_SC;
218 trig_tmp.gtp.bcal = BCAL_EN_SC;
219 trig_tmp.gtp.en_thr = FCAL_BCAL_EN;
220 trig_tmp.gtp.fcal_min = 200;
221 triggers_enabled.push_back(trig_tmp);
222
223 if(print_messages)
224 cout << " Do not use RCDB for the trigger simulation. Default (spring 2017) trigger settings are used " << endl;
225 }
226
227
228 // extract the FCAL Geometry
229 vector<const DFCALGeometry*> fcalGeomVect;
230 eventLoop->Get( fcalGeomVect );
231 if (fcalGeomVect.size() < 1)
232 return OBJECT_NOT_AVAILABLE;
233 const DFCALGeometry& fcalGeom = *(fcalGeomVect[0]);
234
235 if(print_messages) jout << "In DL1MCTrigger_factory, loading constants..." << endl;
236
237 vector< double > fcal_gains_ch;
238 vector< double > fcal_pedestals_ch;
239
240 if (eventLoop->GetCalib("/FCAL/gains", fcal_gains_ch)){
241 jout << "DL1MCTrigger_factory: Error loading /FCAL/gains !" << endl;
242 // Load default values of gains if CCDB table is not found
243 for(int ii = 0; ii < DFCALGeometry::kBlocksTall; ii++){
244 for(int jj = 0; jj < DFCALGeometry::kBlocksWide; jj++){
245 fcal_gains[ii][jj] = 1.;
246 }
247 }
248 } else {
249 LoadFCALConst(fcal_gains, fcal_gains_ch, fcalGeom);
250
251 if(debug){
252 for(int ch = 0; ch < (int)fcal_gains_ch.size(); ch++){
253 int row = fcalGeom.row(ch);
254 int col = fcalGeom.column(ch);
255 // Sanity check for regular FCAL (row,col) ranges (anticipating
256 // future upgrade to FCAL to include insert)
257 if(fcalGeom.isBlockActive(row,col)&&row<DFCALGeometry::kBlocksTall
258 && col<DFCALGeometry::kBlocksWide){
259 hfcal_gains->Fill(fcal_gains[row][col]);
260 DVector2 aaa = fcalGeom.positionOnFace(row,col);
261 hfcal_gains2->Fill(float(aaa.X()), float(aaa.Y()), fcal_gains[row][col]);
262 // cout << aaa.X() << " " << aaa.Y() << endl;
263
264 }
265 }
266 }
267
268 }
269
270 if (eventLoop->GetCalib("/FCAL/pedestals", fcal_pedestals_ch)){
271 jout << "DL1MCTrigger_factory: Error loading /FCAL/pedestals !" << endl;
272 // Load default values of pedestals if CCDB table is not found
273 for(int ii = 0; ii < DFCALGeometry::kBlocksTall; ii++){
274 for(int jj = 0; jj < DFCALGeometry::kBlocksWide; jj++){
275 fcal_pedestals[ii][jj] = 100.;
276 }
277 }
278 } else {
279 LoadFCALConst(fcal_pedestals, fcal_pedestals_ch, fcalGeom);
280
281 if(debug){
282 for(int ch = 0; ch < (int)fcal_gains_ch.size(); ch++){
283 int row = fcalGeom.row(ch);
284 int col = fcalGeom.column(ch);
285 // Sanity check for regular FCAL (row,col) ranges (anticipating
286 // future upgrade to FCAL to include insert)
287 if(fcalGeom.isBlockActive(row,col)&&row<DFCALGeometry::kBlocksTall
288 && col<DFCALGeometry::kBlocksWide){
289 hfcal_ped->Fill(fcal_pedestals[row][col]);
290 }
291 }
292 }
293
294 }
295
296 if(!SIMU_BASELINE){
297 simu_baseline_fcal = 0;
298 simu_baseline_bcal = 0;
299 }
300
301 if(!SIMU_GAIN){
302 simu_gain_fcal = 0;
303 simu_gain_bcal = 0;
304 }
305
306 if(debug){
307 for(int ii = 0; ii < 100; ii++){
308 cout << " Channel = " << ii << " Value = " <<
309 fcal_gains_ch[ii] << endl;
310 }
311 }
312
313
314 return NOERROR;
315}
316
317//------------------
318// evnt
319//------------------
320jerror_t DL1MCTrigger_factory::evnt(JEventLoop *loop, uint64_t eventnumber){
321
322 if(BYPASS) {
323 DL1MCTrigger *trigger = new DL1MCTrigger;
324 trigger->trig_mask = 1;
325 _data.push_back(trigger);
326 return NOERROR;
327 }
328
329 int l1_found = 1;
330
331 int status = 0;
332
333 fcal_signal_hits.clear();
334 bcal_signal_hits.clear();
335
336 fcal_merged_hits.clear();
337 bcal_merged_hits.clear();
338
339 memset(fcal_ssp,0,sizeof(fcal_ssp));
340 memset(fcal_gtp,0,sizeof(fcal_gtp));
341
342 memset(bcal_ssp,0,sizeof(bcal_ssp));
343 memset(bcal_gtp,0,sizeof(bcal_gtp));
344
345
346 vector<const DFCALHit*> fcal_hits;
347 vector<const DBCALHit*> bcal_hits;
348
349 loop->Get(fcal_hits);
350 loop->Get(bcal_hits);
351
352 DRandom2 gDRandom(0); // declared extern in DRandom2.h
353
354 // This is temporary, to allow this simulation to be run on data
355 // to help out with trigger efficiency studies - sdobbs (Aug. 26, 2020)
356 if( loop->GetJEvent().GetStatusBit(kSTATUS_EVIO) ){
357 if(print_data_message) {
358 jout << "WARNING: Running L1 trigger simulation on EVIO data" << endl;
359 print_data_message = false;
360 }
361
362 // for data, don't add in baseline shifts, since they already exist
363 simu_baseline_fcal = 0;
364 simu_baseline_bcal = 0;
365 } else {
366 // Initialize random number generator
367 // Read seeds from hddm file
368 // Generate seeds according to the event number if they are not stored in hddm
369 // The proceedure is consistent with the mcsmear
370
371 UInt_t seed1 = 0;
372 UInt_t seed2 = 0;
373 UInt_t seed3 = 0;
374
375 GetSeeds(loop, eventnumber, seed1, seed2, seed3);
376
377 gDRandom.SetSeeds(seed1, seed2, seed3);
378 }
379
380 // cout << endl;
381 // cout << " Event = " << eventnumber << endl;
382 // cout << " Seed 1: " << seed1 << endl;
383 // cout << " Seed 2: " << seed2 << endl;
384 // cout << " Seed 3: " << seed3 << endl;
385 // cout << endl;
386
387
388 DL1MCTrigger *trigger = new DL1MCTrigger;
389
390 // FCAL energy sum
391 double fcal_hit_en = 0;
392
393 for (unsigned int ii = 0; ii < fcal_hits.size(); ii++){
394 int row = fcal_hits[ii]->row;
395 int col = fcal_hits[ii]->column;
396
397 // Shift time to simulate pile up hits
398 double time = fcal_hits[ii]->t + time_shift;
399 if((time < time_min) || (time > time_max)){
400 continue;
401 }
402
403 // Check channels masked for trigger
404 int ch_masked = 0;
405 for(unsigned int jj = 0; jj < fcal_trig_mask.size(); jj++){
406 if( (row == fcal_trig_mask[jj].row) && (col == fcal_trig_mask[jj].col)){
407 ch_masked = 1;
408 break;
409 }
410 }
411
412 if(ch_masked == 0){
413 fcal_hit_en += fcal_hits[ii]->E;
414
415 fcal_signal fcal_tmp;
416
417 fcal_tmp.row = row;
418 fcal_tmp.column = col;
419
420 fcal_tmp.energy = fcal_hits[ii]->E;
421 fcal_tmp.time = time;
422 memset(fcal_tmp.adc_amp,0,sizeof(fcal_tmp.adc_amp));
423 memset(fcal_tmp.adc_en, 0,sizeof(fcal_tmp.adc_en));
424
425 double fcal_adc_en = fcal_tmp.energy*FCAL_ADC_PER_MEV*1000;
426
427 // Account for gain fluctuations
428 if(simu_gain_fcal && row<DFCALGeometry::kBlocksTall
429 && col<DFCALGeometry::kBlocksWide){
430
431 double gain = fcal_gains[row][col];
432
433 fcal_adc_en *= gain;
434 }
435
436 status = SignalPulse(fcal_adc_en, fcal_tmp.time, fcal_tmp.adc_en, 1);
437 status = 0;
438
439 fcal_signal_hits.push_back(fcal_tmp);
440 }
441
442 }
443
444
445 // Merge FCAL hits
446 for(unsigned int ii = 0; ii < fcal_signal_hits.size(); ii++){
447
448 if(fcal_signal_hits[ii].merged == 1) continue;
449
450 fcal_signal fcal_tmp;
451 fcal_tmp.row = fcal_signal_hits[ii].row;
452 fcal_tmp.column = fcal_signal_hits[ii].column;
453 fcal_tmp.energy = 0.;
454 fcal_tmp.time = 0.;
455 for(int kk = 0; kk < sample; kk++)
456 fcal_tmp.adc_en[kk] = fcal_signal_hits[ii].adc_en[kk];
457
458 for(unsigned int jj = ii + 1; jj < fcal_signal_hits.size(); jj++){
459 if((fcal_signal_hits[ii].row == fcal_signal_hits[jj].row) &&
460 (fcal_signal_hits[ii].column == fcal_signal_hits[jj].column)){
461
462 fcal_signal_hits[jj].merged = 1;
463
464 for(int kk = 0; kk < sample; kk++)
465 fcal_tmp.adc_en[kk] += fcal_signal_hits[jj].adc_en[kk];
466 }
467 }
468
469 fcal_merged_hits.push_back(fcal_tmp);
470 }
471
472 // Add baseline fluctuations for channels with hits
473 if(simu_baseline_fcal){
474 for(unsigned int ii = 0; ii < fcal_merged_hits.size(); ii++){
475 int row = fcal_merged_hits[ii].row;
476 int column = fcal_merged_hits[ii].column;
477 double pedestal = 100.0;
478 if (row<DFCALGeometry::kBlocksTall
479 && column<DFCALGeometry::kBlocksWide){
480 pedestal=fcal_pedestals[row][column];
481 }
482 AddBaseline(fcal_merged_hits[ii].adc_en, pedestal, gDRandom);
483 }
484 }
485
486
487 // Digitize
488 for(unsigned int ii = 0; ii < fcal_merged_hits.size(); ii++){
489 Digitize(fcal_merged_hits[ii].adc_en,fcal_merged_hits[ii].adc_amp);
490 // cout << " Digitize " << fcal_merged_hits[ii].adc_en[sample - 3]
491 // << " " << fcal_merged_hits[ii].adc_amp[sample - 3] << endl;
492 }
493
494
495 int fcal_hit_adc_en = 0;
496
497 for(unsigned int ii = 0; ii < fcal_merged_hits.size(); ii++)
498 for(int jj = 0; jj < sample; jj++)
499 fcal_hit_adc_en += fcal_merged_hits[ii].adc_amp[jj];
500
501
502 status += FADC_SSP(fcal_merged_hits, 1);
503
504 status += GTP(1);
505
506
507
508 // BCAL energy sum
509 double bcal_hit_en = 0;
510
511 for (unsigned int ii = 0; ii < bcal_hits.size(); ii++){
512
513 // Shift time to simulate pile up hits
514 double time = bcal_hits[ii]->t + time_shift;
515 if((time < time_min) || (time > time_max)){
516 continue;
517 }
518
519 int module = bcal_hits[ii]->module;
520 int layer = bcal_hits[ii]->layer;
521 int sector = bcal_hits[ii]->sector;
522 int end = bcal_hits[ii]->end;
523
524 // Check trigger masks
525 int ch_masked = 0;
526
527 for(unsigned int jj = 0; jj < bcal_trig_mask.size(); jj++){
528 if( (module == bcal_trig_mask[jj].module) && (layer == bcal_trig_mask[jj].layer) &&
529 (sector == bcal_trig_mask[jj].sector) && (end == bcal_trig_mask[jj].end) ){
530 ch_masked = 1;
531 break;
532 }
533 }
534
535
536 if(ch_masked == 0){
537 bcal_hit_en += bcal_hits[ii]->E;
538
539 bcal_signal bcal_tmp;
540 bcal_tmp.module = module;
541 bcal_tmp.layer = layer;
542 bcal_tmp.sector = sector;
543 bcal_tmp.end = end;
544
545 bcal_tmp.energy = bcal_hits[ii]->E;
546 bcal_tmp.time = time;
547 memset(bcal_tmp.adc_amp,0,sizeof(bcal_tmp.adc_amp));
548 memset(bcal_tmp.adc_en, 0,sizeof(bcal_tmp.adc_en));
549
550 double bcal_adc_en = bcal_tmp.energy*BCAL_ADC_PER_MEV*1000;
551
552 status = SignalPulse(bcal_adc_en, bcal_tmp.time, bcal_tmp.adc_en, 2);
553 status = 0;
554
555 bcal_signal_hits.push_back(bcal_tmp);
556 }
557 }
558
559
560 // Merge BCAL hits
561 for(unsigned int ii = 0; ii < bcal_signal_hits.size(); ii++){
562
563 if(bcal_signal_hits[ii].merged == 1) continue;
564
565 bcal_signal bcal_tmp;
566 bcal_tmp.module = bcal_signal_hits[ii].module;
567 bcal_tmp.layer = bcal_signal_hits[ii].layer;
568 bcal_tmp.sector = bcal_signal_hits[ii].sector;
569 bcal_tmp.end = bcal_signal_hits[ii].end;
570
571 bcal_tmp.energy = 0.;
572 bcal_tmp.time = 0.;
573
574 for(int kk = 0; kk < sample; kk++)
575 bcal_tmp.adc_en[kk] = bcal_signal_hits[ii].adc_en[kk];
576
577 for(unsigned int jj = ii + 1; jj < bcal_signal_hits.size(); jj++){
578 if((bcal_signal_hits[ii].module == bcal_signal_hits[jj].module) &&
579 (bcal_signal_hits[ii].layer == bcal_signal_hits[jj].layer) &&
580 (bcal_signal_hits[ii].sector == bcal_signal_hits[jj].sector) &&
581 (bcal_signal_hits[ii].end == bcal_signal_hits[jj].end)){
582
583 bcal_signal_hits[jj].merged = 1;
584
585 for(int kk = 0; kk < sample; kk++)
586 bcal_tmp.adc_en[kk] += bcal_signal_hits[jj].adc_en[kk];
587 }
588 }
589
590 bcal_merged_hits.push_back(bcal_tmp);
591 }
592
593
594 // Add baseline fluctuations for channels with hits
595 if(simu_baseline_bcal){
596 for(unsigned int ii = 0; ii < bcal_merged_hits.size(); ii++){
597 // Assume that all BCAL pedestals are 100
598 double pedestal = TRIG_BASELINE;
599 AddBaseline(bcal_merged_hits[ii].adc_en, pedestal, gDRandom);
600 }
601 }
602
603
604 // Digitize
605 for(unsigned int ii = 0; ii < bcal_merged_hits.size(); ii++)
606 Digitize(bcal_merged_hits[ii].adc_en,bcal_merged_hits[ii].adc_amp);
607
608
609 int bcal_hit_adc_en = 0;
610 for(unsigned int ii = 0; ii < bcal_merged_hits.size(); ii++)
611 for(int jj = 0; jj < sample; jj++)
612 bcal_hit_adc_en += bcal_merged_hits[ii].adc_amp[jj];
613
614
615 status = FADC_SSP(bcal_merged_hits, 2);
616
617 status = GTP(2);
618
619 // Search for triggers
620
621 l1_found = FindTriggers(trigger);
622
623 if(l1_found){
624
625 int fcal_gtp_max = 0;
626 int bcal_gtp_max = 0;
627
628 for(unsigned int ii = 0; ii < sample; ii++){
629 if(fcal_gtp[ii] > fcal_gtp_max) fcal_gtp_max = fcal_gtp[ii];
630 if(bcal_gtp[ii] > bcal_gtp_max) bcal_gtp_max = bcal_gtp[ii];
631 }
632
633 trigger->fcal_en = fcal_hit_en;
634 trigger->fcal_adc = fcal_hit_adc_en;
635 trigger->fcal_adc_en = fcal_hit_adc_en/FCAL_ADC_PER_MEV/1000.;
636 trigger->fcal_gtp = fcal_gtp_max;
637 trigger->fcal_gtp_en = fcal_gtp_max/FCAL_ADC_PER_MEV/1000.;
638
639 trigger->bcal_en = bcal_hit_en;
640 trigger->bcal_adc = bcal_hit_adc_en;
641 trigger->bcal_adc_en = bcal_hit_adc_en/BCAL_ADC_PER_MEV_CORRECT/2./1000.;
642 trigger->bcal_gtp = bcal_gtp_max;
643 trigger->bcal_gtp_en = bcal_gtp_max/BCAL_ADC_PER_MEV_CORRECT/2./1000.;
644
645 _data.push_back(trigger);
646
647 } else{
648 delete trigger;
649 }
650
651 return NOERROR;
652}
653
654//------------------
655// erun
656//------------------
657jerror_t DL1MCTrigger_factory::erun(void)
658{
659 return NOERROR;
660}
661
662//------------------
663// fini
664//------------------
665jerror_t DL1MCTrigger_factory::fini(void)
666{
667 return NOERROR;
668}
669
670//*********************
671// Read RCDB
672//*********************
673
674int DL1MCTrigger_factory::Read_RCDB(int32_t runnumber, bool print_messages)
675{
676
677#if HAVE_RCDB1
678
679 vector<const DTranslationTable*> ttab;
680 eventLoop->Get(ttab);
1
Calling 'JEventLoop::Get'
681
682 vector<string> SectionNames = {"TRIGGER", "GLOBAL", "FCAL", "BCAL", "TOF", "ST", "TAGH",
683 "TAGM", "PS", "PSC", "TPOL", "CDC", "FDC"};
684
685 string RCDB_CONNECTION;
686
687 if( getenv("RCDB_CONNECTION")!= NULL__null )
688 RCDB_CONNECTION = getenv("RCDB_CONNECTION");
689 else
690 RCDB_CONNECTION = "mysql://rcdb@hallddb.jlab.org/rcdb"; // default to outward-facing MySQL DB
691
692
693 rcdb::Connection connection(RCDB_CONNECTION);
694
695 auto rtvsCnd = connection.GetCondition(runnumber, "rtvs");
696
697 if( !rtvsCnd ) {
698 cout<<"'rtvs' condition is not set for run " << runnumber << endl;
699 return 2;
700 }
701
702
703 auto json = rtvsCnd->ToJsonDocument(); // The CODA rtvs is serialized as JSon dictionary.
704 string fileName(json["%(config)"].GetString()); // We need item with name '%(config)'
705
706 auto file = connection.GetFile(runnumber, fileName);
707
708 if(!file) { // If there is no such file, null is returned
709 cout<<"File with name: "<< fileName
710 <<" doesn't exist (not associated) with run: "<< runnumber << endl;
711 return 3;
712 }
713
714 string fileContent = file->GetContent(); // Get file content
715 auto result = rcdb::ConfigParser::Parse(fileContent, SectionNames); // Parse it!
716
717
718 // Pulse parameters for trigger
719
720 auto trig_thr = result.Sections["FCAL"].NameValues["FADC250_TRIG_THR"];
721 auto trig_nsb = result.Sections["FCAL"].NameValues["FADC250_TRIG_NSB"];
722 auto trig_nsa = result.Sections["FCAL"].NameValues["FADC250_TRIG_NSA"];
723
724 if(trig_thr.size() > 0){
725 FCAL_CELL_THR = stoi(trig_thr);
726 if(FCAL_CELL_THR < 0) FCAL_CELL_THR = 0;
727 }
728
729 if(trig_nsb.size() > 0)
730 FCAL_NSB = stoi(trig_nsb);
731
732 if(trig_thr.size() > 0)
733 FCAL_NSA = stoi(trig_nsa);
734
735 trig_thr = result.Sections["BCAL"].NameValues["FADC250_TRIG_THR"];
736 trig_nsb = result.Sections["BCAL"].NameValues["FADC250_TRIG_NSB"];
737 trig_nsa = result.Sections["BCAL"].NameValues["FADC250_TRIG_NSA"];
738
739 if(trig_thr.size() > 0){
740 BCAL_CELL_THR = stoi(trig_thr);
741 if(BCAL_CELL_THR < 0) BCAL_CELL_THR = 0;
742 }
743
744 if(trig_nsb.size() > 0)
745 BCAL_NSB = stoi(trig_nsb);
746
747 if(trig_thr.size() > 0)
748 BCAL_NSA = stoi(trig_nsa);
749
750
751 // List of enabled GTP equations and triggers
752 vector<vector<string>> triggerTypes;
753
754 for(auto row : result.Sections["TRIGGER"].Rows) {
755
756 if(row[0] == "TRIG_TYPE") {
757
758 if(row.size() >= 9){
759 triggerTypes.push_back(row); // The line in config file starts with TRIG_TYPE
760 } else {
761 cout << " Cannot parse TRIG_TYPE. Insufficient number of parameters " << row.size() << endl;
762 }
763 }
764
765
766 // Integration windows for BCAL and FCAL
767 if(row[0] == "TRIG_EQ") {
768 if(row.size() >= 5){
769 if(stoi(row[4]) == 1){ // Trigger enabled
770 if(row[1] == "FCAL")
771 FCAL_WINDOW = stoi(row[3]);
772 if(row[1] == "BCAL_E")
773 BCAL_WINDOW = stoi(row[3]);
774 }
775 }
776 }
777 }
778
779
780
781
782 for(int ii = 0; ii < 32; ii++){
783
784 int trig_found = 0;
785
786 trigger_conf trigger_tmp;
787
788 memset(&trigger_tmp,0,sizeof(trigger_tmp));
789
790 trigger_tmp.bit = ii;
791
792 for(unsigned int jj = 0; jj < triggerTypes.size(); jj++){
793
794 if(triggerTypes[jj].size() < 9) continue; // Minimum 9 parameters are required in the config file
795
796 int bit = stoi(triggerTypes[jj][8]); // Trigger bit
797
798 if( bit == ii){
799
800 // FCAL, BCAL triggers
801 if(triggerTypes[jj][1] == "BFCAL"){
802
803 int fcal = 0;
804 int bcal = 0;
805
806 if(triggerTypes[jj][4].size() > 0) fcal = stoi(triggerTypes[jj][4]);
807 if(triggerTypes[jj][5].size() > 0) bcal = stoi(triggerTypes[jj][5]);
808
809
810 if( (fcal > 0) && (bcal > 0)){ // FCAL & BCAL
811 trigger_tmp.type = 0x3;
812 trigger_tmp.gtp.fcal = fcal;
813 trigger_tmp.gtp.bcal = bcal;
814 if(triggerTypes[jj][6].size() > 0) trigger_tmp.gtp.en_thr = stoi(triggerTypes[jj][6]);
815
816 if(triggerTypes[jj].size() > 9){
817 if((triggerTypes[jj].size() >= 10) && (triggerTypes[jj][9].size() > 0)) trigger_tmp.gtp.fcal_min = stoi(triggerTypes[jj][9]);
818 if((triggerTypes[jj].size() >= 11) && (triggerTypes[jj][10].size() > 0)) trigger_tmp.gtp.fcal_max = stoi(triggerTypes[jj][10]);
819 if((triggerTypes[jj].size() >= 12) && (triggerTypes[jj][11].size() > 0)) trigger_tmp.gtp.bcal_min = stoi(triggerTypes[jj][11]);
820 if((triggerTypes[jj].size() >= 13) && (triggerTypes[jj][12].size() > 0)) trigger_tmp.gtp.bcal_max = stoi(triggerTypes[jj][12]);
821 }
822
823 trig_found++;
824 } else if ((fcal > 0) && (bcal == 0)){ // FCAL only
825 trigger_tmp.type = 0x1;
826 trigger_tmp.gtp.fcal = fcal;
827 if(triggerTypes[jj][6].size() > 0) trigger_tmp.gtp.en_thr = stoi(triggerTypes[jj][6]);
828 if(triggerTypes[jj].size() > 9){
829 if(triggerTypes[jj][9].size() > 0) trigger_tmp.gtp.fcal_min = stoi(triggerTypes[jj][9]);
830 if(triggerTypes[jj][10].size() > 0) trigger_tmp.gtp.fcal_max = stoi(triggerTypes[jj][10]);
831 }
832 trig_found++;
833 } else if ((bcal > 0) && (fcal == 0)){ // BCAL only
834 trigger_tmp.type = 0x2;
835 trigger_tmp.gtp.bcal = bcal;
836 if(triggerTypes[jj][6].size() > 0) trigger_tmp.gtp.en_thr = stoi(triggerTypes[jj][6]);
837 if((triggerTypes[jj].size() >= 12) && (triggerTypes[jj][11].size() > 0)) trigger_tmp.gtp.bcal_min = stoi(triggerTypes[jj][11]);
838 if((triggerTypes[jj].size() >= 13) && (triggerTypes[jj][12].size() > 0)) trigger_tmp.gtp.bcal_max = stoi(triggerTypes[jj][12]);
839
840
841 trig_found++;
842 } else {
843 cout << " Incorrect parameters for BFCAL trigger " << endl;
844 }
845
846 } else if(triggerTypes[jj][1] == "ST"){ // ST
847 trigger_tmp.type = 0x4;
848 if(triggerTypes[jj][7].size() > 0) trigger_tmp.gtp.st_nhit = stoi(triggerTypes[jj][7]);
849 if((triggerTypes[jj].size() >= 14) && (triggerTypes[jj][13].size() > 0)) trigger_tmp.gtp.st_pattern = stoi(triggerTypes[jj][13]);
850
851 trig_found++;
852 } else if(triggerTypes[jj][1] == "PS"){ // PS
853 trigger_tmp.type = 0x8;
854 if(triggerTypes[jj][7].size() > 0) trigger_tmp.gtp.ps_nhit = stoi(triggerTypes[jj][7]);
855 if((triggerTypes[jj].size() >= 14) && (triggerTypes[jj][13].size() > 0)) trigger_tmp.gtp.ps_pattern = stoi(triggerTypes[jj][13]);
856 trig_found++;
857 } else if(triggerTypes[jj][1] == "TAGH"){ // TAGH
858 trigger_tmp.type = 0x10;
859 if(triggerTypes[jj][7].size() > 0) trigger_tmp.gtp.tof_nhit = stoi(triggerTypes[jj][7]);
860 if((triggerTypes[jj].size() >= 14) && (triggerTypes[jj][13].size() > 0)){
861 trigger_tmp.gtp.tagh_pattern = stoi(triggerTypes[jj][13],nullptr,0);
862 }
863
864 trig_found++;
865 } else if(triggerTypes[jj][1] == "TOF"){ // TOF
866 trigger_tmp.type = 0x20;
867 if(triggerTypes[jj][7].size() > 0) trigger_tmp.gtp.ps_nhit = stoi(triggerTypes[jj][7]);
868 if((triggerTypes[jj].size() >= 14) && (triggerTypes[jj][13].size() > 0)) trigger_tmp.gtp.tof_pattern = stoi(triggerTypes[jj][13]);
869 trig_found++;
870 } else {
871 cout << " Incorrect Trigger type " << triggerTypes[jj][1] << endl;
872 }
873
874 if(trig_found > 0){
875 triggers_enabled.push_back(trigger_tmp);
876 }
877
878 } // Trigger lane is enabled
879 } // Trigger types
880
881 }
882
883
884
885 // Load FCAL Trigger Masks
886
887 string comDir = result.Sections["FCAL"].NameValues["FADC250_COM_DIR"];
888 string comVer = result.Sections["FCAL"].NameValues["FADC250_COM_VER"];
889 string userDir = result.Sections["FCAL"].NameValues["FADC250_USER_DIR"];
890 string userVer = result.Sections["FCAL"].NameValues["FADC250_USER_VER"];
891
892
893 for(int crate = 1; crate <= 12; crate++){
894
895 std::string s = std::to_string(crate);
896
897 string comFileName = comDir + "/rocfcal" +s + "_fadc250_" + comVer + ".cnf";
898 string userFileName = userDir + "/rocfcal" +s + "_" + userVer + ".cnf";
899
900 auto userFile = connection.GetFile(runnumber, userFileName);
901
902 if(!userFile) {
903 // cout<<" USER File with name: "<< userFileName
904 // <<" doesn't exist (not associated) with run: "<< runnumber <<endl;
905 continue;
906 }
907
908
909 auto userParseResult = rcdb::ConfigParser::ParseWithSlots(userFile->GetContent(), "FADC250_SLOTS");
910
911
912 for(unsigned int slot = 3; slot <= 21; slot++){
913
914 auto userValues = userParseResult.SectionsBySlotNumber[slot].NameVectors["FADC250_TRG_MASK"]; // Parse it and return
915
916 if(userValues.size() > 0){
917 for (unsigned int ch = 0; ch < userValues.size(); ++ch) {
918
919 if(userValues[ch].size() == 0) continue;
920
921 if(stoi(userValues[ch]) > 0){
922
923 uint32_t roc_id = 10 + crate;
924
925 DTranslationTable::csc_t daq_index = {roc_id, slot, ch };
926
927 DTranslationTable::DChannelInfo channel_info;
928
929 try {
930 channel_info = ttab[0]->GetDetectorIndex(daq_index);
931 }
932
933 catch(...){
934 if(print_messages) cout << "Exception: FCAL channel is not in the translation table " << " Crate = " << 10 + crate << " Slot = " << slot <<
935 " Channel = " << ch << endl;
936 continue;
937 }
938
939 fcal_mod tmp;
940
941 tmp.roc = crate;
942 tmp.slot = slot;
943 tmp.ch = ch;
944
945 tmp.row = channel_info.fcal.row;
946 tmp.col = channel_info.fcal.col;
947
948 fcal_trig_mask.push_back(tmp);
949 }
950
951 }
952
953 }
954
955 } // Loop over slots
956 } // Loop over crates
957
958
959
960
961 // Load BCAL Trigger Masks
962
963 comDir = result.Sections["BCAL"].NameValues["FADC250_COM_DIR"];
964 comVer = result.Sections["BCAL"].NameValues["FADC250_COM_VER"];
965 userDir = result.Sections["BCAL"].NameValues["FADC250_USER_DIR"];
966 userVer = result.Sections["BCAL"].NameValues["FADC250_USER_VER"];
967
968
969 for(int crate = 1; crate <= 12; crate++){
970
971 if( (crate == 3) || (crate == 6) || (crate == 9) || (crate == 12)) continue;
972
973 std::string s = std::to_string(crate);
974
975 string comFileName = comDir + "/rocbcal" +s + "_fadc250_" + comVer + ".cnf";
976 string userFileName = userDir + "/rocbcal" +s + "_" + userVer + ".cnf";
977
978 auto userFile = connection.GetFile(runnumber, userFileName);
979
980 if(!userFile){
981 // cout<<" USER File with name: "<< userFileName
982 // <<" doesn't exist (not associated) with run: "<< runnumber <<endl;
983 continue;
984 }
985
986
987 auto userParseResult = rcdb::ConfigParser::ParseWithSlots(userFile->GetContent(), "FADC250_SLOTS");
988
989 for(unsigned int slot = 3; slot <= 21; slot++){
990
991 auto userValues = userParseResult.SectionsBySlotNumber[slot].NameVectors["FADC250_TRG_MASK"]; // Parse it and return
992
993 if(userValues.size() > 0){
994
995 for (unsigned int ch = 0; ch < userValues.size(); ++ch) {
996
997 if(userValues[ch].size() == 0) continue;
998
999 if(stoi(userValues[ch]) > 0){
1000
1001 uint32_t roc_id = 30 + crate;
1002
1003 DTranslationTable::csc_t daq_index = {roc_id, slot, ch };
1004
1005 DTranslationTable::DChannelInfo channel_info;
1006
1007 try {
1008 channel_info = ttab[0]->GetDetectorIndex(daq_index);
1009 }
1010
1011 catch(...){
1012 cout << "Exception: BCAL channel is not in the translation table " << " Crate = " << 30 + crate << " Slot = " << slot <<
1013 " Channel = " << ch << endl;
1014 continue;
1015 }
1016
1017
1018 bcal_mod tmp;
1019
1020 tmp.roc = crate;
1021 tmp.slot = slot;
1022 tmp.ch = ch;
1023
1024 tmp.module = channel_info.bcal.module;
1025 tmp.layer = channel_info.bcal.layer;
1026 tmp.sector = channel_info.bcal.sector;
1027 tmp.end = channel_info.bcal.end;
1028
1029 bcal_trig_mask.push_back(tmp);
1030 }
1031
1032 }
1033
1034 }
1035
1036 } // Loop over slots
1037 } // Loop over crates
1038
1039 return 0;
1040
1041#else // HAVE_RCDB
1042
1043 return 10; // RCDB is not available
1044
1045#endif
1046
1047}
1048
1049
1050int DL1MCTrigger_factory::SignalPulse(double en, double time, double amp_array[sample], int type){
1051
1052
1053 // Parameterize and digitize pulse shapes. Sum up amplitudes
1054 // type = 1 - FCAL
1055 // type = 2 - BCAL
1056
1057 float exp_par = 0.358;
1058
1059 int pulse_length = 20;
1060
1061 if(type == 2) exp_par = 0.18;
1062
1063 int sample_first = (int)floor(time/time_stamp);
1064
1065 // digitization range
1066 int ind_min = sample_first + 1;
1067 int ind_max = ind_min + pulse_length + 1;
1068
1069 if( (ind_min > sample) || (ind_min < 0)){
1070 // cout << " SignalPulse() FATAL error: time out of range " << time << " " << ind_min << endl;
1071 return 1;
1072 }
1073
1074 if(ind_max > sample){
1075 // cout << " SignalPulse(): ind_max set to maximum" << time << " " << ind_max << endl;
1076 ind_max = sample - 1;
1077 }
1078
1079 for(int i = ind_min; i < ind_max; i++ ){
1080 double adc_t = time_stamp*i - time;
1081 double amp = exp_par*exp_par*exp(-adc_t*exp_par)*adc_t;
1082
1083 // amp_array[i] += (int)(amp*time_stamp*en + 0.5);
1084 // if(amp_array[i] > max_adc_bins){
1085 // amp_array[i] = max_adc_bins;
1086 // }
1087
1088 amp_array[i] += amp*time_stamp*en;
1089
1090 }
1091
1092 return 0;
1093}
1094
1095int DL1MCTrigger_factory::GTP(int detector){
1096
1097 // 1 - FCAL
1098 // 2 - BCAL
1099
1100 int INT_WINDOW = 20;
1101
1102 switch(detector){
1103 case 1:
1104 INT_WINDOW = FCAL_WINDOW;
1105 break;
1106 case 2:
1107 INT_WINDOW = BCAL_WINDOW;
1108 break;
1109 default:
1110 break;
1111 }
1112
1113 int index_min = 0;
1114 int index_max = 0;
1115
1116 for(unsigned int samp = 0; samp < sample; samp++){
1117
1118 index_max = samp;
1119 index_min = samp - INT_WINDOW;
1120
1121 if(index_min < 0) index_min = 0;
1122
1123 int energy_sum = 0;
1124
1125 for(int ii = index_min; ii <= index_max; ii++){
1126 if(detector == 1)
1127 energy_sum += fcal_ssp[ii];
1128 else
1129 energy_sum += bcal_ssp[ii];
1130 }
1131
1132 if(detector == 1)
1133 fcal_gtp[samp] = energy_sum;
1134 else
1135 bcal_gtp[samp] = energy_sum;
1136 }
1137
1138 return 0;
1139
1140}
1141
1142
1143template <typename T> int DL1MCTrigger_factory::FADC_SSP(vector<T> merged_hits, int detector){
1144
1145 // 1 - FCAL
1146 // 2 - BCAL
1147
1148 int EN_THR = 4096;
1149 int NSA = 10;
1150 int NSB = 3;;
1151
1152 switch(detector){
1153 case 1:
1154 EN_THR = FCAL_CELL_THR;
1155 NSA = FCAL_NSA;
1156 NSB = FCAL_NSB;
1157 break;
1158 case 2:
1159 EN_THR = BCAL_CELL_THR;
1160 NSA = BCAL_NSA;
1161 NSB = BCAL_NSB;
1162 break;
1163 default:
1164 break;
1165 }
1166
1167 for(unsigned int hit = 0; hit < merged_hits.size(); hit++){
1168
1169 int index_min = -10;
1170 int index_max = -10;
1171
1172 for(int ii = 0; ii < sample; ii++){
1173
1174 int pulse_found = 0;
1175
1176 if(merged_hits[hit].adc_amp[ii] >= EN_THR){
1177 pulse_found = 1;
1178 } else {
1179 continue;
1180 }
1181
1182 index_min = ii - NSB;
1183
1184 if(index_max > index_min) index_min = index_max + 1;
1185
1186 index_max = ii + NSA - 1;
1187
1188 if(index_min < 0) index_min = 0;
1189
1190 if(index_max >= sample){
1191 index_max = sample - 1;
1192 }
1193
1194 int extend_nsa = 1;
1195
1196 // Extend FADC range if needed
1197
1198 while(extend_nsa){
1199
1200 int index_tmp = index_max + 1;
1201
1202 if(index_tmp < sample){
1203 if(merged_hits[hit].adc_amp[index_tmp] >= EN_THR){
1204 index_max += NSA;
1205 } else extend_nsa = 0;
1206 } else extend_nsa = 0;
1207 }
1208
1209 if(index_max >= sample)
1210 index_max = sample - 1;
1211
1212 for(int kk = index_min; kk <= index_max; kk++){
1213 if(detector == 1){
1214 if((merged_hits[hit].adc_amp[kk] - 100) > 0)
1215 fcal_ssp[kk] += (merged_hits[hit].adc_amp[kk] - TRIG_BASELINE);
1216 }
1217 else if(detector == 2){
1218 if((merged_hits[hit].adc_amp[kk] - 100) > 0)
1219 bcal_ssp[kk] += (merged_hits[hit].adc_amp[kk] - TRIG_BASELINE);
1220 }
1221 }
1222
1223 if(pulse_found == 1){
1224 ii = index_max + 1;
1225 pulse_found = 0;
1226 }
1227
1228 }
1229
1230 }
1231
1232 return 0;
1233}
1234
1235void DL1MCTrigger_factory::PrintTriggers(){
1236
1237 string detector;
1238 int nhit = 0;
1239 unsigned int pattern = 0;
1240
1241 cout << endl << endl;
1242 cout << " ------------ Trigger Settings --------------- " << endl;
1243 cout << endl << endl;
1244
1245 cout << "----------- FCAL ----------- " << endl << endl;
1246
1247 cout << "FCAL_CELL_THR = " << setw(10) << FCAL_CELL_THR << endl;
1248 cout << "FCAL_NSA = " << setw(10) << FCAL_NSA << endl;
1249 cout << "FCAL_NSB = " << setw(10) << FCAL_NSB << endl;
1250 cout << "FCAL_WINDOW = " << setw(10) << FCAL_WINDOW << endl;
1251
1252 cout << endl;
1253
1254 cout << "----------- BCAL ----------- " << endl << endl;
1255
1256 cout << "BCAL_CELL_THR = " << setw(10) << BCAL_CELL_THR << endl;
1257 cout << "BCAL_NSA = " << setw(10) << BCAL_NSA << endl;
1258 cout << "BCAL_NSB = " << setw(10) << BCAL_NSB << endl;
1259 cout << "BCAL_WINDOW = " << setw(10) << BCAL_WINDOW << endl;
1260
1261 cout << endl << endl;
1262
1263
1264 if(triggers_enabled.size() > 0){
1265 cout << "TYPE " << "FCAL_E " << "BCAL_E " <<
1266 "EN_THR " << "NHIT " << "LANE " << "FCAL_EMIN " << "FCAL_EMAX " <<
1267 "BCAL_EMIN " << "BCAL_EMAX " << "PATTERN " << endl;
1268 }
1269
1270
1271 for(unsigned int ii = 0; ii < triggers_enabled.size(); ii++){
1272
1273 switch(triggers_enabled[ii].type){
1274 case 1: detector = "BFCAL ";
1275 break;
1276 case 2: detector = "BFCAL ";
1277 break;
1278 case 3: detector = "BFCAL ";
1279 break;
1280 case 4: detector = "ST ";
1281 nhit = triggers_enabled[ii].gtp.st_nhit;
1282 pattern = triggers_enabled[ii].gtp.st_pattern;
1283 break;
1284 case 8: detector = "PS ";
1285 nhit = triggers_enabled[ii].gtp.ps_nhit;
1286 pattern = triggers_enabled[ii].gtp.ps_pattern;
1287 break;
1288 case 16: detector = "TAGH ";
1289 nhit = triggers_enabled[ii].gtp.tagh_nhit;
1290 pattern = triggers_enabled[ii].gtp.tagh_pattern;
1291 break;
1292 case 32: detector = "TOF ";
1293 nhit = triggers_enabled[ii].gtp.tof_nhit;
1294 pattern = triggers_enabled[ii].gtp.tof_pattern;
1295 break;
1296 default: detector = "NONE ";
1297 cout << " Unknown detector ===== " << triggers_enabled[ii].type << endl;
1298 break;
1299 }
1300
1301 cout << detector << setw(6) <<
1302 triggers_enabled[ii].gtp.fcal << setw(9) <<
1303 triggers_enabled[ii].gtp.bcal << setw(11) <<
1304 triggers_enabled[ii].gtp.en_thr << setw(6) <<
1305 nhit << setw(9) <<
1306 triggers_enabled[ii].bit << setw(12) <<
1307 triggers_enabled[ii].gtp.fcal_min << setw(14) <<
1308 triggers_enabled[ii].gtp.fcal_max << setw(10) <<
1309 triggers_enabled[ii].gtp.bcal_min << setw(14) <<
1310 triggers_enabled[ii].gtp.bcal_max << setw(8) <<
1311 hex << uppercase << "0x" << pattern << nouppercase << dec << endl;
1312
1313 }
1314
1315 cout << endl << endl;
1316
1317}
1318
1319
1320
1321int DL1MCTrigger_factory::FindTriggers(DL1MCTrigger *trigger){
1322
1323 int trig_found = 0;
1324
1325 // Main production trigger
1326 for(unsigned int ii = 0; ii < triggers_enabled.size(); ii++){
1327
1328 if(triggers_enabled[ii].bit == 0){ // Main production trigger found
1329
1330 int gtp_energy = 0;
1331 int bcal_energy = 0;
1332
1333 for(unsigned int samp = 0; samp < sample; samp++){
1334
1335 int bcal_samp = samp - BCAL_OFFSET;
1336
1337 if(bcal_samp < 0){
1338 bcal_energy = 0;
1339 } else if(bcal_samp >= sample){
1340 bcal_energy = 0;
1341 } else{
1342 bcal_energy = bcal_gtp[bcal_samp];
1343 }
1344
1345
1346 gtp_energy = triggers_enabled[ii].gtp.fcal*fcal_gtp[samp] +
1347 triggers_enabled[ii].gtp.bcal*bcal_energy;
1348
1349 if(gtp_energy >= triggers_enabled[ii].gtp.en_thr){
1350
1351 if(triggers_enabled[ii].gtp.fcal_min > 0) { // FCAL > 0
1352 if(fcal_gtp[samp] > triggers_enabled[ii].gtp.fcal_min){
1353 trigger->trig_mask = (trigger->trig_mask | 0x1);
1354 trigger->trig_time[0] = samp - 25;
1355 trig_found++;
1356 }
1357 } else {
1358
1359 trigger->trig_mask = (trigger->trig_mask | 0x1);
1360 trigger->trig_time[0] = samp - 25;
1361 trig_found++;
1362 }
1363
1364 break;
1365
1366 } // Check energy threshold
1367 }
1368 } // Trigger Bit 0
1369
1370
1371 if(triggers_enabled[ii].bit == 2){ // BCAL trigger found
1372
1373 int gtp_energy = 0;
1374 int bcal_energy = 0;
1375
1376 for(unsigned int samp = 0; samp < sample; samp++){
1377
1378 int bcal_samp = samp - BCAL_OFFSET;
1379
1380 if(bcal_samp < 0){
1381 bcal_energy = 0;
1382 } else if(bcal_samp >= sample){
1383 bcal_energy = 0;
1384 } else{
1385 bcal_energy = bcal_gtp[bcal_samp];
1386 }
1387
1388 gtp_energy = triggers_enabled[ii].gtp.bcal*bcal_energy;
1389
1390 if(gtp_energy >= triggers_enabled[ii].gtp.en_thr){
1391
1392 trigger->trig_mask = (trigger->trig_mask | 0x4);
1393 trigger->trig_time[2] = samp - 25;
1394 trig_found++;
1395
1396 break;
1397
1398 } // Energy threshold
1399 }
1400 } // Trigger Bit 3
1401
1402 } // Loop over triggers found in the config file
1403
1404 return trig_found;
1405
1406}
1407
1408
1409// Fill fcal calibration tables similar to FCALHit factory
1410void DL1MCTrigger_factory::LoadFCALConst(fcal_constants_t &table, const vector<double> &fcal_const_ch,
1411 const DFCALGeometry &fcalGeom){
1412 for (int ch = 0; ch < static_cast<int>(fcal_const_ch.size()); ch++) {
1413 int row = fcalGeom.row(ch);
1414 int col = fcalGeom.column(ch);
1415 table[row][col] = fcal_const_ch[ch];
1416 }
1417
1418}
1419
1420void DL1MCTrigger_factory::Digitize(double adc_amp[sample], int adc_count[sample]){
1421
1422 for(int samp = 0; samp < sample; samp++ ){
1423
1424 adc_count[samp] += (int)(adc_amp[samp] + TRIG_BASELINE + 0.5);
1425
1426 if(adc_count[samp] > max_adc_bins)
1427 adc_count[samp] = max_adc_bins;
1428
1429 }
1430}
1431
1432
1433void DL1MCTrigger_factory::AddBaseline(double adc_amp[sample], double pedestal, DRandom2 &gDRandom){
1434
1435 double pedestal_correct = pedestal - TRIG_BASELINE;
1436
1437 for(int samp = 0; samp < sample; samp++ ){
1438 double tmp = gDRandom.Gaus(pedestal_correct, pedestal_sigma);
1439 adc_amp[samp] += tmp;
1440 // cout << " " << tmp;
1441 }
1442
1443 if(debug){
1444 cout << endl;
1445 cout << " Corrected pedestals = " << pedestal_correct << " " << adc_amp[sample - 2]
1446 << " " << pedestal_sigma << endl;
1447 }
1448
1449}
1450
1451
1452
1453void DL1MCTrigger_factory::GetSeeds(JEventLoop *loop, uint64_t eventnumber, UInt_t &seed1, UInt_t &seed2, UInt_t &seed3){
1454
1455 // Use seeds similar to mcsmear
1456
1457 JEvent& event = loop->GetJEvent();
1458
1459 JEventSource *source = event.GetJEventSource();
1460
1461 DEventSourceHDDM *hddm_source = dynamic_cast<DEventSourceHDDM*>(source);
1462
1463 if (!hddm_source) {
1464
1465 cerr << "DL1MCTrigger_factory: This program MUST be used with an HDDM file as input!" << endl;
1466 cerr << " Default seeds will be used for the random generator " << endl;
1467 seed1 = 259921049 + eventnumber;
1468 seed2 = 442249570 + eventnumber;
1469 seed3 = 709975946 + eventnumber;
1470 } else {
1471
1472 hddm_s::HDDM *record = (hddm_s::HDDM*)event.GetRef();
1473 if (!record){
1474 seed1 = 259921049 + eventnumber;
1475 seed2 = 442249570 + eventnumber;
1476 seed3 = 709975946 + eventnumber;
1477 } else {
1478
1479
1480 hddm_s::ReactionList::iterator reiter = record->getReactions().begin();
1481
1482 hddm_s::Random my_rand = reiter->getRandom();
1483
1484 // Copy seeds from event record to local variables
1485 seed1 = my_rand.getSeed1();
1486 seed2 = my_rand.getSeed2();
1487 seed3 = my_rand.getSeed3();
1488
1489 // If no seeds are stored in the hddm file, generate them in the same way
1490 // as in mcsmear
1491 if ((seed1 == 0) || (seed2 == 0) || (seed3 == 0)){
1492 uint64_t eventNo = record->getPhysicsEvent().getEventNo();
1493 seed1 = 259921049 + eventNo;
1494 seed2 = 442249570 + eventNo;
1495 seed3 = 709975946 + eventNo;
1496
1497 }
1498
1499 } // Record doesn't exist
1500 } // Not an HDDM file
1501}

/w/halld-scifs17exp/halld2/home/sdobbs/Software/jana/jana_0.8.2/Linux_CentOS7.7-x86_64-gcc4.8.5/include/JANA/JEventLoop.h

1// $Id: JEventLoop.h 1763 2006-05-10 14:29:25Z davidl $
2//
3// File: JEventLoop.h
4// Created: Wed Jun 8 12:30:51 EDT 2005
5// Creator: davidl (on Darwin wire129.jlab.org 7.8.0 powerpc)
6//
7
8#ifndef _JEventLoop_
9#define _JEventLoop_
10
11#include <sys/time.h>
12
13#include <vector>
14#include <list>
15#include <string>
16#include <utility>
17#include <typeinfo>
18#include <string.h>
19#include <map>
20#include <utility>
21using std::vector;
22using std::list;
23using std::string;
24using std::type_info;
25
26#include <JANA/jerror.h>
27#include <JANA/JObject.h>
28#include <JANA/JException.h>
29#include <JANA/JEvent.h>
30#include <JANA/JThread.h>
31#include <JANA/JFactory_base.h>
32#include <JANA/JCalibration.h>
33#include <JANA/JGeometry.h>
34#include <JANA/JResourceManager.h>
35#include <JANA/JStreamLog.h>
36
37// The following is here just so we can use ROOT's THtml class to generate documentation.
38#include "cint.h"
39
40
41// Place everything in JANA namespace
42namespace jana{
43
44
45template<class T> class JFactory;
46class JApplication;
47class JEventProcessor;
48
49
50class JEventLoop{
51 public:
52
53 friend class JApplication;
54
55 enum data_source_t{
56 DATA_NOT_AVAILABLE = 1,
57 DATA_FROM_CACHE,
58 DATA_FROM_SOURCE,
59 DATA_FROM_FACTORY
60 };
61
62 typedef struct{
63 string caller_name;
64 string caller_tag;
65 string callee_name;
66 string callee_tag;
67 double start_time;
68 double end_time;
69 data_source_t data_source;
70 }call_stack_t;
71
72 typedef struct{
73 const char* factory_name;
74 string tag;
75 const char* filename;
76 int line;
77 }error_call_stack_t;
78
79 JEventLoop(JApplication *app); ///< Constructor
80 virtual ~JEventLoop(); ////< Destructor
81 virtual const char* className(void){return static_className();}
82 static const char* static_className(void){return "JEventLoop";}
83
84 JApplication* GetJApplication(void) const {return app;} ///< Get pointer to the JApplication object
85 void RefreshProcessorListFromJApplication(void); ///< Re-copy the list of JEventProcessors from JApplication
86 virtual jerror_t AddFactory(JFactory_base* factory); ///< Add a factory
87 jerror_t RemoveFactory(JFactory_base* factory); ///< Remove a factory
88 JFactory_base* GetFactory(const string data_name, const char *tag="", bool allow_deftag=true); ///< Get a specific factory pointer
89 vector<JFactory_base*> GetFactories(void) const {return factories;} ///< Get all factory pointers
90 void GetFactoryNames(vector<string> &factorynames); ///< Get names of all factories in name:tag format
91 void GetFactoryNames(map<string,string> &factorynames); ///< Get names of all factories in map with key=name, value=tag
92 map<string,string> GetDefaultTags(void) const {return default_tags;}
93 jerror_t ClearFactories(void); ///< Reset all factories in preparation for next event.
94 jerror_t PrintFactories(int sparsify=0); ///< Print a list of all factories.
95 jerror_t Print(const string data_name, const char *tag=""); ///< Print the data of the given type
96
97 JCalibration* GetJCalibration();
98 template<class T> bool GetCalib(string namepath, map<string,T> &vals);
99 template<class T> bool GetCalib(string namepath, vector<T> &vals);
100 template<class T> bool GetCalib(string namepath, T &val);
101
102 JGeometry* GetJGeometry();
103 template<class T> bool GetGeom(string namepath, map<string,T> &vals);
104 template<class T> bool GetGeom(string namepath, T &val);
105
106 JResourceManager* GetJResourceManager(void);
107 string GetResource(string namepath);
108 template<class T> bool GetResource(string namepath, T vals, int event_number=0);
109
110 void Initialize(void); ///< Do initializations just before event processing starts
111 jerror_t Loop(void); ///< Loop over events
112 jerror_t OneEvent(uint64_t event_number); ///< Process a specific single event (if source supports it)
113 jerror_t OneEvent(void); ///< Process a single event
114 inline void Pause(void){pause = 1;} ///< Pause event processing
115 inline void Resume(void){pause = 0;} ///< Resume event processing
116 inline void Quit(void){quit = 1;} ///< Clean up and exit the event loop
117 inline bool GetQuit(void) const {return quit;}
118 void QuitProgram(void);
119
120 // Support for random access of events
121 bool HasRandomAccess(void);
122 void AddToEventQueue(uint64_t event_number){ next_events_to_process.push_back(event_number); }
123 void AddToEventQueue(list<uint64_t> &event_numbers) { next_events_to_process.insert(next_events_to_process.end(), event_numbers.begin(), event_numbers.end()); }
124 list<uint64_t> GetEventQueue(void){ return next_events_to_process; }
125 void ClearEventQueue(void){ next_events_to_process.clear(); }
126
127 template<class T> JFactory<T>* GetSingle(const T* &t, const char *tag="", bool exception_if_not_one=true); ///< Get pointer to first data object from (source or factory).
128 template<class T> JFactory<T>* Get(vector<const T*> &t, const char *tag="", bool allow_deftag=true); ///< Get data object pointers from (source or factory)
129 template<class T> JFactory<T>* GetFromFactory(vector<const T*> &t, const char *tag="", data_source_t &data_source=null_data_source, bool allow_deftag=true); ///< Get data object pointers from factory
130 template<class T> jerror_t GetFromSource(vector<const T*> &t, JFactory_base *factory=NULL__null); ///< Get data object pointers from source.
131 inline JEvent& GetJEvent(void){return event;} ///< Get pointer to the current JEvent object.
132 inline void SetJEvent(JEvent *event){this->event = *event;} ///< Set the JEvent pointer.
133 inline void SetAutoFree(int auto_free){this->auto_free = auto_free;} ///< Set the Auto-Free flag on/off
134 inline pthread_t GetPThreadID(void) const {return pthread_id;} ///< Get the pthread of the thread to which this JEventLoop belongs
135 double GetInstantaneousRate(void) const {return rate_instantaneous;} ///< Get the current event processing rate
136 double GetIntegratedRate(void) const {return rate_integrated;} ///< Get the current event processing rate
137 double GetLastEventProcessingTime(void) const {return delta_time_single;}
138 unsigned int GetNevents(void) const {return Nevents;}
139
140 inline bool CheckEventBoundary(uint64_t event_numberA, uint64_t event_numberB);
141
142 inline bool GetCallStackRecordingStatus(void){ return record_call_stack; }
143 inline void DisableCallStackRecording(void){ record_call_stack = false; }
144 inline void EnableCallStackRecording(void){ record_call_stack = true; }
145 inline void CallStackStart(JEventLoop::call_stack_t &cs, const string &caller_name, const string &caller_tag, const string callee_name, const string callee_tag);
146 inline void CallStackEnd(JEventLoop::call_stack_t &cs);
147 inline vector<call_stack_t> GetCallStack(void){return call_stack;} ///< Get the current factory call stack
148 inline void AddToCallStack(call_stack_t &cs){if(record_call_stack) call_stack.push_back(cs);} ///< Add specified item to call stack record but only if record_call_stack is true
149 inline void AddToErrorCallStack(error_call_stack_t &cs){error_call_stack.push_back(cs);} ///< Add layer to the factory call stack
150 inline vector<error_call_stack_t> GetErrorCallStack(void){return error_call_stack;} ///< Get the current factory error call stack
151 void PrintErrorCallStack(void); ///< Print the current factory call stack
152
153 const JObject* FindByID(JObject::oid_t id); ///< Find a data object by its identifier.
154 template<class T> const T* FindByID(JObject::oid_t id); ///< Find a data object by its type and identifier
155 JFactory_base* FindOwner(const JObject *t); ///< Find the factory that owns a data object by pointer
156 JFactory_base* FindOwner(JObject::oid_t id); ///< Find a factory that owns a data object by identifier
157
158 // User defined references
159 template<class T> void SetRef(T *t); ///< Add a user reference to this JEventLoop (must be a pointer)
160 template<class T> T* GetRef(void); ///< Get a user-defined reference of a specific type
161 template<class T> vector<T*> GetRefsT(void); ///< Get all user-defined refrences of a specicif type
162 vector<pair<const char*, void*> > GetRefs(void){ return user_refs; } ///< Get copy of full list of user-defined references
163 template<class T> void RemoveRef(T *t); ///< Remove user reference from list
164
165 // Convenience methods wrapping JEvent methods of same name
166 uint64_t GetStatus(void){return event.GetStatus();}
167 bool GetStatusBit(uint32_t bit){return event.GetStatusBit(bit);}
168 bool SetStatusBit(uint32_t bit, bool val=true){return event.SetStatusBit(bit, val);}
169 bool ClearStatusBit(uint32_t bit){return event.ClearStatusBit(bit);}
170 void ClearStatus(void){event.ClearStatus();}
171 void SetStatusBitDescription(uint32_t bit, string description){event.SetStatusBitDescription(bit, description);}
172 string GetStatusBitDescription(uint32_t bit){return event.GetStatusBitDescription(bit);}
173 void GetStatusBitDescriptions(map<uint32_t, string> &status_bit_descriptions){return event.GetStatusBitDescriptions(status_bit_descriptions);}
174 string StatusWordToString(void);
175
176 private:
177 JEvent event;
178 vector<JFactory_base*> factories;
179 vector<JEventProcessor*> processors;
180 vector<error_call_stack_t> error_call_stack;
181 vector<call_stack_t> call_stack;
182 JApplication *app;
183 JThread *jthread;
184 bool initialized;
185 bool print_parameters_called;
186 int pause;
187 int quit;
188 int auto_free;
189 pthread_t pthread_id;
190 map<string, string> default_tags;
191 vector<pair<string,string> > auto_activated_factories;
192 bool record_call_stack;
193 string caller_name;
194 string caller_tag;
195 vector<uint64_t> event_boundaries;
196 int32_t event_boundaries_run; ///< Run number boundaries were retrieved from (possbily 0)
197 list<uint64_t> next_events_to_process;
198
199 uint64_t Nevents; ///< Total events processed (this thread)
200 uint64_t Nevents_rate; ///< Num. events accumulated for "instantaneous" rate
201 double delta_time_single; ///< Time spent processing last event
202 double delta_time_rate; ///< Integrated time accumulated "instantaneous" rate (partial number of events)
203 double delta_time; ///< Total time spent processing events (this thread)
204 double rate_instantaneous; ///< Latest instantaneous rate
205 double rate_integrated; ///< Rate integrated over all events
206
207 static data_source_t null_data_source;
208
209 vector<pair<const char*, void*> > user_refs;
210};
211
212
213// The following is here just so we can use ROOT's THtml class to generate documentation.
214#ifdef G__DICTIONARY
215typedef JEventLoop::call_stack_t call_stack_t;
216typedef JEventLoop::error_call_stack_t error_call_stack_t;
217#endif
218
219#if !defined(__CINT__) && !defined(__CLING__)
220
221//-------------
222// GetSingle
223//-------------
224template<class T>
225JFactory<T>* JEventLoop::GetSingle(const T* &t, const char *tag, bool exception_if_not_one)
226{
227 /// This is a convenience method that can be used to get a pointer to the single
228 /// object of type T from the specified factory. It simply calls the Get(vector<...>) method
229 /// and copies the first pointer into "t" (or NULL if something other than 1 object is returned).
230 ///
231 /// This is intended to address the common situation in which there is an interest
232 /// in the event if and only if there is exactly 1 object of type T. If the event
233 /// has no objects of that type or more than 1 object of that type (for the specified
234 /// factory) then an exception of type "unsigned long" is thrown with the value
235 /// being the number of objects of type T. You can supress the exception by setting
236 /// exception_if_not_one to false. In that case, you will have to check if t==NULL to
237 /// know if the call succeeded.
238 vector<const T*> v;
239 JFactory<T> *fac = Get(v, tag);
240
241 if(v.size()!=1){
242 t = NULL__null;
243 if(exception_if_not_one) throw v.size();
244 }
245
246 t = v[0];
247
248 return fac;
249}
250
251//-------------
252// Get
253//-------------
254template<class T>
255JFactory<T>* JEventLoop::Get(vector<const T*> &t, const char *tag, bool allow_deftag)
256{
257 /// Retrieve or generate the array of objects of
258 /// type T for the curent event being processed
259 /// by this thread.
260 ///
261 /// By default, preference is given to reading the
262 /// objects from the data source(e.g. file) before generating
263 /// them in the factory. A flag exists in the factory
264 /// however to change this so that the factory is
265 /// given preference.
266 ///
267 /// Note that regardless of the setting of this flag,
268 /// the data are only either read in or generated once.
269 /// Ownership of the objects will always be with the
270 /// factory so subsequent calls will always return pointers to
271 /// the same data.
272 ///
273 /// If the factory is called on to generate the data,
274 /// it is done by calling the factory's Get() method
275 /// which, in turn, calls the evnt() method.
276 ///
277 /// First, we just call the GetFromFactory() method.
278 /// It will make the initial decision as to whether
279 /// it should look in the source first or not. If
280 /// it returns NULL, then the factory couldn't be
281 /// found so we automatically try the file.
282 ///
283 /// Note that if no factory exists to hold the objects
284 /// from the file, one can be created automatically
285 /// providing the <i>JANA:AUTOFACTORYCREATE</i>
286 /// configuration parameter is set.
287
288 // Check if a tag was specified for this data type to use for the
289 // default.
290 const char *mytag = tag
1.1
'tag' is not equal to NULL
1.1
'tag' is not equal to NULL
1.1
'tag' is not equal to NULL
==NULL__null ? "":tag; // protection against NULL tags
2
'?' condition is false
291 if(strlen(mytag)==0 && allow_deftag
2.1
'allow_deftag' is true
2.1
'allow_deftag' is true
2.1
'allow_deftag' is true
){
3
Taking true branch
292 map<string, string>::const_iterator iter = default_tags.find(T::static_className());
293 if(iter!=default_tags.end())tag = iter->second.c_str();
4
Assuming the condition is true
5
Taking true branch
6
Value assigned to 'tag'
294 }
295
296
297 // If we are trying to keep track of the call stack then we
298 // need to add a new call_stack_t object to the the list
299 // and initialize it with the start time and caller/callee
300 // info.
301 call_stack_t cs;
302
303 // Optionally record starting info of call stack entry
304 if(record_call_stack) CallStackStart(cs, caller_name, caller_tag, T::static_className(), tag);
7
Assuming field 'record_call_stack' is false
8
Taking false branch
305
306 // Get the data (or at least try to)
307 JFactory<T>* factory=NULL__null;
308 try{
309 factory = GetFromFactory(t, tag, cs.data_source, allow_deftag);
9
Passing value via 2nd parameter 'tag'
10
Calling 'JEventLoop::GetFromFactory'
310 if(!factory){
311 // No factory exists for this type and tag. It's possible
312 // that the source may be able to provide the objects
313 // but it will need a place to put them. We can create a
314 // dumb JFactory just to hold the data in case the source
315 // can provide the objects. Before we do though, make sure
316 // the user condones this via the presence of the
317 // "JANA:AUTOFACTORYCREATE" config parameter.
318 string p;
319 try{
320 gPARMS->GetParameter("JANA:AUTOFACTORYCREATE", p);
321 }catch(...){}
322 if(p.size()==0){
323 jout<<std::endl;
324 _DBG__std::cerr<<"/w/halld-scifs17exp/halld2/home/sdobbs/Software/jana/jana_0.8.2/Linux_CentOS7.7-x86_64-gcc4.8.5/include/JANA/JEventLoop.h"
<<":"<<324<<std::endl
;
325 jout<<"No factory of type \""<<T::static_className()<<"\" with tag \""<<tag<<"\" exists."<<std::endl;
326 jout<<"If you are reading objects from a file, I can auto-create a factory"<<std::endl;
327 jout<<"of the appropriate type to hold the objects, but this feature is turned"<<std::endl;
328 jout<<"off by default. To turn it on, set the \"JANA:AUTOFACTORYCREATE\""<<std::endl;
329 jout<<"configuration parameter. This can usually be done by passing the"<<std::endl;
330 jout<<"following argument to the program from the command line:"<<std::endl;
331 jout<<std::endl;
332 jout<<" -PJANA:AUTOFACTORYCREATE=1"<<std::endl;
333 jout<<std::endl;
334 jout<<"Note that since the most commonly expected occurance of this situation."<<std::endl;
335 jout<<"is an error, the program will now throw an exception so that the factory."<<std::endl;
336 jout<<"call stack can be printed."<<std::endl;
337 jout<<std::endl;
338 throw exception();
339 }else{
340 AddFactory(new JFactory<T>(tag));
341 jout<<__FILE__"/w/halld-scifs17exp/halld2/home/sdobbs/Software/jana/jana_0.8.2/Linux_CentOS7.7-x86_64-gcc4.8.5/include/JANA/JEventLoop.h"<<":"<<__LINE__341<<" Auto-created "<<T::static_className()<<":"<<tag<<" factory"<<std::endl;
342
343 // Now try once more. The GetFromFactory method will call
344 // GetFromSource since it's empty.
345 factory = GetFromFactory(t, tag, cs.data_source, allow_deftag);
346 }
347 }
348 }catch(exception &e){
349 // Uh-oh, an exception was thrown. Add us to the call stack
350 // and re-throw the exception
351 error_call_stack_t ecs;
352 ecs.factory_name = T::static_className();
353 ecs.tag = tag;
354 ecs.filename = NULL__null;
355 error_call_stack.push_back(ecs);
356 throw e;
357 }
358
359 // If recording the call stack, update the end_time field and add to stack
360 if(record_call_stack) CallStackEnd(cs);
361
362 return factory;
363}
364
365//-------------
366// GetFromFactory
367//-------------
368template<class T>
369JFactory<T>* JEventLoop::GetFromFactory(vector<const T*> &t, const char *tag, data_source_t &data_source, bool allow_deftag)
370{
371 // We need to find the factory providing data type T with
372 // tag given by "tag".
373 vector<JFactory_base*>::iterator iter=factories.begin();
374 JFactory<T> *factory = NULL__null;
375 string className(T::static_className());
376
377 // Check if a tag was specified for this data type to use for the
378 // default.
379 const char *mytag = tag==NULL__null ? "":tag; // protection against NULL tags
11
Assuming 'tag' is equal to NULL
12
Assuming pointer value is null
13
'?' condition is true
380 if(strlen(mytag)==0 && allow_deftag
13.1
'allow_deftag' is true
13.1
'allow_deftag' is true
13.1
'allow_deftag' is true
){
14
Taking true branch
381 map<string, string>::const_iterator iter = default_tags.find(className);
382 if(iter!=default_tags.end())tag = iter->second.c_str();
15
Assuming the condition is false
16
Taking false branch
383 }
384
385 for(; iter!=factories.end(); iter++){
17
Calling 'operator!=<jana::JFactory_base **, std::vector<jana::JFactory_base *>>'
20
Returning from 'operator!=<jana::JFactory_base **, std::vector<jana::JFactory_base *>>'
21
Loop condition is true. Entering loop body
386 // It turns out a long standing bug in g++ makes dynamic_cast return
387 // zero improperly when used on objects created on one side of
388 // a dynamically shared object (DSO) and the cast occurs on the
389 // other side. I saw bug reports ranging from 2001 to 2004. I saw
390 // saw it first-hand on LinuxEL4 using g++ 3.4.5. This is too bad
391 // since it is much more elegant (and safe) to use dynamic_cast.
392 // To avoid this problem which can occur with plugins, we check
393 // the name of the data classes are the same. (sigh)
394 //factory = dynamic_cast<JFactory<T> *>(*iter);
395 if(className == (*iter)->GetDataClassName())factory = (JFactory<T>*)(*iter);
22
Taking true branch
396 if(factory == NULL__null)continue;
23
Assuming 'factory' is not equal to NULL
24
Taking false branch
397 const char *factag = factory->Tag()==NULL__null ? "":factory->Tag();
25
Assuming the condition is true
26
'?' condition is true
398 if(!strcmp(factag, tag)){
27
Null pointer passed to 2nd parameter expecting 'nonnull'
399 break;
400 }else{
401 factory=NULL__null;
402 }
403 }
404
405 // If factory not found, just return now
406 if(!factory){
407 data_source = DATA_NOT_AVAILABLE;
408 return NULL__null;
409 }
410
411 // OK, we found the factory. If the evnt() routine has already
412 // been called, then just call the factory's Get() routine
413 // to return a copy of the existing data
414 if(factory->evnt_was_called()){
415 factory->CopyFrom(t);
416 data_source = DATA_FROM_CACHE;
417 return factory;
418 }
419
420 // Next option is to get the objects from the data source
421 if(factory->GetCheckSourceFirst()){
422 // If the object type/tag is found in the source, it
423 // will return NOERROR, even if there are zero instances
424 // of it. If it is not available in the source then it
425 // will return OBJECT_NOT_AVAILABLE.
426
427 jerror_t err = GetFromSource(t, factory);
428 if(err == NOERROR){
429 // A return value of NOERROR means the source had the objects
430 // even if there were zero of them.(If the source had no
431 // information about the objects OBJECT_NOT_AVAILABLE would
432 // have been returned.)
433 // The GetFromSource() call will eventually lead to a call to
434 // the GetObjects() method of the concrete class derived
435 // from JEventSource. That routine should copy the object
436 // pointers into the factory using the factory's CopyTo()
437 // method which also sets the evnt_called flag for the factory.
438 // Note also that the "t" vector is then filled with a call
439 // to the factory's CopyFrom() method in JEvent::GetObjects().
440 // All we need to do now is just set the factory pointers in
441 // the newly generated JObjects and return the factory pointer.
442
443 factory->SetFactoryPointers();
444 data_source = DATA_FROM_SOURCE;
445
446 return factory;
447 }
448 }
449
450 // OK. It looks like we have to have the factory make this.
451 // Get pointers to data from the factory.
452 factory->Get(t);
453 factory->SetFactoryPointers();
454 data_source = DATA_FROM_FACTORY;
455
456 return factory;
457}
458
459//-------------
460// GetFromSource
461//-------------
462template<class T>
463jerror_t JEventLoop::GetFromSource(vector<const T*> &t, JFactory_base *factory)
464{
465 /// This tries to get objects from the event source.
466 /// "factory" must be a valid pointer to a JFactory
467 /// object since that will take ownership of the objects
468 /// created by the source.
469 /// This should usually be called from JEventLoop::GetFromFactory
470 /// which is called from JEventLoop::Get. The latter will
471 /// create a dummy JFactory of the proper flavor and tag if
472 /// one does not already exist so if objects exist in the
473 /// file without a corresponding factory to create them, they
474 /// can still be used.
475 if(!factory)throw OBJECT_NOT_AVAILABLE;
476
477 return event.GetObjects(t, factory);
478}
479
480//-------------
481// CallStackStart
482//-------------
483inline void JEventLoop::CallStackStart(JEventLoop::call_stack_t &cs, const string &caller_name, const string &caller_tag, const string callee_name, const string callee_tag)
484{
485 /// This is used to fill initial info into a call_stack_t stucture
486 /// for recording the call stack. It should be matched with a call
487 /// to CallStackEnd. It is normally called from the Get() method
488 /// above, but may also be used by external actors to manipulate
489 /// the call stack (presumably for good and not evil).
490
491 struct itimerval tmr;
492 getitimer(ITIMER_PROFITIMER_PROF, &tmr);
493
494 cs.caller_name = this->caller_name;
495 cs.caller_tag = this->caller_tag;
496 this->caller_name = cs.callee_name = callee_name;
497 this->caller_tag = cs.callee_tag = callee_tag;
498 cs.start_time = tmr.it_value.tv_sec + tmr.it_value.tv_usec/1.0E6;
499}
500
501//-------------
502// CallStackEnd
503//-------------
504inline void JEventLoop::CallStackEnd(JEventLoop::call_stack_t &cs)
505{
506 /// Complete a call stack entry. This should be matched
507 /// with a previous call to CallStackStart which was
508 /// used to fill the cs structure.
509
510 struct itimerval tmr;
511 getitimer(ITIMER_PROFITIMER_PROF, &tmr);
512 cs.end_time = tmr.it_value.tv_sec + tmr.it_value.tv_usec/1.0E6;
513 caller_name = cs.caller_name;
514 caller_tag = cs.caller_tag;
515 call_stack.push_back(cs);
516}
517
518//-------------
519// CheckEventBoundary
520//-------------
521inline bool JEventLoop::CheckEventBoundary(uint64_t event_numberA, uint64_t event_numberB)
522{
523 /// Check whether the two event numbers span one or more boundaries
524 /// in the calibration/conditions database for the current run number.
525 /// Return true if they do and false if they don't. The first parameter
526 /// "event_numberA" is also checked if it lands on a boundary in which
527 /// case true is also returned. If event_numberB lands on a boundary,
528 /// but event_numberA does not, then false is returned.
529 ///
530 /// This method is not expected to be called by a user. It is, however called,
531 /// everytime a JFactory's Get() method is called.
532
533 // Make sure our copy of the boundaries is up to date
534 if(event.GetRunNumber()!=event_boundaries_run){
535 event_boundaries.clear(); // in case we can't get the JCalibration pointer
536 JCalibration *jcalib = GetJCalibration();
537 if(jcalib)jcalib->GetEventBoundaries(event_boundaries);
538 event_boundaries_run = event.GetRunNumber();
539 }
540
541 // Loop over boundaries
542 for(unsigned int i=0; i<event_boundaries.size(); i++){
543 uint64_t eb = event_boundaries[i];
544 if((eb - event_numberA)*(eb - event_numberB) < 0.0 || eb==event_numberA){ // think about it ....
545 // events span a boundary or is on a boundary. Return true
546 return true;
547 }
548 }
549
550 return false;
551}
552
553//-------------
554// FindByID
555//-------------
556template<class T>
557const T* JEventLoop::FindByID(JObject::oid_t id)
558{
559 /// This is a templated method that can be used in place
560 /// of the non-templated FindByID(oid_t) method if one knows
561 /// the class of the object with the specified id.
562 /// This method is faster than calling the non-templated
563 /// FindByID and dynamic_cast-ing the JObject since
564 /// this will only search the objects of factories that
565 /// produce the desired data type.
566 /// This method will cast the JObject pointer to one
567 /// of the specified type. To use this method,
568 /// a type is specified in the call as follows:
569 ///
570 /// const DMyType *t = loop->FindByID<DMyType>(id);
571
572 // Loop over factories looking for ones that provide
573 // specified data type.
574 for(unsigned int i=0; i<factories.size(); i++){
575 if(factories[i]->GetDataClassName() != T::static_className())continue;
576
577 // This factory provides data of type T. Search it for
578 // the object with the specified id.
579 const JObject *my_obj = factories[i]->GetByID(id);
580 if(my_obj)return dynamic_cast<const T*>(my_obj);
581 }
582
583 return NULL__null;
584}
585
586//-------------
587// GetCalib (map)
588//-------------
589template<class T>
590bool JEventLoop::GetCalib(string namepath, map<string,T> &vals)
591{
592 /// Get the JCalibration object from JApplication for the run number of
593 /// the current event and call its Get() method to get the constants.
594
595 // Note that we could do this by making "vals" a generic type T thus, combining
596 // this with the vector version below. However, doing this explicitly will make
597 // it easier for the user to understand how to call us.
598
599 vals.clear();
600
601 JCalibration *calib = GetJCalibration();
602 if(!calib){
603 _DBG_std::cerr<<"/w/halld-scifs17exp/halld2/home/sdobbs/Software/jana/jana_0.8.2/Linux_CentOS7.7-x86_64-gcc4.8.5/include/JANA/JEventLoop.h"
<<":"<<603<<" "
<<"Unable to get JCalibration object for run "<<event.GetRunNumber()<<std::endl;
604 return true;
605 }
606
607 return calib->Get(namepath, vals, event.GetEventNumber());
608}
609
610//-------------
611// GetCalib (vector)
612//-------------
613template<class T> bool JEventLoop::GetCalib(string namepath, vector<T> &vals)
614{
615 /// Get the JCalibration object from JApplication for the run number of
616 /// the current event and call its Get() method to get the constants.
617
618 vals.clear();
619
620 JCalibration *calib = GetJCalibration();
621 if(!calib){
622 _DBG_std::cerr<<"/w/halld-scifs17exp/halld2/home/sdobbs/Software/jana/jana_0.8.2/Linux_CentOS7.7-x86_64-gcc4.8.5/include/JANA/JEventLoop.h"
<<":"<<622<<" "
<<"Unable to get JCalibration object for run "<<event.GetRunNumber()<<std::endl;
623 return true;
624 }
625
626 return calib->Get(namepath, vals, event.GetEventNumber());
627}
628
629//-------------
630// GetCalib (single)
631//-------------
632template<class T> bool JEventLoop::GetCalib(string namepath, T &val)
633{
634 /// This is a convenience method for getting a single entry. It
635 /// simply calls the vector version and returns the first entry.
636 /// It returns true if the vector version returns true AND there
637 /// is at least one entry in the vector. No check is made for there
638 /// there being more than one entry in the vector.
639
640 vector<T> vals;
641 bool ret = GetCalib(namepath, vals);
642 if(vals.empty()) return true;
643 val = vals[0];
644
645 return ret;
646}
647
648//-------------
649// GetGeom (map)
650//-------------
651template<class T>
652bool JEventLoop::GetGeom(string namepath, map<string,T> &vals)
653{
654 /// Get the JGeometry object from JApplication for the run number of
655 /// the current event and call its Get() method to get the constants.
656
657 // Note that we could do this by making "vals" a generic type T thus, combining
658 // this with the vector version below. However, doing this explicitly will make
659 // it easier for the user to understand how to call us.
660
661 vals.clear();
662
663 JGeometry *geom = GetJGeometry();
664 if(!geom){
665 _DBG_std::cerr<<"/w/halld-scifs17exp/halld2/home/sdobbs/Software/jana/jana_0.8.2/Linux_CentOS7.7-x86_64-gcc4.8.5/include/JANA/JEventLoop.h"
<<":"<<665<<" "
<<"Unable to get JGeometry object for run "<<event.GetRunNumber()<<std::endl;
666 return true;
667 }
668
669 return geom->Get(namepath, vals);
670}
671
672//-------------
673// GetGeom (atomic)
674//-------------
675template<class T> bool JEventLoop::GetGeom(string namepath, T &val)
676{
677 /// Get the JCalibration object from JApplication for the run number of
678 /// the current event and call its Get() method to get the constants.
679
680 JGeometry *geom = GetJGeometry();
681 if(!geom){
682 _DBG_std::cerr<<"/w/halld-scifs17exp/halld2/home/sdobbs/Software/jana/jana_0.8.2/Linux_CentOS7.7-x86_64-gcc4.8.5/include/JANA/JEventLoop.h"
<<":"<<682<<" "
<<"Unable to get JGeometry object for run "<<event.GetRunNumber()<<std::endl;
683 return true;
684 }
685
686 return geom->Get(namepath, val);
687}
688
689//-------------
690// SetRef
691//-------------
692template<class T>
693void JEventLoop::SetRef(T *t)
694{
695 pair<const char*, void*> p(typeid(T).name(), (void*)t);
696 user_refs.push_back(p);
697}
698
699//-------------
700// GetResource
701//-------------
702template<class T> bool JEventLoop::GetResource(string namepath, T vals, int event_number)
703{
704 JResourceManager *resource_manager = GetJResourceManager();
705 if(!resource_manager){
706 string mess = string("Unable to get the JResourceManager object (namepath=\"")+namepath+"\")";
707 throw JException(mess);
708 }
709
710 return resource_manager->Get(namepath, vals, event_number);
711}
712
713//-------------
714// GetRef
715//-------------
716template<class T>
717T* JEventLoop::GetRef(void)
718{
719 /// Get a user-defined reference (a pointer)
720 for(unsigned int i=0; i<user_refs.size(); i++){
721 if(user_refs[i].first == typeid(T).name()) return (T*)user_refs[i].second;
722 }
723
724 return NULL__null;
725}
726
727//-------------
728// GetRefsT
729//-------------
730template<class T>
731vector<T*> JEventLoop::GetRefsT(void)
732{
733 vector<T*> refs;
734 for(unsigned int i=0; i<user_refs.size(); i++){
735 if(user_refs[i].first == typeid(T).name()){
736 refs.push_back((T*)user_refs[i].second);
737 }
738 }
739
740 return refs;
741}
742
743//-------------
744// RemoveRef
745//-------------
746template<class T>
747void JEventLoop::RemoveRef(T *t)
748{
749 vector<pair<const char*, void*> >::iterator iter;
750 for(iter=user_refs.begin(); iter!= user_refs.end(); iter++){
751 if(iter->second == (void*)t){
752 user_refs.erase(iter);
753 return;
754 }
755 }
756 _DBG_std::cerr<<"/w/halld-scifs17exp/halld2/home/sdobbs/Software/jana/jana_0.8.2/Linux_CentOS7.7-x86_64-gcc4.8.5/include/JANA/JEventLoop.h"
<<":"<<756<<" "
<<" Attempt to remove user reference not in event loop!" << std::endl;
757}
758
759
760#endif //__CINT__ __CLING__
761
762} // Close JANA namespace
763
764
765
766#endif // _JEventLoop_
767

/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../include/c++/4.8.5/bits/stl_iterator.h

1// Iterators -*- C++ -*-
2
3// Copyright (C) 2001-2013 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/*
26 *
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
29 *
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
37 *
38 *
39 * Copyright (c) 1996-1998
40 * Silicon Graphics Computer Systems, Inc.
41 *
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
49 */
50
51/** @file bits/stl_iterator.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{iterator}
54 *
55 * This file implements reverse_iterator, back_insert_iterator,
56 * front_insert_iterator, insert_iterator, __normal_iterator, and their
57 * supporting functions and overloaded operators.
58 */
59
60#ifndef _STL_ITERATOR_H1
61#define _STL_ITERATOR_H1 1
62
63#include <bits/cpp_type_traits.h>
64#include <ext/type_traits.h>
65#include <bits/move.h>
66
67namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default")))
68{
69_GLIBCXX_BEGIN_NAMESPACE_VERSION
70
71 /**
72 * @addtogroup iterators
73 * @{
74 */
75
76 // 24.4.1 Reverse iterators
77 /**
78 * Bidirectional and random access iterators have corresponding reverse
79 * %iterator adaptors that iterate through the data structure in the
80 * opposite direction. They have the same signatures as the corresponding
81 * iterators. The fundamental relation between a reverse %iterator and its
82 * corresponding %iterator @c i is established by the identity:
83 * @code
84 * &*(reverse_iterator(i)) == &*(i - 1)
85 * @endcode
86 *
87 * <em>This mapping is dictated by the fact that while there is always a
88 * pointer past the end of an array, there might not be a valid pointer
89 * before the beginning of an array.</em> [24.4.1]/1,2
90 *
91 * Reverse iterators can be tricky and surprising at first. Their
92 * semantics make sense, however, and the trickiness is a side effect of
93 * the requirement that the iterators must be safe.
94 */
95 template<typename _Iterator>
96 class reverse_iterator
97 : public iterator<typename iterator_traits<_Iterator>::iterator_category,
98 typename iterator_traits<_Iterator>::value_type,
99 typename iterator_traits<_Iterator>::difference_type,
100 typename iterator_traits<_Iterator>::pointer,
101 typename iterator_traits<_Iterator>::reference>
102 {
103 protected:
104 _Iterator current;
105
106 typedef iterator_traits<_Iterator> __traits_type;
107
108 public:
109 typedef _Iterator iterator_type;
110 typedef typename __traits_type::difference_type difference_type;
111 typedef typename __traits_type::pointer pointer;
112 typedef typename __traits_type::reference reference;
113
114 /**
115 * The default constructor value-initializes member @p current.
116 * If it is a pointer, that means it is zero-initialized.
117 */
118 // _GLIBCXX_RESOLVE_LIB_DEFECTS
119 // 235 No specification of default ctor for reverse_iterator
120 reverse_iterator() : current() { }
121
122 /**
123 * This %iterator will move in the opposite direction that @p x does.
124 */
125 explicit
126 reverse_iterator(iterator_type __x) : current(__x) { }
127
128 /**
129 * The copy constructor is normal.
130 */
131 reverse_iterator(const reverse_iterator& __x)
132 : current(__x.current) { }
133
134 /**
135 * A %reverse_iterator across other types can be copied if the
136 * underlying %iterator can be converted to the type of @c current.
137 */
138 template<typename _Iter>
139 reverse_iterator(const reverse_iterator<_Iter>& __x)
140 : current(__x.base()) { }
141
142 /**
143 * @return @c current, the %iterator used for underlying work.
144 */
145 iterator_type
146 base() const
147 { return current; }
148
149 /**
150 * @return A reference to the value at @c --current
151 *
152 * This requires that @c --current is dereferenceable.
153 *
154 * @warning This implementation requires that for an iterator of the
155 * underlying iterator type, @c x, a reference obtained by
156 * @c *x remains valid after @c x has been modified or
157 * destroyed. This is a bug: http://gcc.gnu.org/PR51823
158 */
159 reference
160 operator*() const
161 {
162 _Iterator __tmp = current;
163 return *--__tmp;
164 }
165
166 /**
167 * @return A pointer to the value at @c --current
168 *
169 * This requires that @c --current is dereferenceable.
170 */
171 pointer
172 operator->() const
173 { return &(operator*()); }
174
175 /**
176 * @return @c *this
177 *
178 * Decrements the underlying iterator.
179 */
180 reverse_iterator&
181 operator++()
182 {
183 --current;
184 return *this;
185 }
186
187 /**
188 * @return The original value of @c *this
189 *
190 * Decrements the underlying iterator.
191 */
192 reverse_iterator
193 operator++(int)
194 {
195 reverse_iterator __tmp = *this;
196 --current;
197 return __tmp;
198 }
199
200 /**
201 * @return @c *this
202 *
203 * Increments the underlying iterator.
204 */
205 reverse_iterator&
206 operator--()
207 {
208 ++current;
209 return *this;
210 }
211
212 /**
213 * @return A reverse_iterator with the previous value of @c *this
214 *
215 * Increments the underlying iterator.
216 */
217 reverse_iterator
218 operator--(int)
219 {
220 reverse_iterator __tmp = *this;
221 ++current;
222 return __tmp;
223 }
224
225 /**
226 * @return A reverse_iterator that refers to @c current - @a __n
227 *
228 * The underlying iterator must be a Random Access Iterator.
229 */
230 reverse_iterator
231 operator+(difference_type __n) const
232 { return reverse_iterator(current - __n); }
233
234 /**
235 * @return *this
236 *
237 * Moves the underlying iterator backwards @a __n steps.
238 * The underlying iterator must be a Random Access Iterator.
239 */
240 reverse_iterator&
241 operator+=(difference_type __n)
242 {
243 current -= __n;
244 return *this;
245 }
246
247 /**
248 * @return A reverse_iterator that refers to @c current - @a __n
249 *
250 * The underlying iterator must be a Random Access Iterator.
251 */
252 reverse_iterator
253 operator-(difference_type __n) const
254 { return reverse_iterator(current + __n); }
255
256 /**
257 * @return *this
258 *
259 * Moves the underlying iterator forwards @a __n steps.
260 * The underlying iterator must be a Random Access Iterator.
261 */
262 reverse_iterator&
263 operator-=(difference_type __n)
264 {
265 current += __n;
266 return *this;
267 }
268
269 /**
270 * @return The value at @c current - @a __n - 1
271 *
272 * The underlying iterator must be a Random Access Iterator.
273 */
274 reference
275 operator[](difference_type __n) const
276 { return *(*this + __n); }
277 };
278
279 //@{
280 /**
281 * @param __x A %reverse_iterator.
282 * @param __y A %reverse_iterator.
283 * @return A simple bool.
284 *
285 * Reverse iterators forward many operations to their underlying base()
286 * iterators. Others are implemented in terms of one another.
287 *
288 */
289 template<typename _Iterator>
290 inline bool
291 operator==(const reverse_iterator<_Iterator>& __x,
292 const reverse_iterator<_Iterator>& __y)
293 { return __x.base() == __y.base(); }
294
295 template<typename _Iterator>
296 inline bool
297 operator<(const reverse_iterator<_Iterator>& __x,
298 const reverse_iterator<_Iterator>& __y)
299 { return __y.base() < __x.base(); }
300
301 template<typename _Iterator>
302 inline bool
303 operator!=(const reverse_iterator<_Iterator>& __x,
304 const reverse_iterator<_Iterator>& __y)
305 { return !(__x == __y); }
306
307 template<typename _Iterator>
308 inline bool
309 operator>(const reverse_iterator<_Iterator>& __x,
310 const reverse_iterator<_Iterator>& __y)
311 { return __y < __x; }
312
313 template<typename _Iterator>
314 inline bool
315 operator<=(const reverse_iterator<_Iterator>& __x,
316 const reverse_iterator<_Iterator>& __y)
317 { return !(__y < __x); }
318
319 template<typename _Iterator>
320 inline bool
321 operator>=(const reverse_iterator<_Iterator>& __x,
322 const reverse_iterator<_Iterator>& __y)
323 { return !(__x < __y); }
324
325 template<typename _Iterator>
326 inline typename reverse_iterator<_Iterator>::difference_type
327 operator-(const reverse_iterator<_Iterator>& __x,
328 const reverse_iterator<_Iterator>& __y)
329 { return __y.base() - __x.base(); }
330
331 template<typename _Iterator>
332 inline reverse_iterator<_Iterator>
333 operator+(typename reverse_iterator<_Iterator>::difference_type __n,
334 const reverse_iterator<_Iterator>& __x)
335 { return reverse_iterator<_Iterator>(__x.base() - __n); }
336
337 // _GLIBCXX_RESOLVE_LIB_DEFECTS
338 // DR 280. Comparison of reverse_iterator to const reverse_iterator.
339 template<typename _IteratorL, typename _IteratorR>
340 inline bool
341 operator==(const reverse_iterator<_IteratorL>& __x,
342 const reverse_iterator<_IteratorR>& __y)
343 { return __x.base() == __y.base(); }
344
345 template<typename _IteratorL, typename _IteratorR>
346 inline bool
347 operator<(const reverse_iterator<_IteratorL>& __x,
348 const reverse_iterator<_IteratorR>& __y)
349 { return __y.base() < __x.base(); }
350
351 template<typename _IteratorL, typename _IteratorR>
352 inline bool
353 operator!=(const reverse_iterator<_IteratorL>& __x,
354 const reverse_iterator<_IteratorR>& __y)
355 { return !(__x == __y); }
356
357 template<typename _IteratorL, typename _IteratorR>
358 inline bool
359 operator>(const reverse_iterator<_IteratorL>& __x,
360 const reverse_iterator<_IteratorR>& __y)
361 { return __y < __x; }
362
363 template<typename _IteratorL, typename _IteratorR>
364 inline bool
365 operator<=(const reverse_iterator<_IteratorL>& __x,
366 const reverse_iterator<_IteratorR>& __y)
367 { return !(__y < __x); }
368
369 template<typename _IteratorL, typename _IteratorR>
370 inline bool
371 operator>=(const reverse_iterator<_IteratorL>& __x,
372 const reverse_iterator<_IteratorR>& __y)
373 { return !(__x < __y); }
374
375 template<typename _IteratorL, typename _IteratorR>
376#if __cplusplus201103L >= 201103L
377 // DR 685.
378 inline auto
379 operator-(const reverse_iterator<_IteratorL>& __x,
380 const reverse_iterator<_IteratorR>& __y)
381 -> decltype(__y.base() - __x.base())
382#else
383 inline typename reverse_iterator<_IteratorL>::difference_type
384 operator-(const reverse_iterator<_IteratorL>& __x,
385 const reverse_iterator<_IteratorR>& __y)
386#endif
387 { return __y.base() - __x.base(); }
388 //@}
389
390 // 24.4.2.2.1 back_insert_iterator
391 /**
392 * @brief Turns assignment into insertion.
393 *
394 * These are output iterators, constructed from a container-of-T.
395 * Assigning a T to the iterator appends it to the container using
396 * push_back.
397 *
398 * Tip: Using the back_inserter function to create these iterators can
399 * save typing.
400 */
401 template<typename _Container>
402 class back_insert_iterator
403 : public iterator<output_iterator_tag, void, void, void, void>
404 {
405 protected:
406 _Container* container;
407
408 public:
409 /// A nested typedef for the type of whatever container you used.
410 typedef _Container container_type;
411
412 /// The only way to create this %iterator is with a container.
413 explicit
414 back_insert_iterator(_Container& __x) : container(&__x) { }
415
416 /**
417 * @param __value An instance of whatever type
418 * container_type::const_reference is; presumably a
419 * reference-to-const T for container<T>.
420 * @return This %iterator, for chained operations.
421 *
422 * This kind of %iterator doesn't really have a @a position in the
423 * container (you can think of the position as being permanently at
424 * the end, if you like). Assigning a value to the %iterator will
425 * always append the value to the end of the container.
426 */
427#if __cplusplus201103L < 201103L
428 back_insert_iterator&
429 operator=(typename _Container::const_reference __value)
430 {
431 container->push_back(__value);
432 return *this;
433 }
434#else
435 back_insert_iterator&
436 operator=(const typename _Container::value_type& __value)
437 {
438 container->push_back(__value);
439 return *this;
440 }
441
442 back_insert_iterator&
443 operator=(typename _Container::value_type&& __value)
444 {
445 container->push_back(std::move(__value));
446 return *this;
447 }
448#endif
449
450 /// Simply returns *this.
451 back_insert_iterator&
452 operator*()
453 { return *this; }
454
455 /// Simply returns *this. (This %iterator does not @a move.)
456 back_insert_iterator&
457 operator++()
458 { return *this; }
459
460 /// Simply returns *this. (This %iterator does not @a move.)
461 back_insert_iterator
462 operator++(int)
463 { return *this; }
464 };
465
466 /**
467 * @param __x A container of arbitrary type.
468 * @return An instance of back_insert_iterator working on @p __x.
469 *
470 * This wrapper function helps in creating back_insert_iterator instances.
471 * Typing the name of the %iterator requires knowing the precise full
472 * type of the container, which can be tedious and impedes generic
473 * programming. Using this function lets you take advantage of automatic
474 * template parameter deduction, making the compiler match the correct
475 * types for you.
476 */
477 template<typename _Container>
478 inline back_insert_iterator<_Container>
479 back_inserter(_Container& __x)
480 { return back_insert_iterator<_Container>(__x); }
481
482 /**
483 * @brief Turns assignment into insertion.
484 *
485 * These are output iterators, constructed from a container-of-T.
486 * Assigning a T to the iterator prepends it to the container using
487 * push_front.
488 *
489 * Tip: Using the front_inserter function to create these iterators can
490 * save typing.
491 */
492 template<typename _Container>
493 class front_insert_iterator
494 : public iterator<output_iterator_tag, void, void, void, void>
495 {
496 protected:
497 _Container* container;
498
499 public:
500 /// A nested typedef for the type of whatever container you used.
501 typedef _Container container_type;
502
503 /// The only way to create this %iterator is with a container.
504 explicit front_insert_iterator(_Container& __x) : container(&__x) { }
505
506 /**
507 * @param __value An instance of whatever type
508 * container_type::const_reference is; presumably a
509 * reference-to-const T for container<T>.
510 * @return This %iterator, for chained operations.
511 *
512 * This kind of %iterator doesn't really have a @a position in the
513 * container (you can think of the position as being permanently at
514 * the front, if you like). Assigning a value to the %iterator will
515 * always prepend the value to the front of the container.
516 */
517#if __cplusplus201103L < 201103L
518 front_insert_iterator&
519 operator=(typename _Container::const_reference __value)
520 {
521 container->push_front(__value);
522 return *this;
523 }
524#else
525 front_insert_iterator&
526 operator=(const typename _Container::value_type& __value)
527 {
528 container->push_front(__value);
529 return *this;
530 }
531
532 front_insert_iterator&
533 operator=(typename _Container::value_type&& __value)
534 {
535 container->push_front(std::move(__value));
536 return *this;
537 }
538#endif
539
540 /// Simply returns *this.
541 front_insert_iterator&
542 operator*()
543 { return *this; }
544
545 /// Simply returns *this. (This %iterator does not @a move.)
546 front_insert_iterator&
547 operator++()
548 { return *this; }
549
550 /// Simply returns *this. (This %iterator does not @a move.)
551 front_insert_iterator
552 operator++(int)
553 { return *this; }
554 };
555
556 /**
557 * @param __x A container of arbitrary type.
558 * @return An instance of front_insert_iterator working on @p x.
559 *
560 * This wrapper function helps in creating front_insert_iterator instances.
561 * Typing the name of the %iterator requires knowing the precise full
562 * type of the container, which can be tedious and impedes generic
563 * programming. Using this function lets you take advantage of automatic
564 * template parameter deduction, making the compiler match the correct
565 * types for you.
566 */
567 template<typename _Container>
568 inline front_insert_iterator<_Container>
569 front_inserter(_Container& __x)
570 { return front_insert_iterator<_Container>(__x); }
571
572 /**
573 * @brief Turns assignment into insertion.
574 *
575 * These are output iterators, constructed from a container-of-T.
576 * Assigning a T to the iterator inserts it in the container at the
577 * %iterator's position, rather than overwriting the value at that
578 * position.
579 *
580 * (Sequences will actually insert a @e copy of the value before the
581 * %iterator's position.)
582 *
583 * Tip: Using the inserter function to create these iterators can
584 * save typing.
585 */
586 template<typename _Container>
587 class insert_iterator
588 : public iterator<output_iterator_tag, void, void, void, void>
589 {
590 protected:
591 _Container* container;
592 typename _Container::iterator iter;
593
594 public:
595 /// A nested typedef for the type of whatever container you used.
596 typedef _Container container_type;
597
598 /**
599 * The only way to create this %iterator is with a container and an
600 * initial position (a normal %iterator into the container).
601 */
602 insert_iterator(_Container& __x, typename _Container::iterator __i)
603 : container(&__x), iter(__i) {}
604
605 /**
606 * @param __value An instance of whatever type
607 * container_type::const_reference is; presumably a
608 * reference-to-const T for container<T>.
609 * @return This %iterator, for chained operations.
610 *
611 * This kind of %iterator maintains its own position in the
612 * container. Assigning a value to the %iterator will insert the
613 * value into the container at the place before the %iterator.
614 *
615 * The position is maintained such that subsequent assignments will
616 * insert values immediately after one another. For example,
617 * @code
618 * // vector v contains A and Z
619 *
620 * insert_iterator i (v, ++v.begin());
621 * i = 1;
622 * i = 2;
623 * i = 3;
624 *
625 * // vector v contains A, 1, 2, 3, and Z
626 * @endcode
627 */
628#if __cplusplus201103L < 201103L
629 insert_iterator&
630 operator=(typename _Container::const_reference __value)
631 {
632 iter = container->insert(iter, __value);
633 ++iter;
634 return *this;
635 }
636#else
637 insert_iterator&
638 operator=(const typename _Container::value_type& __value)
639 {
640 iter = container->insert(iter, __value);
641 ++iter;
642 return *this;
643 }
644
645 insert_iterator&
646 operator=(typename _Container::value_type&& __value)
647 {
648 iter = container->insert(iter, std::move(__value));
649 ++iter;
650 return *this;
651 }
652#endif
653
654 /// Simply returns *this.
655 insert_iterator&
656 operator*()
657 { return *this; }
658
659 /// Simply returns *this. (This %iterator does not @a move.)
660 insert_iterator&
661 operator++()
662 { return *this; }
663
664 /// Simply returns *this. (This %iterator does not @a move.)
665 insert_iterator&
666 operator++(int)
667 { return *this; }
668 };
669
670 /**
671 * @param __x A container of arbitrary type.
672 * @return An instance of insert_iterator working on @p __x.
673 *
674 * This wrapper function helps in creating insert_iterator instances.
675 * Typing the name of the %iterator requires knowing the precise full
676 * type of the container, which can be tedious and impedes generic
677 * programming. Using this function lets you take advantage of automatic
678 * template parameter deduction, making the compiler match the correct
679 * types for you.
680 */
681 template<typename _Container, typename _Iterator>
682 inline insert_iterator<_Container>
683 inserter(_Container& __x, _Iterator __i)
684 {
685 return insert_iterator<_Container>(__x,
686 typename _Container::iterator(__i));
687 }
688
689 // @} group iterators
690
691_GLIBCXX_END_NAMESPACE_VERSION
692} // namespace
693
694namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default")))
695{
696_GLIBCXX_BEGIN_NAMESPACE_VERSION
697
698 // This iterator adapter is @a normal in the sense that it does not
699 // change the semantics of any of the operators of its iterator
700 // parameter. Its primary purpose is to convert an iterator that is
701 // not a class, e.g. a pointer, into an iterator that is a class.
702 // The _Container parameter exists solely so that different containers
703 // using this template can instantiate different types, even if the
704 // _Iterator parameter is the same.
705 using std::iterator_traits;
706 using std::iterator;
707 template<typename _Iterator, typename _Container>
708 class __normal_iterator
709 {
710 protected:
711 _Iterator _M_current;
712
713 typedef iterator_traits<_Iterator> __traits_type;
714
715 public:
716 typedef _Iterator iterator_type;
717 typedef typename __traits_type::iterator_category iterator_category;
718 typedef typename __traits_type::value_type value_type;
719 typedef typename __traits_type::difference_type difference_type;
720 typedef typename __traits_type::reference reference;
721 typedef typename __traits_type::pointer pointer;
722
723 _GLIBCXX_CONSTEXPRconstexpr __normal_iterator() : _M_current(_Iterator()) { }
724
725 explicit
726 __normal_iterator(const _Iterator& __i) : _M_current(__i) { }
727
728 // Allow iterator to const_iterator conversion
729 template<typename _Iter>
730 __normal_iterator(const __normal_iterator<_Iter,
731 typename __enable_if<
732 (std::__are_same<_Iter, typename _Container::pointer>::__value),
733 _Container>::__type>& __i)
734 : _M_current(__i.base()) { }
735
736 // Forward iterator requirements
737 reference
738 operator*() const
739 { return *_M_current; }
740
741 pointer
742 operator->() const
743 { return _M_current; }
744
745 __normal_iterator&
746 operator++()
747 {
748 ++_M_current;
749 return *this;
750 }
751
752 __normal_iterator
753 operator++(int)
754 { return __normal_iterator(_M_current++); }
755
756 // Bidirectional iterator requirements
757 __normal_iterator&
758 operator--()
759 {
760 --_M_current;
761 return *this;
762 }
763
764 __normal_iterator
765 operator--(int)
766 { return __normal_iterator(_M_current--); }
767
768 // Random access iterator requirements
769 reference
770 operator[](const difference_type& __n) const
771 { return _M_current[__n]; }
772
773 __normal_iterator&
774 operator+=(const difference_type& __n)
775 { _M_current += __n; return *this; }
776
777 __normal_iterator
778 operator+(const difference_type& __n) const
779 { return __normal_iterator(_M_current + __n); }
780
781 __normal_iterator&
782 operator-=(const difference_type& __n)
783 { _M_current -= __n; return *this; }
784
785 __normal_iterator
786 operator-(const difference_type& __n) const
787 { return __normal_iterator(_M_current - __n); }
788
789 const _Iterator&
790 base() const
791 { return _M_current; }
792 };
793
794 // Note: In what follows, the left- and right-hand-side iterators are
795 // allowed to vary in types (conceptually in cv-qualification) so that
796 // comparison between cv-qualified and non-cv-qualified iterators be
797 // valid. However, the greedy and unfriendly operators in std::rel_ops
798 // will make overload resolution ambiguous (when in scope) if we don't
799 // provide overloads whose operands are of the same type. Can someone
800 // remind me what generic programming is about? -- Gaby
801
802 // Forward iterator requirements
803 template<typename _IteratorL, typename _IteratorR, typename _Container>
804 inline bool
805 operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
806 const __normal_iterator<_IteratorR, _Container>& __rhs)
807 { return __lhs.base() == __rhs.base(); }
808
809 template<typename _Iterator, typename _Container>
810 inline bool
811 operator==(const __normal_iterator<_Iterator, _Container>& __lhs,
812 const __normal_iterator<_Iterator, _Container>& __rhs)
813 { return __lhs.base() == __rhs.base(); }
814
815 template<typename _IteratorL, typename _IteratorR, typename _Container>
816 inline bool
817 operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs,
818 const __normal_iterator<_IteratorR, _Container>& __rhs)
819 { return __lhs.base() != __rhs.base(); }
820
821 template<typename _Iterator, typename _Container>
822 inline bool
823 operator!=(const __normal_iterator<_Iterator, _Container>& __lhs,
824 const __normal_iterator<_Iterator, _Container>& __rhs)
825 { return __lhs.base() != __rhs.base(); }
18
Assuming the condition is true
19
Returning the value 1, which participates in a condition later
826
827 // Random access iterator requirements
828 template<typename _IteratorL, typename _IteratorR, typename _Container>
829 inline bool
830 operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
831 const __normal_iterator<_IteratorR, _Container>& __rhs)
832 { return __lhs.base() < __rhs.base(); }
833
834 template<typename _Iterator, typename _Container>
835 inline bool
836 operator<(const __normal_iterator<_Iterator, _Container>& __lhs,
837 const __normal_iterator<_Iterator, _Container>& __rhs)
838 { return __lhs.base() < __rhs.base(); }
839
840 template<typename _IteratorL, typename _IteratorR, typename _Container>
841 inline bool
842 operator>(const __normal_iterator<_IteratorL, _Container>& __lhs,
843 const __normal_iterator<_IteratorR, _Container>& __rhs)
844 { return __lhs.base() > __rhs.base(); }
845
846 template<typename _Iterator, typename _Container>
847 inline bool
848 operator>(const __normal_iterator<_Iterator, _Container>& __lhs,
849 const __normal_iterator<_Iterator, _Container>& __rhs)
850 { return __lhs.base() > __rhs.base(); }
851
852 template<typename _IteratorL, typename _IteratorR, typename _Container>
853 inline bool
854 operator<=(const __normal_iterator<_IteratorL, _Container>& __lhs,
855 const __normal_iterator<_IteratorR, _Container>& __rhs)
856 { return __lhs.base() <= __rhs.base(); }
857
858 template<typename _Iterator, typename _Container>
859 inline bool
860 operator<=(const __normal_iterator<_Iterator, _Container>& __lhs,
861 const __normal_iterator<_Iterator, _Container>& __rhs)
862 { return __lhs.base() <= __rhs.base(); }
863
864 template<typename _IteratorL, typename _IteratorR, typename _Container>
865 inline bool
866 operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs,
867 const __normal_iterator<_IteratorR, _Container>& __rhs)
868 { return __lhs.base() >= __rhs.base(); }
869
870 template<typename _Iterator, typename _Container>
871 inline bool
872 operator>=(const __normal_iterator<_Iterator, _Container>& __lhs,
873 const __normal_iterator<_Iterator, _Container>& __rhs)
874 { return __lhs.base() >= __rhs.base(); }
875
876 // _GLIBCXX_RESOLVE_LIB_DEFECTS
877 // According to the resolution of DR179 not only the various comparison
878 // operators but also operator- must accept mixed iterator/const_iterator
879 // parameters.
880 template<typename _IteratorL, typename _IteratorR, typename _Container>
881#if __cplusplus201103L >= 201103L
882 // DR 685.
883 inline auto
884 operator-(const __normal_iterator<_IteratorL, _Container>& __lhs,
885 const __normal_iterator<_IteratorR, _Container>& __rhs)
886 -> decltype(__lhs.base() - __rhs.base())
887#else
888 inline typename __normal_iterator<_IteratorL, _Container>::difference_type
889 operator-(const __normal_iterator<_IteratorL, _Container>& __lhs,
890 const __normal_iterator<_IteratorR, _Container>& __rhs)
891#endif
892 { return __lhs.base() - __rhs.base(); }
893
894 template<typename _Iterator, typename _Container>
895 inline typename __normal_iterator<_Iterator, _Container>::difference_type
896 operator-(const __normal_iterator<_Iterator, _Container>& __lhs,
897 const __normal_iterator<_Iterator, _Container>& __rhs)
898 { return __lhs.base() - __rhs.base(); }
899
900 template<typename _Iterator, typename _Container>
901 inline __normal_iterator<_Iterator, _Container>
902 operator+(typename __normal_iterator<_Iterator, _Container>::difference_type
903 __n, const __normal_iterator<_Iterator, _Container>& __i)
904 { return __normal_iterator<_Iterator, _Container>(__i.base() + __n); }
905
906_GLIBCXX_END_NAMESPACE_VERSION
907} // namespace
908
909#if __cplusplus201103L >= 201103L
910
911namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default")))
912{
913_GLIBCXX_BEGIN_NAMESPACE_VERSION
914
915 /**
916 * @addtogroup iterators
917 * @{
918 */
919
920 // 24.4.3 Move iterators
921 /**
922 * Class template move_iterator is an iterator adapter with the same
923 * behavior as the underlying iterator except that its dereference
924 * operator implicitly converts the value returned by the underlying
925 * iterator's dereference operator to an rvalue reference. Some
926 * generic algorithms can be called with move iterators to replace
927 * copying with moving.
928 */
929 template<typename _Iterator>
930 class move_iterator
931 {
932 protected:
933 _Iterator _M_current;
934
935 typedef iterator_traits<_Iterator> __traits_type;
936
937 public:
938 typedef _Iterator iterator_type;
939 typedef typename __traits_type::iterator_category iterator_category;
940 typedef typename __traits_type::value_type value_type;
941 typedef typename __traits_type::difference_type difference_type;
942 // NB: DR 680.
943 typedef _Iterator pointer;
944 typedef value_type&& reference;
945
946 move_iterator()
947 : _M_current() { }
948
949 explicit
950 move_iterator(iterator_type __i)
951 : _M_current(__i) { }
952
953 template<typename _Iter>
954 move_iterator(const move_iterator<_Iter>& __i)
955 : _M_current(__i.base()) { }
956
957 iterator_type
958 base() const
959 { return _M_current; }
960
961 reference
962 operator*() const
963 { return std::move(*_M_current); }
964
965 pointer
966 operator->() const
967 { return _M_current; }
968
969 move_iterator&
970 operator++()
971 {
972 ++_M_current;
973 return *this;
974 }
975
976 move_iterator
977 operator++(int)
978 {
979 move_iterator __tmp = *this;
980 ++_M_current;
981 return __tmp;
982 }
983
984 move_iterator&
985 operator--()
986 {
987 --_M_current;
988 return *this;
989 }
990
991 move_iterator
992 operator--(int)
993 {
994 move_iterator __tmp = *this;
995 --_M_current;
996 return __tmp;
997 }
998
999 move_iterator
1000 operator+(difference_type __n) const
1001 { return move_iterator(_M_current + __n); }
1002
1003 move_iterator&
1004 operator+=(difference_type __n)
1005 {
1006 _M_current += __n;
1007 return *this;
1008 }
1009
1010 move_iterator
1011 operator-(difference_type __n) const
1012 { return move_iterator(_M_current - __n); }
1013
1014 move_iterator&
1015 operator-=(difference_type __n)
1016 {
1017 _M_current -= __n;
1018 return *this;
1019 }
1020
1021 reference
1022 operator[](difference_type __n) const
1023 { return std::move(_M_current[__n]); }
1024 };
1025
1026 // Note: See __normal_iterator operators note from Gaby to understand
1027 // why there are always 2 versions for most of the move_iterator
1028 // operators.
1029 template<typename _IteratorL, typename _IteratorR>
1030 inline bool
1031 operator==(const move_iterator<_IteratorL>& __x,
1032 const move_iterator<_IteratorR>& __y)
1033 { return __x.base() == __y.base(); }
1034
1035 template<typename _Iterator>
1036 inline bool
1037 operator==(const move_iterator<_Iterator>& __x,
1038 const move_iterator<_Iterator>& __y)
1039 { return __x.base() == __y.base(); }
1040
1041 template<typename _IteratorL, typename _IteratorR>
1042 inline bool
1043 operator!=(const move_iterator<_IteratorL>& __x,
1044 const move_iterator<_IteratorR>& __y)
1045 { return !(__x == __y); }
1046
1047 template<typename _Iterator>
1048 inline bool
1049 operator!=(const move_iterator<_Iterator>& __x,
1050 const move_iterator<_Iterator>& __y)
1051 { return !(__x == __y); }
1052
1053 template<typename _IteratorL, typename _IteratorR>
1054 inline bool
1055 operator<(const move_iterator<_IteratorL>& __x,
1056 const move_iterator<_IteratorR>& __y)
1057 { return __x.base() < __y.base(); }
1058
1059 template<typename _Iterator>
1060 inline bool
1061 operator<(const move_iterator<_Iterator>& __x,
1062 const move_iterator<_Iterator>& __y)
1063 { return __x.base() < __y.base(); }
1064
1065 template<typename _IteratorL, typename _IteratorR>
1066 inline bool
1067 operator<=(const move_iterator<_IteratorL>& __x,
1068 const move_iterator<_IteratorR>& __y)
1069 { return !(__y < __x); }
1070
1071 template<typename _Iterator>
1072 inline bool
1073 operator<=(const move_iterator<_Iterator>& __x,
1074 const move_iterator<_Iterator>& __y)
1075 { return !(__y < __x); }
1076
1077 template<typename _IteratorL, typename _IteratorR>
1078 inline bool
1079 operator>(const move_iterator<_IteratorL>& __x,
1080 const move_iterator<_IteratorR>& __y)
1081 { return __y < __x; }
1082
1083 template<typename _Iterator>
1084 inline bool
1085 operator>(const move_iterator<_Iterator>& __x,
1086 const move_iterator<_Iterator>& __y)
1087 { return __y < __x; }
1088
1089 template<typename _IteratorL, typename _IteratorR>
1090 inline bool
1091 operator>=(const move_iterator<_IteratorL>& __x,
1092 const move_iterator<_IteratorR>& __y)
1093 { return !(__x < __y); }
1094
1095 template<typename _Iterator>
1096 inline bool
1097 operator>=(const move_iterator<_Iterator>& __x,
1098 const move_iterator<_Iterator>& __y)
1099 { return !(__x < __y); }
1100
1101 // DR 685.
1102 template<typename _IteratorL, typename _IteratorR>
1103 inline auto
1104 operator-(const move_iterator<_IteratorL>& __x,
1105 const move_iterator<_IteratorR>& __y)
1106 -> decltype(__x.base() - __y.base())
1107 { return __x.base() - __y.base(); }
1108
1109 template<typename _Iterator>
1110 inline auto
1111 operator-(const move_iterator<_Iterator>& __x,
1112 const move_iterator<_Iterator>& __y)
1113 -> decltype(__x.base() - __y.base())
1114 { return __x.base() - __y.base(); }
1115
1116 template<typename _Iterator>
1117 inline move_iterator<_Iterator>
1118 operator+(typename move_iterator<_Iterator>::difference_type __n,
1119 const move_iterator<_Iterator>& __x)
1120 { return __x + __n; }
1121
1122 template<typename _Iterator>
1123 inline move_iterator<_Iterator>
1124 make_move_iterator(_Iterator __i)
1125 { return move_iterator<_Iterator>(__i); }
1126
1127 template<typename _Iterator, typename _ReturnType
1128 = typename conditional<__move_if_noexcept_cond
1129 <typename iterator_traits<_Iterator>::value_type>::value,
1130 _Iterator, move_iterator<_Iterator>>::type>
1131 inline _ReturnType
1132 __make_move_if_noexcept_iterator(_Iterator __i)
1133 { return _ReturnType(__i); }
1134
1135 // @} group iterators
1136
1137_GLIBCXX_END_NAMESPACE_VERSION
1138} // namespace
1139
1140#define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter)std::make_move_iterator(_Iter) std::make_move_iterator(_Iter)
1141#define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter)std::__make_move_if_noexcept_iterator(_Iter) \
1142 std::__make_move_if_noexcept_iterator(_Iter)
1143#else
1144#define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter)std::make_move_iterator(_Iter) (_Iter)
1145#define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter)std::__make_move_if_noexcept_iterator(_Iter) (_Iter)
1146#endif // C++11
1147
1148#endif