Class Reference for E1039 Core & Analysis Software
PHGenEventListv1.cc
Go to the documentation of this file.
1 #include "PHGenEventListv1.h"
2 
3 #include <vector>
4 #include <algorithm>
5 #include <iostream>
6 
7 using namespace std;
8 
10 
12 : PHGenEventList(),
13  _genevents(),
14  _stale(true),
15  _id_genevent_map() {
16 }
17 
19  _genevents.clear();
20  _id_genevent_map.clear();
21 }
22 
23 void PHGenEventListv1::identify(std::ostream& out) const {
24  if (_stale) refresh();
25  out << "PHGenEventListv1" << endl;
26 }
27 
28 void PHGenEventListv1::print(std::ostream& out) const {
29  if (_stale) refresh();
30 
31  identify(out);
32  out << " size = " << size() << endl;
33  for (size_t i=0; i<size(); ++i) {
34  _genevents[i].print(out);
35  }
36 }
37 
39  clear();
40  _stale = true;
41 }
42 
43 const PHGenEvent* PHGenEventListv1::at(size_t i) const {
44  if (_stale) refresh();
45  const PHGenEventVersion* versioned_const = &_genevents[i];
46  const PHGenEvent* genevent
47  = static_cast<const PHGenEvent*>(versioned_const);
48  return genevent;
49 }
50 
52  if (_stale) refresh();
53  PHGenEventVersion* versioned = &_genevents[i];
54  PHGenEvent* genevent = static_cast<PHGenEvent*>(versioned);
55  return genevent;
56 }
57 
58 bool PHGenEventListv1::has(unsigned int id) const {
59  if (_stale) refresh();
60  if (_id_genevent_map.find(id) == _id_genevent_map.end()) return false;
61  return true;
62 }
63 
64 size_t PHGenEventListv1::find(unsigned int id) const {
65  if (_stale) refresh();
66  for (size_t i = 0; i < _genevents.size(); ++i) {
67  if (_genevents[i].get_id() == id) return i;
68  }
69  return _genevents.size();
70 }
71 
72 const PHGenEvent* PHGenEventListv1::fetch(unsigned int id) const {
73  if (_stale) refresh();
74  if (_id_genevent_map.find(id) != _id_genevent_map.end()) {
75  return (const PHGenEvent*)_id_genevent_map[id];
76  }
77  return NULL;
78 }
79 
81  if (_stale) refresh();
82  if (_id_genevent_map.find(id) != _id_genevent_map.end()) {
83  return (PHGenEvent*)_id_genevent_map[id];
84  }
85  return NULL;
86 }
87 
88 void PHGenEventListv1::insert(const PHGenEvent* genevent) {
89  if (_stale) refresh();
90 
91  if (has(genevent->get_id())) {
92  size_t pos = find(genevent->get_id());
93  remove(pos);
94  }
95 
96  const PHGenEventVersion* versioned_const
97  = dynamic_cast<const PHGenEventVersion*>(genevent);
98  if (versioned_const) {
99  _genevents.push_back(PHGenEventVersion(*versioned_const));
100  _id_genevent_map.insert(make_pair(genevent->get_id(),
101  &_genevents[_genevents.size()-1]));
102  }
103  return;
104 }
105 
106 unsigned int PHGenEventListv1::generate_id() const {
107  if (_stale) refresh();
108 
109  unsigned int key = 0;
110  if (_id_genevent_map.empty()) return key;
111 
112  key = _id_genevent_map.rbegin()->first + 1;
113  return key;
114 }
115 
116 void PHGenEventListv1::remove(size_t i) {
117  _genevents.erase(_genevents.begin()+i);
118  refresh();
119 }
120 
122  _genevents.clear();
123  _id_genevent_map.clear();
124 }
125 
126 void PHGenEventListv1::refresh() const {
127 
128  _id_genevent_map.clear();
129 
130  for (size_t i = 0; i < _genevents.size(); ++i) {
131  _id_genevent_map.insert(make_pair(_genevents[i].get_id(),&_genevents[i]));
132  }
133 
134  _stale = false;
135 }
ClassImp(PHGenEventListv1) PHGenEventListv1
#define NULL
Definition: Pdb.h:9
unsigned int generate_id() const
void insert(const PHGenEvent *genevent)
bool has(unsigned int id) const
PHGenEventv1 PHGenEventVersion
void print(std::ostream &out=std::cout) const
void Reset()
Clear Event.
const PHGenEvent * at(size_t i) const
size_t find(unsigned int id) const
size_t size() const
const PHGenEvent * fetch(unsigned int id) const
virtual ~PHGenEventListv1()
void identify(std::ostream &out=std::cout) const
void remove(size_t i)
virtual unsigned int get_id() const
Definition: PHGenEvent.h:19