CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
AbsReference.cc
Go to the documentation of this file.
1 #include <algorithm>
2 
3 #include "Alignment/Geners/interface/AbsReference.hh"
4 #include "Alignment/Geners/interface/CatalogEntry.hh"
5 #include "Alignment/Geners/interface/AbsArchive.hh"
6 #include "Alignment/Geners/interface/IOException.hh"
7 
8 // Notes on implementation
9 //
10 // The following invariants should be preserved:
11 //
12 // 0 items: must have itemId_ == 0 && idList_.empty()
13 // 1 item: must have itemId_ != 0 && idList_.empty()
14 // more items: must have itemId_ == 0 && !idList_.empty()
15 //
16 namespace gs {
17  std::istream& AbsReference::positionInputStream(
18  const unsigned long long id) const
19  {
20  return archive_.inputStream(id, 0);
21  }
22 
23  void AbsReference::initialize() const
24  {
25  AbsReference* modifiable = const_cast<AbsReference*>(this);
26  if (searchId_)
27  {
28  // Make sure that the searchId_ item id is going to produce
29  // a valid item
30  CPP11_shared_ptr<const CatalogEntry> record =
31  archive_.catalogEntry(searchId_);
32  const unsigned long long idFound = record->id();
33 
34  // Check for valid id in the archive
35  if (idFound && this->isIOCompatible(*record))
36  {
37  if (idFound != searchId_) throw IOInvalidData(
38  "In AbsReference::initialize: catalog is corrupted");
39  modifiable->itemId_ = searchId_;
40  }
41  }
42  else
43  {
44  // Search for matching items in the archive
45  archive_.search(*modifiable);
46  if (!idList_.empty())
47  {
48  // Check if the list is sorted.
49  // Sort if it is not.
50  const unsigned long nFound = idList_.size();
51  const unsigned long long* ids = &idList_[0];
52  bool sorted = true;
53  for (unsigned long i=1; i<nFound; ++i)
54  if (ids[i-1] >= ids[i])
55  {
56  sorted = false;
57  break;
58  }
59  if (!sorted)
60  std::sort(modifiable->idList_.begin(),
61  modifiable->idList_.end());
62  }
63  }
64  modifiable->initialized_ = true;
65  }
66 
67  bool AbsReference::isIOCompatible(const CatalogEntry& r) const
68  {
69  return !classId_.name().empty() &&
70  classId_.name() == r.type().name() &&
71  ioProto_ == r.ioPrototype();
72  }
73 
74  bool AbsReference::empty() const
75  {
76  if (!initialized_)
77  initialize();
78  return itemId_ == 0 && idList_.empty();
79  }
80 
81  bool AbsReference::unique() const
82  {
83  if (!initialized_)
84  initialize();
85  return itemId_ && idList_.empty();
86  }
87 
88  unsigned long AbsReference::size() const
89  {
90  if (!initialized_)
91  initialize();
92  return itemId_ ? 1UL : idList_.size();
93  }
94 
95  unsigned long long AbsReference::id(const unsigned long index) const
96  {
97  if (!initialized_)
98  initialize();
99  if (itemId_ && index == 0UL)
100  return itemId_;
101  else if (index < idList_.size())
102  return idList_[index];
103  else
104  throw gs::IOOutOfRange("In gs::AbsReference::id: "
105  "index out of range");
106  }
107 
108  CPP11_shared_ptr<const CatalogEntry>
109  AbsReference::indexedCatalogEntry(const unsigned long index) const
110  {
111  return archive_.catalogEntry(id(index));
112  }
113 
114  bool AbsReference::isSameIOPrototype(const CatalogEntry& r) const
115  {
116  return ioProto_ == r.ioPrototype();
117  }
118 
119  void AbsReference::addItemId(const unsigned long long idIn)
120  {
121  if (!idIn) throw gs::IOInvalidArgument(
122  "In AbsReference::addItemId: invalid item id");
123  const unsigned long mySize = itemId_ ? 1UL : idList_.size();
124  switch (mySize)
125  {
126  case 0UL:
127  itemId_ = idIn;
128  break;
129 
130  case 1UL:
131  idList_.reserve(2);
132  idList_.push_back(itemId_);
133  idList_.push_back(idIn);
134  itemId_ = 0ULL;
135  break;
136 
137  default:
138  idList_.push_back(idIn);
139  break;
140  }
141  }
142 
143  AbsReference::AbsReference(AbsArchive& ar, const ClassId& classId,
144  const char* ioPrototype,
145  const unsigned long long itemId)
146  : archive_(ar),
147  classId_(classId),
148  ioProto_(ioPrototype ? ioPrototype : ""),
149  searchId_(itemId),
150  namePattern_(0),
151  categoryPattern_(0),
152  itemId_(0),
153  initialized_(false)
154  {
155  if (!itemId) throw gs::IOInvalidArgument(
156  "In AbsReference constructor: invalid item id");
157  }
158 
159  AbsReference::AbsReference(AbsArchive& ar, const ClassId& classId,
160  const char* ioPrototype,
161  const SearchSpecifier& namePattern,
162  const SearchSpecifier& categoryPattern)
163  : archive_(ar),
164  classId_(classId),
165  ioProto_(ioPrototype ? ioPrototype : ""),
166  searchId_(0),
167  namePattern_(namePattern),
168  categoryPattern_(categoryPattern),
169  itemId_(0),
170  initialized_(false)
171  {
172  }
173 }
static AlgebraicMatrix initialize()
int i
Definition: DBlmapReader.cc:9
JetCorrectorParameters::Record record
Definition: classes.h:7
volatile std::atomic< bool > shutdown_flag false
tuple size
Write out results.