FileWriter.h 2.94 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/**
 * @file
 * @brief Definition of ROOT data file writer module
 * @copyright Copyright (c) 2019 CERN and the Corryvreckan authors.
 * This software is distributed under the terms of the MIT License, copied verbatim in the file "LICENSE.md".
 * In applying this license, CERN does not waive the privileges and immunities granted to it by virtue of its status as an
 * Intergovernmental Organization or submit itself to any jurisdiction.
 */

#include <map>
#include <string>
12

13
14
#include <TFile.h>
#include <TTree.h>
15

16
#include "core/module/Module.hpp"
17

18
namespace corryvreckan {
19
20
21
    /**
     * @ingroup Modules
     * @brief Module to write object data to ROOT trees in file for persistent storage
22
     * @remarks The implementation of this module is based on the ROOTObjectWriter module of the Allpix Squared project
23
24
25
26
     *
     * Reads the whole clipboard. Creates a tree as soon as a new type of object is encountered and
     * saves the data in those objects to tree for every event. The tree name is the class name of the object. A separate
     * branch is created for every combination of detector name and message name that outputs this object.
Simon Spannagel's avatar
Simon Spannagel committed
27
     */
28
    class FileWriter : public Module {
Simon Spannagel's avatar
Simon Spannagel committed
29
    public:
30
31
32
33
34
        /**
         * @brief Constructor for this global module
         * @param config Configuration object for this module as retrieved from the steering file
         * @param detectors List of detectors to perform task on
         */
35
        FileWriter(Configuration config, std::vector<std::shared_ptr<Detector>> detectors);
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

        /**
         * @brief Destructor deletes the internal objects used to build the ROOT Tree
         */
        ~FileWriter() override;

        /**
         * @brief Opens the file to write the objects to
         */
        void initialise() override;

        /**
         * @brief Writes the objects fetched to their specific tree, constructing trees on the fly for new objects.
         */
        StatusCode run(std::shared_ptr<Clipboard> clipboard) override;

        /**
         * @brief Finalize file writing, provide statistics information
         */
        void finalise() override;

    private:
        // Object names to include or exclude from writing
        std::set<std::string> include_;
        std::set<std::string> exclude_;

        // Output data file to write
        std::unique_ptr<TFile> output_file_;
        std::string output_file_name_{};

        // List of trees that are stored in data file
        std::map<std::string, std::unique_ptr<TTree>> trees_;
        std::unique_ptr<TTree> event_tree_;
69
        Event* event_{};
70

71
72
73
        // Last event processed
        unsigned int last_event_{0};

74
75
76
77
78
        // List of objects of a particular type, bound to a specific detector and having a particular name
        std::map<std::tuple<std::type_index, std::string>, std::vector<Object*>*> write_list_;

        // Statistical information about number of objects
        unsigned long write_cnt_{};
Simon Spannagel's avatar
Simon Spannagel committed
79
    };
80
} // namespace corryvreckan