evio  6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
StructureTransformer.h
Go to the documentation of this file.
1 //
2 // Copyright 2020, Jefferson Science Associates, LLC.
3 // Subject to the terms in the LICENSE file found in the top-level directory.
4 //
5 // EPSCI Group
6 // Thomas Jefferson National Accelerator Facility
7 // 12000, Jefferson Ave, Newport News, VA 23606
8 // (757)-269-7100
9 
10 
11 #ifndef EVIO_6_0_STRUCTURETRANSFORMER_H
12 #define EVIO_6_0_STRUCTURETRANSFORMER_H
13 
14 
15 #include <cstring>
16 #include <sstream>
17 #include <memory>
18 
19 
20 #include "BankHeader.h"
21 #include "SegmentHeader.h"
22 #include "TagSegmentHeader.h"
23 #include "DataType.h"
24 #include "EvioBank.h"
25 #include "EvioSegment.h"
26 #include "EvioTagSegment.h"
27 
28 
29 namespace evio {
30 
39 
40  public:
41 
42  // Segment --> Bank
43 
54  static std::shared_ptr<EvioBank> transform(std::shared_ptr<EvioSegment> const & segment, uint8_t num) {
55  // Copy over header & create new EvioBank
56  auto const & segHeader = segment->getHeader();
57  auto bank = EvioBank::getInstance(segHeader->getTag(), segHeader->getDataType(), num);
58  auto const & bankHeader = bank->getHeader();
59  bankHeader->setLength(segHeader->getLength() + 1);
60  bankHeader->setPadding(segHeader->getPadding());
61 
62  // Copy over the data (take care of padding)
63  bank->transform(segment);
64  return bank;
65  }
66 
76  static void copy(std::shared_ptr<EvioBank> const & bank,
77  std::shared_ptr<EvioSegment> const & segment,
78  uint8_t num) {
79 
80  // Copy over header
81  auto const & segHeader = segment->getHeader();
82  auto const & bankHeader = bank->getHeader();
83  bankHeader->copy(segHeader);
84  bankHeader->setNumber(num);
85  bankHeader->setLength(segHeader->getLength() + 1);
86  bankHeader->setPadding(segHeader->getPadding());
87 
88  // Copy over the data (take care of padding)
89  bank->transform(segment);
90  }
91 
92  // TagSegment --> Bank
93 
107  static std::shared_ptr<EvioBank> transform(std::shared_ptr<EvioTagSegment> const & tagsegment, uint8_t num) {
108  auto const & tagsegHeader = tagsegment->getHeader();
109  auto bank = EvioBank::getInstance(tagsegHeader->getTag(), tagsegHeader->getDataType(), num);
110  auto const & bankHeader = bank->getHeader();
111  bankHeader->setLength(tagsegHeader->getLength() + 1);
112 
113  bank->transform(tagsegment);
114  return bank;
115  }
116 
129  static void copy(std::shared_ptr<EvioBank> const & bank,
130  std::shared_ptr<EvioTagSegment> const & tagsegment,
131  uint8_t num) {
132 
133  auto const & tagsegHeader = tagsegment->getHeader();
134  auto const & bankHeader = bank->getHeader();
135  bankHeader->copy(tagsegHeader);
136  bankHeader->setNumber(num);
137  bankHeader->setLength(tagsegHeader->getLength() + 1);
138 
139  bank->transform(tagsegment);
140  }
141 
142  // Segment --> TagSegment
143 
158  static std::shared_ptr<EvioTagSegment> transform(std::shared_ptr<EvioSegment> const & segment) {
159  auto const & segHeader = segment->getHeader();
160  auto ts = EvioTagSegment::getInstance(segHeader->getTag(), segHeader->getDataType());
161  auto const & tsHeader = ts->getHeader();
162  tsHeader->setLength(segHeader->getLength());
163  tsHeader->setPadding(segHeader->getPadding());
164 
165  // Change 6 bit content type to 4 bits. Do this by changing
166  // BANK to ALSOBANK, SEGMENT to ALSOSEGMENT
167  DataType const & segType = segHeader->getDataType();
168  if (segType == DataType::BANK) {
169  tsHeader->setDataType(DataType::ALSOBANK);
170  }
171  else if (segType == DataType::SEGMENT) {
172  tsHeader->setDataType(DataType::ALSOSEGMENT);
173  }
174 
175  ts->transform(segment);
176  return ts;
177  }
178 
192  static void copy(std::shared_ptr<EvioTagSegment> const & tagsegment,
193  std::shared_ptr<EvioSegment> const & segment) {
194 
195  auto const & segHeader = segment->getHeader();
196  auto const & tsHeader = tagsegment->getHeader();
197  tsHeader->copy(segHeader);
198 
199  // Change 6 bit content type to equivalent 4 bits
200  DataType const & segType = segHeader->getDataType();
201  if (segType == DataType::BANK) {
202  tsHeader->setDataType(DataType::ALSOBANK);
203  }
204  else if (segType == DataType::SEGMENT) {
205  tsHeader->setDataType(DataType::ALSOSEGMENT);
206  }
207 
208  tagsegment->transform(segment);
209  }
210 
211  // TagSegment --> Segment
212 
228  static std::shared_ptr<EvioSegment> transform(std::shared_ptr<EvioTagSegment> const & tagsegment) {
229  auto const & tsHeader = tagsegment->getHeader();
230  auto seg = EvioSegment::getInstance(tsHeader->getTag(), tsHeader->getDataType());
231  auto const & segHeader = seg->getHeader();
232  segHeader->setLength(tsHeader->getLength());
233 
234  seg->transform(tagsegment);
235  return seg;
236  }
237 
252  static void copy(std::shared_ptr<EvioSegment> const & segment,
253  std::shared_ptr<EvioSegment> const & tagsegment) {
254 
255  auto const & tsHeader = tagsegment->getHeader();
256  auto const & segHeader = segment->getHeader();
257  segHeader->copy(tsHeader);
258  tagsegment->transform(segment);
259  }
260 
261  // Bank -> Segment
262 
284  static std::shared_ptr<EvioSegment> transform(std::shared_ptr<EvioBank> const & bank) {
285  auto const & bankHeader = bank->getHeader();
286  size_t bankLen = bankHeader->getLength();
287  if (bankLen > 65535) {
288  throw new EvioException("Bank is too long to transform into segment");
289  }
290  auto segment = EvioSegment::getInstance(bankHeader->getTag(), bankHeader->getDataType());
291  auto const & segHeader = segment->getHeader();
292  segHeader->setLength(bankLen - 1);
293  segHeader->setPadding(bankHeader->getPadding());
294  segHeader->setNumber(bankHeader->getNumber());
295 
296  segment->transform(bank);
297  return segment;
298  }
299 
300 
321  static void copy(std::shared_ptr<EvioSegment> const & segment,
322  std::shared_ptr<EvioBank> const & bank) {
323 
324  auto const & bankHeader = bank->getHeader();
325  size_t bankLen = bankHeader->getLength();
326  if (bankLen > 65535) {
327  throw new EvioException("Bank is too long to transform into segment");
328  }
329  auto const & segHeader = segment->getHeader();
330  segHeader->copy(bankHeader);
331  segHeader->setLength(bankLen - 1);
332 
333  segment->transform(bank);
334  }
335 
336  // Bank -> TagSegment
337 
364  static std::shared_ptr<EvioTagSegment> transform(std::shared_ptr<EvioBank> const & bank,
365  int dummy) {
366  auto const & bankHeader = bank->getHeader();
367  if (bankHeader->getLength() > 65535) {
368  throw new EvioException("Bank is too long to transform into segment");
369  }
370  auto ts = EvioTagSegment::getInstance(bankHeader->getTag(), bankHeader->getDataType());
371  auto const & tsHeader = ts->getHeader();
372  tsHeader->setLength(bankHeader->getLength() - 1);
373  tsHeader->setPadding(bankHeader->getPadding());
374  tsHeader->setNumber(bankHeader->getNumber());
375 
376  DataType const & tsType = tsHeader->getDataType();
377  if (tsType == DataType::BANK) {
378  tsHeader->setDataType(DataType::ALSOBANK);
379  }
380  else if (tsType == DataType::SEGMENT) {
381  tsHeader->setDataType(DataType::ALSOSEGMENT);
382  }
383 
384  ts->transform(bank);
385  return ts;
386  }
387 
388 
409  static void copy(std::shared_ptr<EvioTagSegment> const & tagsegment,
410  std::shared_ptr<EvioBank> const & bank) {
411 
412  auto const & bankHeader = bank->getHeader();
413  size_t bankLen = bankHeader->getLength();
414  if (bankLen > 65535) {
415  throw new EvioException("Bank is too long to transform into tagsegment");
416  }
417  auto const & tsHeader = tagsegment->getHeader();
418  tsHeader->copy(bankHeader);
419  tsHeader->setLength(bankLen - 1);
420 
421  DataType const & bankType = bankHeader->getDataType();
422  if (bankType == DataType::BANK) {
423  tsHeader->setDataType(DataType::ALSOBANK);
424  }
425  else if (bankType == DataType::SEGMENT) {
426  tsHeader->setDataType(DataType::ALSOSEGMENT);
427  }
428 
429  tagsegment->transform(bank);
430  }
431 
432 
433  };
434 
435 
436 
437 }
438 
439 #endif //EVIO_6_0_STRUCTURETRANSFORMER_H
static const DataType ALSOBANK
Bank alternate value.
Definition: DataType.h:50
static std::shared_ptr< EvioSegment > getInstance(std::shared_ptr< SegmentHeader > const &head)
Method to return a shared pointer to a constructed object of this class.
Definition: EvioSegment.h:54
static const DataType SEGMENT
Segment.
Definition: DataType.h:53
Exception class for Evio software package.
Definition: EvioException.h:29
static std::shared_ptr< EvioTagSegment > transform(std::shared_ptr< EvioSegment > const &segment)
Create an EvioTagSegment object from an EvioSegment.
Definition: StructureTransformer.h:158
static const DataType & getDataType(uint32_t val)
Get the object from the integer value.
Definition: DataType.h:100
static std::shared_ptr< EvioSegment > transform(std::shared_ptr< EvioTagSegment > const &tagsegment)
Create an EvioSegment object from an EvioTagSegment.
Definition: StructureTransformer.h:228
static void copy(std::shared_ptr< EvioTagSegment > const &tagsegment, std::shared_ptr< EvioSegment > const &segment)
Copy the data in an EvioSegment object into an existing EvioTagSegment.
Definition: StructureTransformer.h:192
static void copy(std::shared_ptr< EvioBank > const &bank, std::shared_ptr< EvioSegment > const &segment, uint8_t num)
Copy the data in an EvioSegment object into an existing EvioBank.
Definition: StructureTransformer.h:76
static std::shared_ptr< EvioSegment > transform(std::shared_ptr< EvioBank > const &bank)
Create an EvioSegment object from an EvioBank.
Definition: StructureTransformer.h:284
static const DataType BANK
Bank.
Definition: DataType.h:52
This class contains methods to transform structures from one type to another, for example...
Definition: StructureTransformer.h:38
static std::shared_ptr< EvioTagSegment > getInstance(std::shared_ptr< TagSegmentHeader > const &head)
Method to return a shared pointer to a constructed object of this class.
Definition: EvioTagSegment.h:53
static void copy(std::shared_ptr< EvioSegment > const &segment, std::shared_ptr< EvioSegment > const &tagsegment)
Copy the data in an EvioTagSegment object into an existing EvioSegment.
Definition: StructureTransformer.h:252
static const DataType ALSOSEGMENT
Segment alternate value.
Definition: DataType.h:49
static void copy(std::shared_ptr< EvioBank > const &bank, std::shared_ptr< EvioTagSegment > const &tagsegment, uint8_t num)
Copy the data in an EvioTagSegment object into an existing EvioBank.
Definition: StructureTransformer.h:129
Numerical values associated with evio data types.
Definition: DataType.h:32
static std::shared_ptr< EvioBank > getInstance()
Method to return a shared pointer to a constructed object of this class.
Definition: EvioBank.h:57
static void copy(std::shared_ptr< EvioSegment > const &segment, std::shared_ptr< EvioBank > const &bank)
Copy the data in an EvioBank object into an existing EvioSegment.
Definition: StructureTransformer.h:321
static void copy(std::shared_ptr< EvioTagSegment > const &tagsegment, std::shared_ptr< EvioBank > const &bank)
Copy the data in an EvioBank object into an existing EvioTagSegment.
Definition: StructureTransformer.h:409
static std::shared_ptr< EvioTagSegment > transform(std::shared_ptr< EvioBank > const &bank, int dummy)
Create an EvioTagSegment object from an EvioBank.
Definition: StructureTransformer.h:364
static std::shared_ptr< EvioBank > transform(std::shared_ptr< EvioSegment > const &segment, uint8_t num)
Create an EvioBank object from an EvioSegment.
Definition: StructureTransformer.h:54
static std::shared_ptr< EvioBank > transform(std::shared_ptr< EvioTagSegment > const &tagsegment, uint8_t num)
Create an EvioBank object from an EvioTagSegment.
Definition: StructureTransformer.h:107