PODIO v00-16-03
An Event-Data-Model Toolkit for High Energy Physics Experiments
Loading...
Searching...
No Matches
ASCIIWriter.h
Go to the documentation of this file.
1#ifndef PODIO_ASCIIWRITER_H
2#define PODIO_ASCIIWRITER_H
3
4#include "podio/EventStore.h"
6
7#include <fstream>
8#include <functional>
9#include <iostream>
10#include <map>
11#include <string>
12#include <vector>
13
14namespace podio {
15
16class CollectionBase;
17
19 virtual void writeCollection(CollectionBase*, std::ostream&) = 0;
20 virtual ~ColWriterBase() = default;
21};
22
23template <class T>
24struct ColWriter : public ColWriterBase {
25 void writeCollection(CollectionBase* c, std::ostream& o) override {
26 T* col = static_cast<T*>(c);
27 o << col->size() << std::endl;
28 o << *col << std::endl;
29 }
30};
31
32typedef std::map<std::string, ColWriterBase*> FunMap;
33
35
36public:
37 ASCIIWriter(const std::string& filename, EventStore* store);
39
40 // non-copyable
41 ASCIIWriter(const ASCIIWriter&) = delete;
43
44 template <typename T>
45 bool registerForWrite(const std::string& name);
46 void writeEvent();
47 void finish();
48
49private:
50 template <typename T>
51 void writeCollection(const std::string& name);
52 // members
53 std::string m_filename;
54 EventStore* m_store;
55
56 std::ofstream* m_file;
57
58 std::vector<CollectionBase*> m_storedCollections{};
59 std::vector<std::string> m_collectionNames{};
60 FunMap m_map{};
61};
62
63template <typename T>
64bool ASCIIWriter::registerForWrite(const std::string& name) {
65 const T* tmp_coll(nullptr);
66 if (!m_store->get(name, tmp_coll)) {
67 std::cerr << "no such collection to write, throw exception." << std::endl;
68 return false;
69 }
70 T* coll = const_cast<T*>(tmp_coll);
71
72 m_storedCollections.emplace_back(coll);
73 m_collectionNames.emplace_back(name);
74 m_map[name] = new ColWriter<T>;
75 return true;
76}
77
78} // namespace podio
79#endif
#define DEPR_EVTSTORE
Definition: Deprecated.h:4
ASCIIWriter(const ASCIIWriter &)=delete
ASCIIWriter & operator=(const ASCIIWriter &)=delete
bool registerForWrite(const std::string &name)
Definition: ASCIIWriter.h:64
bool get(const std::string &name, const T *&collection)
access a collection by name. returns true if successful
Definition: EventStore.h:143
std::map< std::string, ColWriterBase * > FunMap
Definition: ASCIIWriter.h:32
void writeCollection()
virtual void writeCollection(CollectionBase *, std::ostream &)=0
virtual ~ColWriterBase()=default
void writeCollection(CollectionBase *c, std::ostream &o) override
Definition: ASCIIWriter.h:25