LDMX Software
File.cxx
1
2#include "Packing/RawDataFile/File.h"
3
4#include "DetDescr/DetectorID.h"
5#include "Packing/Utility/CRC.h"
6#include "Packing/Utility/Mask.h"
7
8namespace packing {
9namespace rawdatafile {
10
12 is_output_ = ps.getParameter<bool>("is_output");
13 skip_unavailable_ = ps.getParameter<bool>("skip_unavailable");
14
15 std::string fn = ps.getParameter<std::string>("filename");
16
17 ecal_object_name_ = ps.getParameter<std::string>("ecal_object_name");
18 hcal_object_name_ = ps.getParameter<std::string>("hcal_object_name");
19 tracker_object_name_ = ps.getParameter<std::string>("tracker_object_name");
21 ps.getParameter<std::string>("triggerpad_object_name");
22 pass_name_ = ps.getParameter<std::string>("pass_name");
23
24 std::cerr << "creating file" << std::endl;
25 if (is_output_) {
26 writer_.open(fn);
27 // leave entry count undefined
28 entries_ = 0;
29 i_entry_ = 0;
30 } else {
31 reader_.open(fn);
32 // get entry count from file
33 // get run id number from file
34 uint32_t word;
35 reader_ >> word;
36
37 uint8_t version = word & utility::mask<4>;
38 if (version != 0) {
39 EXCEPTION_RAISE("RawFileVers", "Unable to handle raw file version " +
40 std::to_string(version));
41 }
42
43 run_ = ((word >> 4) & utility::mask<28>);
44
45 reader_.seek<uint32_t>(-2, std::ios::end);
46 // save EOF in number of 32-bit-width words
47 auto eof{reader_.tell<uint32_t>()};
48 uint32_t crc_read_in;
49 reader_ >> entries_ >> crc_read_in;
50 i_entry_ = 0;
51
52 reader_.seek<uint32_t>(1, std::ios::beg);
53
54 if (ps.getParameter<bool>("verify_checksum")) {
55 utility::CRC crc;
56 for (auto ifile{reader_.tell<uint32_t>()}; ifile < eof; ifile++) {
57 reader_ >> word;
58 crc << word;
59 }
60
61 if (crc.get() != crc_read_in) {
62 EXCEPTION_RAISE("CRCNotOk",
63 "Failure to verify CRC checksum of entire input file.");
64 }
65
66 reader_.seek<uint32_t>(1, std::ios::beg);
67 } // verify checksum of input file
68 } // input or output file
69}
70
72 event_ = &event;
73 return true;
74}
75
77 static std::map<uint16_t, std::string> eid_to_name = {
78 {ldmx::SubdetectorIDType::EID_TRACKER, tracker_object_name_},
79 {ldmx::SubdetectorIDType::EID_TRIGGER_SCINT, triggerpad_object_name_},
80 {ldmx::SubdetectorIDType::EID_ECAL, ecal_object_name_},
81 {ldmx::SubdetectorIDType::EID_HCAL, hcal_object_name_},
82 };
83
84 if (is_output_) {
85 // dump buffers into event packet and write out
86 std::map<uint16_t, std::vector<uint32_t>> the_subsys_data;
87 for (auto const &[id, name] : eid_to_name) {
88 if (skip_unavailable_ and not event_->exists(name, pass_name_)) continue;
89 the_subsys_data[id] = event_->getCollection<uint32_t>(name, pass_name_);
90 }
91
92 EventPacket write_event(event_->getEventNumber(), the_subsys_data);
93 writer_ << write_event;
94 crc_ << write_event;
95 if (!writer_) return false;
96
97 entries_++;
98 i_entry_++;
99 } else {
100 // check for EoF
101 if (i_entry_ + 1 > entries_) return false;
102
103 i_entry_++;
104
105 // read buffers from event packet and add to event bus
106 static EventPacket read_event;
107 reader_ >> read_event;
108 if (!reader_) {
109 // ERROR
110 return false;
111 }
112
113 event_->getEventHeader().setEventNumber(read_event.id());
114
115 for (auto &subsys : read_event.data()) {
116 // construct name if not provided by default EID mappings
117 if (eid_to_name.find(subsys.id()) == eid_to_name.end()) {
118 std::cerr << subsys.id() << " unrecognized electronics ID."
119 << std::endl;
120 eid_to_name[subsys.id()] = "EID" + std::to_string(subsys.id());
121 }
122
123 event_->add(eid_to_name.at(subsys.id()), subsys.data());
124 } // loop over subsystems
125 } // input or output
126 return true;
127}
128
130 if (is_output_) {
131 // use passed run number
132 run_ = header.getRunNumber();
133 // Why cant we just take the header from above?
134 uint32_t tempHeader =
135 (0 & utility::mask<4>)+((run_ & utility::mask<28>) << 4);
136 writer_ << tempHeader;
137 crc_ << tempHeader;
138 } else {
139 // put our read-in run number here
140 header.setIntParameter("raw_run", run_);
141 }
142}
143
145 event_ = nullptr;
146 if (is_output_) {
147 writer_ << entries_;
148 crc_ << entries_;
149 writer_ << crc_.get();
150 }
151}
152
153} // namespace rawdatafile
154} // namespace packing
Implements an event buffer system for storing event data.
Definition Event.h:41
const std::vector< ContentType > & getCollection(const std::string &collectionName, const std::string &passName="") const
Get a collection (std::vector) of objects from the event bus.
Definition Event.h:386
int getEventNumber() const
Get the event number.
Definition Event.h:70
ldmx::EventHeader & getEventHeader()
Get the event header.
Definition Event.h:58
void add(const std::string &collectionName, T &obj)
Adds an object to the event bus.
Definition Event.h:173
bool exists(const std::string &name, const std::string &passName="", bool unique=true) const
Check for the existence of an object or collection with the given name and pass name in the event.
Definition Event.cxx:92
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:27
T getParameter(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:89
void setEventNumber(int eventNumber)
Set the event number.
Run-specific configuration and data stored in its own output TTree alongside the event TTree in the o...
Definition RunHeader.h:54
void setIntParameter(const std::string &name, int value)
Set an int parameter value.
Definition RunHeader.h:164
int getRunNumber() const
Definition RunHeader.h:74
Event Packet structure.
Definition EventPacket.h:17
bool skip_unavailable_
for writing, do we skip subsystems if their raw object is unavailable
Definition File.h:59
void writeRunHeader(ldmx::RunHeader &header)
Write the run header.
Definition File.cxx:129
uint32_t run_
run number corresponding to this file of raw data
Definition File.h:69
std::string tracker_object_name_
the event bus object name of the Tracker encoded data
Definition File.h:53
uint32_t i_entry_
current entry index (may not be same as event number)
Definition File.h:65
framework::Event * event_
handle to the event bus we are reading from or writing to
Definition File.h:67
std::string triggerpad_object_name_
the event bus object name of the Trigger Pad encoded data
Definition File.h:55
uint32_t entries_
number of entries in the file
Definition File.h:63
std::string ecal_object_name_
the event bus object name of the ECal encoded data
Definition File.h:49
utility::Reader reader_
utility class for reading binary data files
Definition File.h:71
utility::Writer writer_
utility class for writing binary data files
Definition File.h:73
bool connect(framework::Event &event)
Connect the passed event bus to this event file.
Definition File.cxx:71
std::string hcal_object_name_
the event bus object name of the HCal encoded data
Definition File.h:51
utility::CRC crc_
crc calculator for output mode
Definition File.h:75
bool is_output_
are we reading or writing?
Definition File.h:47
bool nextEvent()
Load the next event into our connected event bus.
Definition File.cxx:76
void close()
close this file
Definition File.cxx:144
File(const framework::config::Parameters &params)
General file constructor.
Definition File.cxx:11
std::string pass_name_
for writing, we also should be able to specify a pass name
Definition File.h:57
The HGC ROC and FPGA use a CRC checksum to double check that the data transfer has been done correctl...
Definition CRC.h:50
uint32_t get()
Get the calculate checksum from the calculator.
Definition CRC.h:110
void open(const std::string &file_name)
Open a file with this reader.
Definition Reader.h:35
int tell()
Tell us where the reader is.
Definition Reader.h:87
void seek(int off, std::ios_base::seekdir dir=std::ios::beg)
Go ("seek") a specific position in the file.
Definition Reader.h:61
void open(const std::string &file_name)
Open a file with this writer.
Definition Writer.h:35