Logo ROOT   6.10/09
Reference Guide
TBufferMerger.hxx
Go to the documentation of this file.
1 // @(#)root/io:$Id$
2 // Author: Philippe Canal, Witold Pokorski, and Guilherme Amadio
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2017, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TBufferMerger
13 #define ROOT_TBufferMerger
14 
15 #include "TMemFile.h"
16 
17 #include <condition_variable>
18 #include <memory>
19 #include <mutex>
20 #include <queue>
21 #include <thread>
22 
23 class TArrayC;
24 class TBufferFile;
25 
26 namespace ROOT {
27 namespace Experimental {
28 
29 class TBufferMergerFile;
30 
31 /**
32  * \class TBufferMerger TBufferMerger.hxx
33  * \ingroup IO
34  *
35  * TBufferMerger is a class to facilitate writing data in
36  * parallel from multiple threads, while writing to a single
37  * output file. Its purpose is similar to TParallelMergingFile,
38  * but instead of using processes that connect to a network
39  * socket, TBufferMerger uses threads that each write to a
40  * TBufferMergerFile, which in turn push data into a queue
41  * managed by the TBufferMerger.
42  */
43 
45 public:
46  /** Constructor
47  * @param name Output file name
48  * @param option Output file creation options
49  * @param ftitle Output file title
50  * @param compression Output file compression level
51  */
52  TBufferMerger(const char *name, Option_t *option = "RECREATE", Int_t compress = 1);
53 
54  /** Destructor */
55  virtual ~TBufferMerger();
56 
57  /** Returns a TBufferMergerFile to which data can be written.
58  * At the end, all TBufferMergerFiles get merged into the output file.
59  * The user is responsible to "cd" into the file to associate objects
60  * such as histograms or trees to it.
61  *
62  * After the creation of this file, the user must reset the kMustCleanup
63  * bit on any objects attached to it and take care of their deletion, as
64  * there is a possibility that a race condition will happen that causes
65  * a crash if ROOT manages these objects.
66  */
67  std::shared_ptr<TBufferMergerFile> GetFile();
68 
69  friend class TBufferMergerFile;
70 
71 private:
72  /** TBufferMerger has no default constructor */
73  TBufferMerger();
74 
75  /** TBufferMerger has no copy constructor */
77 
78  /** TBufferMerger has no copy operator */
80 
81  void Push(TBufferFile *buffer);
82  void WriteOutputFile();
83 
84  const std::string fName;
85  const std::string fOption;
87  std::mutex fQueueMutex; //< Mutex used to lock fQueue
88  std::condition_variable fDataAvailable; //< Condition variable used to wait for data
89  std::queue<TBufferFile *> fQueue; //< Queue to which data is pushed and merged
90  std::unique_ptr<std::thread> fMergingThread; //< Worker thread that writes to disk
91  std::vector<std::weak_ptr<TBufferMergerFile>> fAttachedFiles; //< Attached files
92 
94 };
95 
96 /**
97  * \class TBufferMerger TBufferMerger.hxx
98  * \ingroup IO
99  *
100  * A TBufferMergerFile is similar to a TMemFile, but when data
101  * is written to it, it is appended to the TBufferMerger queue.
102  * The TBufferMerger merges all data into the output file on disk.
103  */
104 
105 class TBufferMergerFile : public TMemFile {
106 private:
107  TBufferMerger &fMerger; //< TBufferMerger this file is attached to
108 
109  /** Constructor. Can only be called by TBufferMerger.
110  * @param m Merger this file is attached to. */
112 
113  /** TBufferMergerFile has no default constructor. */
115 
116  /** TBufferMergerFile has no copy constructor. */
118 
119  /** TBufferMergerFile has no copy operator */
121 
122  friend class TBufferMerger;
123 
124 public:
125  /** Destructor */
127 
128  using TMemFile::Write;
129 
130  /** Write data into a TBufferFile and append it to TBufferMerger.
131  * @param name Name
132  * @param opt Options
133  * @param bufsize Buffer size
134  * This function must be called before the TBufferMergerFile gets destroyed,
135  * or no data is appended to the TBufferMerger.
136  */
137  virtual Int_t Write(const char *name = nullptr, Int_t opt = 0, Int_t bufsize = 0) override;
138 
140 };
141 
142 } // namespace Experimental
143 } // namespace ROOT
144 
145 #endif
The concrete implementation of TBuffer for writing/reading to/from a ROOT file or socket...
Definition: TBufferFile.h:47
std::vector< std::weak_ptr< TBufferMergerFile > > fAttachedFiles
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
const char Option_t
Definition: RtypesCore.h:62
int Int_t
Definition: RtypesCore.h:41
void Push(TBufferFile *buffer)
A TMemFile is like a normal TFile except that it reads and writes only from memory.
Definition: TMemFile.h:17
std::queue< TBufferFile * > fQueue
#define ClassDef(name, id)
Definition: Rtypes.h:297
TBufferMerger & operator=(const TBufferMerger &)
TBufferMerger has no copy operator.
virtual ~TBufferMerger()
Destructor.
std::condition_variable fDataAvailable
virtual Int_t Write(const char *name=0, Int_t opt=0, Int_t bufsiz=0)
Write memory objects to this file.
Definition: TFile.cxx:2272
TMarker * m
Definition: textangle.C:8
TBufferMerger is a class to facilitate writing data in parallel from multiple threads, while writing to a single output file.
std::unique_ptr< std::thread > fMergingThread
TBufferMerger()
TBufferMerger has no default constructor.
std::shared_ptr< TBufferMergerFile > GetFile()
Returns a TBufferMergerFile to which data can be written.
#define ClassDefOverride(name, id)
Definition: Rtypes.h:301
Array of chars or bytes (8 bits per element).
Definition: TArrayC.h:27