Project
Loading...
Searching...
No Matches
O2HitMerger.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
13
14#ifndef ALICEO2_DEVICES_HITMERGER_H_
15#define ALICEO2_DEVICES_HITMERGER_H_
16
17#include <atomic>
18#include <functional>
19#include <list>
20#include <map>
21#include <memory>
22#include <string>
23#include <thread>
24#include <vector>
25#include <fairmq/Device.h>
26#include <fairmq/Parts.h>
27#include <TStopwatch.h>
28#include <tbb/concurrent_unordered_map.h>
30#include <SimConfig/SimConfig.h>
35
36class TFile;
37class TTree;
38
39namespace o2
40{
41namespace devices
42{
43
45{
46 public:
49
51 ~O2HitMerger() override;
52
53 private:
55 void InitTask() final;
56
57 bool setWorkingDirectory(std::string const& dir);
58
59 // function for intermediate/on-the-fly reinitializations
60 bool ReInit(o2::conf::SimReconfigData const& reconfig);
61
62 template <typename T, typename V>
63 V insertAdd(std::map<T, V>& m, T const& key, V value);
64
65 template <typename T>
66 bool isDataComplete(T checksum, T nparts);
67
68 void consumeHits(int eventID, fair::mq::Parts& data, int& index);
69
70 template <typename T, typename BT>
71 void consumeData(int eventID, fair::mq::Parts& data, int& index, BT& buffer);
72
73 // fills a special branch of SubEventInfos in order to keep
74 // track of which entry corresponds to which event etc.
75 // also creates the MCEventHeader branch expected for physics analysis
76 void fillSubEventInfoEntry(o2::data::SubEventInfo& info);
77
78 bool waitForControlInput();
79
80 bool ConditionalRun() override;
81
82 bool handleSimData(fair::mq::Parts& data, int /*index*/);
83
84 // releases the buffered data of an event once it is flushed or discarded
85 void cleanEvent(int eventID);
86
87 template <typename T>
88 void backInsert(T const& from, T& to);
89
90 void reorderAndMergeMCTracks(int eventID, TTree* target, const std::vector<int>& nprimaries, const std::vector<int>& nsubevents, std::function<void(std::vector<MCTrack> const&)> tracks_analysis_hook, o2::dataformats::MCEventHeader const* mceventheader);
91
92 template <typename T, typename M>
93 void remapTrackIdsAndMerge(std::string brname, int eventID, TTree& target,
94 const std::vector<int>& trackoffsets, const std::vector<int>& nprimaries, const std::vector<int>& subevOrdered, M& mapOfVectorOfTs);
95
96 void updateTrackIdWithOffset(MCTrack& track, Int_t nprim, Int_t idelta0, Int_t idelta1);
97
98 void updateTrackIdWithOffset(TrackReference& ref, Int_t nprim, Int_t idelta0, Int_t idelta1);
99
100 void initHitTreeAndOutFile(std::string prefix, int detID);
101
102 // This method goes over the buffers containing data for a given event; potentially merges
103 // them and flushes into the actual output file.
104 // The method can be called asynchronously to data collection
105 bool mergeAndFlushData();
106
107 std::map<uint32_t, uint32_t> mPartsCheckSum;
108 std::string mOutFileName;
109
110 // structures for the final flush
111 TFile* mOutFile = nullptr;
112 TTree* mOutTree = nullptr;
113 TFile* mMCHeaderOnlyOutFile = nullptr;
114 TTree* mMCHeaderTree = nullptr;
115
116 template <class K, class V>
117 using Hashtable = tbb::concurrent_unordered_map<K, V>;
118 Hashtable<int, TFile*> mDetectorOutFiles;
119 Hashtable<int, TTree*> mDetectorToTTreeMap;
120
121 // intermediate structures to collect data per event
122 std::thread mMergerIOThread;
123 std::atomic<bool> mergingInProgress{false};
124
125 Hashtable<int, std::vector<std::vector<o2::MCTrack>*>> mMCTrackBuffer;
126 Hashtable<int, std::vector<std::vector<o2::TrackReference>*>> mTrackRefBuffer;
127 Hashtable<int, std::list<o2::data::SubEventInfo*>> mSubEventInfoBuffer;
128 Hashtable<int, bool> mFlushableEvents;
129
130 int mEventChecksum = 0;
131 int mNExpectedEvents = 0;
132 int mNextFlushID = 1;
133 TStopwatch mTimer;
134
135 bool mAsService = false;
136 bool mForwardKine = true;
137 bool mWriteToDisc = true;
138
139 int mPipeToDriver = -1;
140
141 std::vector<std::unique_ptr<o2::base::Detector>> mDetectorInstances;
142 std::vector<int> mExternalDetIDs;
143
144 // output folder configuration
145 std::string mInitialOutputDir; // initial output folder of the process (initialized during construction)
146 std::string mCurrentOutputDir; // current output folder asked
147
148 // channel to PUB status messages to outside subscribers
149 fair::mq::Channel mPubChannel;
150
151 // init detector instances
152 void initDetInstances();
153 void initExternalDetInstances();
154 void initHitFiles(std::string prefix);
155};
156
157} // namespace devices
158} // namespace o2
159
160#endif
Definition of the Detector class.
Definition of the MCTrack class.
StringRef key
O2HitMerger()
Default constructor.
~O2HitMerger() override
Default destructor.
const GLfloat * m
Definition glcorearb.h:4066
GLuint buffer
Definition glcorearb.h:655
GLuint index
Definition glcorearb.h:781
GLsizei const GLfloat * value
Definition glcorearb.h:819
GLenum target
Definition glcorearb.h:1641
GLboolean * data
Definition glcorearb.h:298
GLint ref
Definition glcorearb.h:291
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
TODO: Make this a base class of SimConfigData?
Definition SimConfig.h:205