Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 09:13:41

0001 // -*- C++ -*-
0002 //
0003 // This file is part of YODA -- Yet more Objects for Data Analysis
0004 // Copyright (C) 2008-2024 The YODA collaboration (see AUTHORS for details)
0005 //
0006 #ifndef YODA_IO_h
0007 #define YODA_IO_h
0008 
0009 #include "YODA/Writer.h"
0010 #include "YODA/Reader.h"
0011 
0012 namespace YODA {
0013 
0014 
0015   /// @name Writer functions to files (with automatic format detection)
0016   /// @{
0017 
0018   /// Write out object @a ao to file @a filename
0019   inline void write(const std::string& filename, const AnalysisObject& ao, int precision=-1) {
0020     Writer& w = mkWriter(filename);
0021     if (precision > 0) w.setPrecision(precision);
0022     w.write(filename, ao);
0023   }
0024 
0025   /// Write out a collection of objects @a objs to file @a filename.
0026   template <typename RANGE>
0027   inline void write(const std::string& filename, const RANGE& aos, int precision=-1) {
0028     Writer& w = mkWriter(filename);
0029     if (precision > 0) w.setPrecision(precision);
0030     w.write(filename, aos);
0031   }
0032 
0033   /// Write out the objects specified by start iterator @a begin and end
0034   /// iterator @a end to file @a filename.
0035   template <typename AOITER>
0036   inline void write(const std::string& filename, const AOITER& begin, const AOITER& end, int precision=-1) {
0037     Writer& w = mkWriter(filename);
0038     if (precision > 0) w.setPrecision(precision);
0039     w.write(filename, begin, end);
0040   }
0041 
0042   /// @}
0043 
0044 
0045   /// @name Writer functions to streams (with explicit format specification)
0046   /// @{
0047 
0048   /// Write out object @a ao to stream @a os with format @a fmt
0049   inline void write(std::ostream& os, const AnalysisObject& ao, const std::string& fmt, int precision=-1) {
0050     Writer& w = mkWriter(fmt);
0051     if (precision > 0) w.setPrecision(precision);
0052     w.write(os, ao);
0053   }
0054 
0055   /// Write out a collection of objects @a objs to file @a filename.
0056   template <typename RANGE>
0057   inline void write(std::ostream& os, const RANGE& aos, const std::string& fmt, int precision=-1) {
0058     Writer& w = mkWriter(fmt);
0059     if (precision > 0) w.setPrecision(precision);
0060     w.write(os, aos);
0061   }
0062 
0063   /// Write out the objects specified by start iterator @a begin and end
0064   /// iterator @a end to file @a filename.
0065   template <typename AOITER>
0066   inline void write(std::ostream& os, const AOITER& begin, const AOITER& end, const std::string& fmt, int precision=-1) {
0067     Writer& w = mkWriter(fmt);
0068     if (precision > 0) w.setPrecision(precision);
0069     w.write(os, begin, end);
0070   }
0071 
0072   /// @}
0073 
0074 
0075   /// @name Reader functions from files (with automatic format detection)
0076   /// @{
0077 
0078   /// @brief Read in a collection of objects @a objs from file @a filename.
0079   ///
0080   /// This version fills (actually, appends to) a supplied vector, avoiding
0081   /// copying, and is hence CPU efficient. The appropriate format reader will
0082   /// be determined from the filename.
0083   ///
0084   /// @todo Use SFINAE magic to allow ~arbitrary collection<AnalysisObject*> (with push_back()?) to be passed
0085   inline void read(const std::string& filename, std::vector<AnalysisObject*>& aos,
0086                                                 const std::string& match = "",
0087                                                 const std::string& unmatch = "") {
0088     Reader& r = mkReader(filename);
0089     r.read(filename, aos, match, unmatch);
0090   }
0091 
0092   /// @brief Read in a collection of objects from file @a filename.
0093   ///
0094   /// This version returns a vector by value, involving copying, and is hence
0095   /// less CPU efficient than the alternative version where a vector is filled
0096   /// by reference. The appropriate format reader will be determined from the
0097   /// filename.
0098   inline std::vector<AnalysisObject*> read(const std::string& filename,
0099                                            const std::string& match = "",
0100                                            const std::string& unmatch = "") {
0101     std::vector<AnalysisObject*> rtn;
0102     read(filename, rtn, match, unmatch);
0103     return rtn;
0104   }
0105 
0106   /// @}
0107 
0108 
0109   /// @name Reader functions from streams (with explicit format specification)
0110   /// @{
0111 
0112   /// @brief Read in a collection of objects @a objs from stream @a is, expecting format @a fmt.
0113   ///
0114   /// This version fills (actually, appends to) a supplied vector, avoiding
0115   /// copying, and is hence CPU efficient.
0116   ///
0117   /// @todo Use SFINAE magic to allow ~arbitrary collection<AnalysisObject*> (with push_back()?) to be passed
0118   inline void read(std::istream& is, std::vector<AnalysisObject*>& aos, const std::string& fmt,
0119                                      const std::string& match = "", const std::string& unmatch = "") {
0120     Reader& r = mkReader(fmt);
0121     r.read(is, aos, match, unmatch);
0122   }
0123 
0124   /// @brief Read in a collection of objects from stream @a is, expecting format @a fmt.
0125   ///
0126   /// This version returns a vector by value, involving copying, and is hence
0127   /// less CPU efficient than the alternative version where a vector is filled
0128   /// by reference.
0129   inline std::vector<AnalysisObject*> read(std::istream& is, const std::string& fmt,
0130                                                              const std::string& match = "",
0131                                                              const std::string& unmatch = "") {
0132     std::vector<AnalysisObject*> rtn;
0133     read(is, rtn, fmt, match, unmatch);
0134     return rtn;
0135   }
0136 
0137   /// @}
0138 
0139 
0140 }
0141 
0142 #endif