CMS 3D CMS Logo

RootFile.h
Go to the documentation of this file.
1 #ifndef IOPool_Input_RootFile_h
2 #define IOPool_Input_RootFile_h
3 
4 /*----------------------------------------------------------------------
5 
6 RootFile.h // used by ROOT input sources
7 
8 ----------------------------------------------------------------------*/
9 
10 #include "RootTree.h"
14 #include "DataFormats/Provenance/interface/EntryDescriptionID.h" // backward compatibility
16 #include "DataFormats/Provenance/interface/EventEntryDescription.h" // backward compatibility
17 #include "DataFormats/Provenance/interface/EventProcessHistoryID.h" // backward compatibility
27 
28 #include <array>
29 #include <map>
30 #include <memory>
31 #include <string>
32 #include <vector>
33 
34 namespace edm {
35 
36  //------------------------------------------------------------
37  // Class RootFile: supports file reading.
38 
39  class BranchID;
40  class BranchIDListHelper;
41  class ProductProvenanceRetriever;
42  struct DaqProvenanceHelper;
43  class DuplicateChecker;
44  class EventSkipperByID;
45  class ProcessHistoryRegistry;
46  class ProductSelectorRules;
47  class InputFile;
48  class ProvenanceReaderBase;
49  class ProvenanceAdaptor;
50  class RunHelperBase;
52 
53  typedef std::map<EntryDescriptionID, EventEntryDescription> EntryDescriptionMap;
54 
56  public:
57  virtual std::unique_ptr<ProvenanceReaderBase> makeReader(RootTree& eventTree, DaqProvenanceHelper const* daqProvenanceHelper) const = 0;
58  virtual ~MakeProvenanceReader() = default;
59  };
60 
61  class RootFile {
62  public:
63  typedef std::array<RootTree*, NumBranchTypes> RootTreePtrArray;
65  ProcessConfiguration const& processConfiguration,
66  std::string const& logicalFileName,
67  std::shared_ptr<InputFile> filePtr,
68  std::shared_ptr<EventSkipperByID> eventSkipperByID,
69  bool skipAnyEvents,
70  int remainingEvents,
71  int remainingLumis,
72  unsigned int nStreams,
73  unsigned int treeCacheSize,
74  int treeMaxVirtualSize,
75  InputSource::ProcessingMode processingMode,
76  RunHelperBase* runHelper,
77  bool noEventSort,
78  ProductSelectorRules const& productSelectorRules,
79  InputType inputType,
80  std::shared_ptr<BranchIDListHelper> branchIDListHelper,
81  std::shared_ptr<ThinnedAssociationsHelper> thinnedAssociationsHelper,
82  std::vector<BranchID> const* associationsFromSecondary,
83  std::shared_ptr<DuplicateChecker> duplicateChecker,
84  bool dropDescendantsOfDroppedProducts,
85  ProcessHistoryRegistry& processHistoryRegistry,
86  std::vector<std::shared_ptr<IndexIntoFile> > const& indexesIntoFiles,
87  std::vector<std::shared_ptr<IndexIntoFile> >::size_type currentIndexIntoFile,
88  std::vector<ProcessHistoryID>& orderedProcessHistoryIDs,
89  bool bypassVersionCheck,
90  bool labelRawDataLikeMC,
91  bool usingGoToEvent,
92  bool enablePrefetching);
93 
95  ProcessConfiguration const& processConfiguration,
96  std::string const& logicalFileName,
97  std::shared_ptr<InputFile> filePtr,
98  unsigned int nStreams,
99  int treeMaxVirtualSize,
100  InputSource::ProcessingMode processingMode,
101  RunHelperBase* runHelper,
102  ProductSelectorRules const& productSelectorRules,
103  InputType inputType,
104  std::shared_ptr<BranchIDListHelper> branchIDListHelper,
105  std::shared_ptr<ThinnedAssociationsHelper> thinnedAssociationsHelper,
106  std::vector<BranchID> const* associationsFromSecondary,
107  bool dropDescendantsOfDroppedProducts,
108  ProcessHistoryRegistry& processHistoryRegistry,
109  std::vector<std::shared_ptr<IndexIntoFile> > const& indexesIntoFiles,
110  std::vector<std::shared_ptr<IndexIntoFile> >::size_type currentIndexIntoFile,
111  std::vector<ProcessHistoryID>& orderedProcessHistoryIDs,
112  bool bypassVersionCheck,
113  bool labelRawDataLikeMC,
114  bool enablePrefetching) : RootFile(
115  fileName, processConfiguration, logicalFileName, filePtr,
116  nullptr, false, -1, -1, nStreams, 0U, treeMaxVirtualSize,
117  processingMode, runHelper,
118  false, productSelectorRules, inputType, branchIDListHelper,
119  thinnedAssociationsHelper, associationsFromSecondary,
120  nullptr, dropDescendantsOfDroppedProducts, processHistoryRegistry,
121  indexesIntoFiles, currentIndexIntoFile, orderedProcessHistoryIDs,
122  bypassVersionCheck, labelRawDataLikeMC,
123  false, enablePrefetching) {}
124 
126  ProcessConfiguration const& processConfiguration,
127  std::string const& logicalFileName,
128  std::shared_ptr<InputFile> filePtr,
129  unsigned int nStreams,
130  unsigned int treeCacheSize,
131  int treeMaxVirtualSize,
132  RunHelperBase* runHelper,
133  ProductSelectorRules const& productSelectorRules,
134  InputType inputType,
135  ProcessHistoryRegistry& processHistoryRegistry,
136  std::vector<std::shared_ptr<IndexIntoFile> > const& indexesIntoFiles,
137  std::vector<std::shared_ptr<IndexIntoFile> >::size_type currentIndexIntoFile,
138  std::vector<ProcessHistoryID>& orderedProcessHistoryIDs,
139  bool bypassVersionCheck,
140  bool enablePrefetching) : RootFile(
141  fileName, processConfiguration, logicalFileName, filePtr,
142  nullptr, false, -1, -1, nStreams, treeCacheSize, treeMaxVirtualSize,
143  InputSource::RunsLumisAndEvents, runHelper,
144  false, productSelectorRules, inputType, nullptr, nullptr,
145  nullptr, nullptr, false, processHistoryRegistry,
146  indexesIntoFiles, currentIndexIntoFile, orderedProcessHistoryIDs,
147  bypassVersionCheck, false,
148  false, enablePrefetching) {}
149 
150  ~RootFile();
151 
152  RootFile(RootFile const&) = delete; // Disallow copying and moving
153  RootFile& operator=(RootFile const&) = delete; // Disallow copying and moving
154 
155  void reportOpened(std::string const& inputType);
156  void close();
157  bool readCurrentEvent(EventPrincipal& cache);
158  void readEvent(EventPrincipal& cache);
159 
160  std::shared_ptr<LuminosityBlockAuxiliary> readLuminosityBlockAuxiliary_();
161  std::shared_ptr<RunAuxiliary> readRunAuxiliary_();
162  std::shared_ptr<RunAuxiliary> readFakeRunAuxiliary_();
163  void readRun_(RunPrincipal& runPrincipal);
164  void readFakeRun_(RunPrincipal& runPrincipal);
165  void readLuminosityBlock_(LuminosityBlockPrincipal& lumiPrincipal);
166  std::string const& file() const {return file_;}
167  std::shared_ptr<ProductRegistry const> productRegistry() const {return productRegistry_;}
168  EventAuxiliary const& eventAux() const {return eventAux_;}
169  // IndexIntoFile::EntryNumber_t const& entryNumber() const {return indexIntoFileIter().entry();}
170  // LuminosityBlockNumber_t const& luminosityBlockNumber() const {return indexIntoFileIter().lumi();}
171  // RunNumber_t const& runNumber() const {return indexIntoFileIter().run();}
172  EventID const& eventID() const {return eventAux().id();}
173  RootTree const& eventTree() const {return eventTree_;}
174  RootTree const& lumiTree() const {return lumiTree_;}
175  RootTree const& runTree() const {return runTree_;}
176  FileFormatVersion fileFormatVersion() const {return fileFormatVersion_;}
177  int whyNotFastClonable() const {return whyNotFastClonable_;}
178  std::array<bool, NumBranchTypes> const& hasNewlyDroppedBranch() const {return hasNewlyDroppedBranch_;}
179  bool branchListIndexesUnchanged() const {return branchListIndexesUnchanged_;}
180  bool modifiedIDs() const {return daqProvenanceHelper_.get() != 0;}
181  std::unique_ptr<FileBlock> createFileBlock() const;
183  return (event != 0) ? setEntryAtEvent(run, lumi, event) : (lumi ? setEntryAtLumi(run, lumi) : setEntryAtRun(run));
184  }
185  bool containsItem(RunNumber_t run, LuminosityBlockNumber_t lumi, EventNumber_t event) const;
186  bool setEntryAtEvent(RunNumber_t run, LuminosityBlockNumber_t lumi, EventNumber_t event);
187  bool setEntryAtLumi(RunNumber_t run, LuminosityBlockNumber_t lumi);
188  bool setEntryAtRun(RunNumber_t run);
189  bool setEntryAtNextEventInLumi(RunNumber_t run, LuminosityBlockNumber_t lumi);
190  void setAtEventEntry(IndexIntoFile::EntryNumber_t entry);
191 
192  void rewind() {
193  indexIntoFileIter_ = indexIntoFileBegin_;
194  eventTree_.rewind();
195  lumiTree_.rewind();
196  runTree_.rewind();
197  }
198  void setToLastEntry() {
199  indexIntoFileIter_ = indexIntoFileEnd_;
200  }
201 
202  bool skipEntries(unsigned int& offset) {return eventTree_.skipEntries(offset);}
203  bool skipEvents(int& offset);
204  bool goToEvent(EventID const& eventID);
205  bool nextEventEntry() {return eventTree_.next();}
206  IndexIntoFile::EntryType getNextItemType(RunNumber_t& run, LuminosityBlockNumber_t& lumi, EventNumber_t& event);
207  std::shared_ptr<BranchIDListHelper const> branchIDListHelper() const {return get_underlying_safe(branchIDListHelper_);}
208  std::shared_ptr<BranchIDListHelper>& branchIDListHelper() {return get_underlying_safe(branchIDListHelper_);}
209  std::shared_ptr<IndexIntoFile const> indexIntoFileSharedPtr() const {return get_underlying_safe(indexIntoFileSharedPtr_);}
210  std::shared_ptr<IndexIntoFile>& indexIntoFileSharedPtr() {return get_underlying_safe(indexIntoFileSharedPtr_);}
211  bool wasLastEventJustRead() const;
212  bool wasFirstEventJustRead() const;
213  IndexIntoFile::IndexIntoFileItr indexIntoFileIter() const;
214  void setPosition(IndexIntoFile::IndexIntoFileItr const& position);
215  void initAssociationsFromSecondary(std::vector<BranchID> const&);
216 
217  void setSignals(signalslot::Signal<void(StreamContext const&, ModuleCallingContext const&)> const* preEventReadSource,
218  signalslot::Signal<void(StreamContext const&, ModuleCallingContext const&)> const* postEventReadSource);
219  private:
220  RootTreePtrArray& treePointers() {return treePointers_;}
221  bool skipThisEntry();
222  void setIfFastClonable(int remainingEvents, int remainingLumis);
223  void validateFile(InputType inputType, bool usingGoToEvent);
224  void fillIndexIntoFile();
225  bool fillEventAuxiliary(IndexIntoFile::EntryNumber_t entry);
226  void fillThisEventAuxiliary();
227  void fillEventHistory();
228  std::shared_ptr<LuminosityBlockAuxiliary> fillLumiAuxiliary();
229  std::shared_ptr<RunAuxiliary> fillRunAuxiliary();
230  std::string const& newBranchToOldBranch(std::string const& newBranch) const;
231  void markBranchToBeDropped(bool dropDescendants, BranchDescription const& branch, std::set<BranchID>& branchesToDrop, std::map<BranchID, BranchID> const& droppedToKeptAlias) const;
232  void dropOnInput(ProductRegistry& reg, ProductSelectorRules const& rules, bool dropDescendants, InputType inputType);
233  void readParentageTree(InputType inputType);
234  void readEntryDescriptionTree(EntryDescriptionMap& entryDescriptionMap, InputType inputType); // backward compatibility
235  void readEventHistoryTree();
236  bool isDuplicateEvent();
237 
238  void initializeDuplicateChecker(std::vector<std::shared_ptr<IndexIntoFile> > const& indexesIntoFiles,
239  std::vector<std::shared_ptr<IndexIntoFile> >::size_type currentIndexIntoFile);
240 
241  std::unique_ptr<MakeProvenanceReader> makeProvenanceReaderMaker(InputType inputType);
242  std::shared_ptr<ProductProvenanceRetriever> makeProductProvenanceRetriever(unsigned int iStreamIndex);
243 
244  std::shared_ptr<RunAuxiliary const> savedRunAuxiliary() const {return get_underlying_safe(savedRunAuxiliary_);}
245  std::shared_ptr<RunAuxiliary>& savedRunAuxiliary() {return get_underlying_safe(savedRunAuxiliary_);}
246 
247  std::shared_ptr<BranchChildren const> branchChildren() const {return get_underlying_safe(branchChildren_);}
248  std::shared_ptr<BranchChildren>& branchChildren() {return get_underlying_safe(branchChildren_);}
249 
250  std::shared_ptr<ProductProvenanceRetriever const> eventProductProvenanceRetriever(size_t index) const {return get_underlying_safe(eventProductProvenanceRetrievers_[index]);}
251  std::shared_ptr<ProductProvenanceRetriever>& eventProductProvenanceRetriever(size_t index) {return get_underlying_safe(eventProductProvenanceRetrievers_[index]);}
252 
263  std::vector<ProcessHistoryID>& orderedProcessHistoryIDs_;
267  std::vector<EventProcessHistoryID> eventProcessHistoryIDs_; // backward compatibility
268  std::vector<EventProcessHistoryID>::const_iterator eventProcessHistoryIter_; // backward compatibility
273  std::array<bool, NumBranchTypes> hasNewlyDroppedBranch_;
279  RootTreePtrArray treePointers_;
281  std::shared_ptr<ProductRegistry const> productRegistry_;
282  std::shared_ptr<BranchIDLists const> branchIDLists_;
288  std::map<std::string, std::string> newBranchToOldBranch_;
297  std::vector<edm::propagate_const<std::shared_ptr<ProductProvenanceRetriever>>> eventProductProvenanceRetrievers_;
298  std::vector<ParentageID> parentageIDLookup_;
301  }; // class RootFile
302 
303 }
304 #endif
EventID const & eventID() const
Definition: RootFile.h:172
bool setEntryAtItem(RunNumber_t run, LuminosityBlockNumber_t lumi, EventNumber_t event)
Definition: RootFile.h:182
edm::propagate_const< std::unique_ptr< ThinnedAssociationsHelper > > fileThinnedAssociationsHelper_
Definition: RootFile.h:284
InputType
Definition: InputType.h:5
RootTree const & lumiTree() const
Definition: RootFile.h:174
std::shared_ptr< BranchIDLists const > branchIDLists_
Definition: RootFile.h:282
edm::propagate_const< std::shared_ptr< DuplicateChecker > > duplicateChecker_
Definition: RootFile.h:294
std::shared_ptr< BranchIDListHelper const > branchIDListHelper() const
Definition: RootFile.h:207
FileFormatVersion fileFormatVersion() const
Definition: RootFile.h:176
virtual ~MakeProvenanceReader()=default
bool branchListIndexesUnchanged() const
Definition: RootFile.h:179
bool nextEventEntry()
Definition: RootFile.h:205
int whyNotFastClonable_
Definition: RootFile.h:272
edm::propagate_const< std::shared_ptr< EventSkipperByID > > eventSkipperByID_
Definition: RootFile.h:258
edm::propagate_const< std::shared_ptr< ThinnedAssociationsHelper > > thinnedAssociationsHelper_
Definition: RootFile.h:285
std::map< std::string, std::string > newBranchToOldBranch_
Definition: RootFile.h:288
edm::propagate_const< std::shared_ptr< IndexIntoFile > > indexIntoFileSharedPtr_
Definition: RootFile.h:261
RootTree lumiTree_
Definition: RootFile.h:277
edm::propagate_const< ProcessHistoryRegistry * > processHistoryRegistry_
Definition: RootFile.h:256
edm::propagate_const< std::unique_ptr< DaqProvenanceHelper > > daqProvenanceHelper_
Definition: RootFile.h:299
edm::propagate_const< std::shared_ptr< RunAuxiliary > > savedRunAuxiliary_
Definition: RootFile.h:269
unsigned long long EventNumber_t
InputSource::ProcessingMode processingMode_
Definition: RootFile.h:286
IndexIntoFile::IndexIntoFileItr indexIntoFileIter_
Definition: RootFile.h:266
std::shared_ptr< ProductProvenanceRetriever const > eventProductProvenanceRetriever(size_t index) const
Definition: RootFile.h:250
bool skipEntries(unsigned int &offset)
Definition: RootFile.h:202
#define nullptr
std::shared_ptr< IndexIntoFile > & indexIntoFileSharedPtr()
Definition: RootFile.h:210
std::shared_ptr< ProductRegistry const > productRegistry() const
Definition: RootFile.h:167
uint16_t size_type
unsigned int LuminosityBlockNumber_t
long long EntryNumber_t
std::vector< EventSelectionID > EventSelectionIDVector
edm::propagate_const< TClass * > edProductClass_
Definition: RootFile.h:300
FileFormatVersion fileFormatVersion_
Definition: RootFile.h:259
void rewind()
Definition: RootFile.h:192
std::vector< edm::propagate_const< std::shared_ptr< ProductProvenanceRetriever > > > eventProductProvenanceRetrievers_
Definition: RootFile.h:297
std::vector< EventProcessHistoryID >::const_iterator eventProcessHistoryIter_
Definition: RootFile.h:268
void setToLastEntry()
Definition: RootFile.h:198
FileID fid_
Definition: RootFile.h:260
std::string const logicalFile_
Definition: RootFile.h:254
std::vector< BranchListIndex > BranchListIndexes
IndexIntoFile::IndexIntoFileItr indexIntoFileEnd_
Definition: RootFile.h:265
RootFile(std::string const &fileName, ProcessConfiguration const &processConfiguration, std::string const &logicalFileName, std::shared_ptr< InputFile > filePtr, unsigned int nStreams, int treeMaxVirtualSize, InputSource::ProcessingMode processingMode, RunHelperBase *runHelper, ProductSelectorRules const &productSelectorRules, InputType inputType, std::shared_ptr< BranchIDListHelper > branchIDListHelper, std::shared_ptr< ThinnedAssociationsHelper > thinnedAssociationsHelper, std::vector< BranchID > const *associationsFromSecondary, bool dropDescendantsOfDroppedProducts, ProcessHistoryRegistry &processHistoryRegistry, std::vector< std::shared_ptr< IndexIntoFile > > const &indexesIntoFiles, std::vector< std::shared_ptr< IndexIntoFile > >::size_type currentIndexIntoFile, std::vector< ProcessHistoryID > &orderedProcessHistoryIDs, bool bypassVersionCheck, bool labelRawDataLikeMC, bool enablePrefetching)
Definition: RootFile.h:94
bool noEventSort_
Definition: RootFile.h:271
RootTree const & runTree() const
Definition: RootFile.h:175
bool modifiedIDs() const
Definition: RootFile.h:180
IndexIntoFile::EntryNumber_t lastEventEntryNumberRead_
Definition: RootFile.h:280
EventAuxiliary const & eventAux() const
Definition: RootFile.h:168
EventSelectionIDVector eventSelectionIDs_
Definition: RootFile.h:290
int whyNotFastClonable() const
Definition: RootFile.h:177
edm::propagate_const< std::unique_ptr< ProvenanceAdaptor > > provenanceAdaptor_
Definition: RootFile.h:295
std::shared_ptr< ProductProvenanceRetriever > & eventProductProvenanceRetriever(size_t index)
Definition: RootFile.h:251
bool skipAnyEvents_
Definition: RootFile.h:270
RootTree eventTree_
Definition: RootFile.h:276
std::array< bool, NumBranchTypes > const & hasNewlyDroppedBranch() const
Definition: RootFile.h:178
BranchListIndexes branchListIndexes_
Definition: RootFile.h:291
std::shared_ptr< T > & get_underlying_safe(propagate_const< std::shared_ptr< T >> &iP)
def cache(function)
edm::propagate_const< std::unique_ptr< History > > history_
Definition: RootFile.h:292
IndexIntoFile & indexIntoFile_
Definition: RootFile.h:262
std::shared_ptr< BranchIDListHelper > & branchIDListHelper()
Definition: RootFile.h:208
RootTree runTree_
Definition: RootFile.h:278
edm::propagate_const< std::unique_ptr< MakeProvenanceReader > > provenanceReaderMaker_
Definition: RootFile.h:296
std::shared_ptr< RunAuxiliary const > savedRunAuxiliary() const
Definition: RootFile.h:244
std::array< bool, NumBranchTypes > hasNewlyDroppedBranch_
Definition: RootFile.h:273
std::shared_ptr< RunAuxiliary > & savedRunAuxiliary()
Definition: RootFile.h:245
ProcessConfiguration const & processConfiguration_
Definition: RootFile.h:255
std::shared_ptr< ProductRegistry const > productRegistry_
Definition: RootFile.h:281
std::map< EntryDescriptionID, EventEntryDescription > EntryDescriptionMap
Definition: RootFile.h:51
edm::propagate_const< std::shared_ptr< BranchChildren > > branchChildren_
Definition: RootFile.h:293
edm::propagate_const< std::shared_ptr< InputFile > > filePtr_
Definition: RootFile.h:257
std::shared_ptr< BranchChildren const > branchChildren() const
Definition: RootFile.h:247
RootFile(std::string const &fileName, ProcessConfiguration const &processConfiguration, std::string const &logicalFileName, std::shared_ptr< InputFile > filePtr, unsigned int nStreams, unsigned int treeCacheSize, int treeMaxVirtualSize, RunHelperBase *runHelper, ProductSelectorRules const &productSelectorRules, InputType inputType, ProcessHistoryRegistry &processHistoryRegistry, std::vector< std::shared_ptr< IndexIntoFile > > const &indexesIntoFiles, std::vector< std::shared_ptr< IndexIntoFile > >::size_type currentIndexIntoFile, std::vector< ProcessHistoryID > &orderedProcessHistoryIDs, bool bypassVersionCheck, bool enablePrefetching)
Definition: RootFile.h:125
std::shared_ptr< BranchChildren > & branchChildren()
Definition: RootFile.h:248
std::string const & file() const
Definition: RootFile.h:166
IndexIntoFile::IndexIntoFileItr indexIntoFileBegin_
Definition: RootFile.h:264
std::array< RootTree *, NumBranchTypes > RootTreePtrArray
Definition: RootFile.h:63
edm::propagate_const< RunHelperBase * > runHelper_
Definition: RootFile.h:287
bool branchListIndexesUnchanged_
Definition: RootFile.h:274
HLT enums.
edm::propagate_const< TTree * > eventHistoryTree_
Definition: RootFile.h:289
RootTreePtrArray treePointers_
Definition: RootFile.h:279
static int position[264][3]
Definition: ReadPGInfo.cc:509
std::vector< ProcessHistoryID > & orderedProcessHistoryIDs_
Definition: RootFile.h:263
RootTreePtrArray & treePointers()
Definition: RootFile.h:220
std::shared_ptr< IndexIntoFile const > indexIntoFileSharedPtr() const
Definition: RootFile.h:209
unsigned int RunNumber_t
std::vector< ParentageID > parentageIDLookup_
Definition: RootFile.h:298
EventAuxiliary eventAux_
Definition: RootFile.h:275
RootTree const & eventTree() const
Definition: RootFile.h:173
edm::propagate_const< std::shared_ptr< BranchIDListHelper > > branchIDListHelper_
Definition: RootFile.h:283
virtual std::unique_ptr< ProvenanceReaderBase > makeReader(RootTree &eventTree, DaqProvenanceHelper const *daqProvenanceHelper) const =0
std::string const file_
Definition: RootFile.h:253
std::vector< EventProcessHistoryID > eventProcessHistoryIDs_
Definition: RootFile.h:267
Definition: event.py:1