KaliVeda
Toolkit for HIC analysis
KVFileReader Class Reference

Detailed Description

Handle reading columns of numeric data in text files.

Author
Eric Bonnet (ebonn.nosp@m.et@s.nosp@m.ubate.nosp@m.ch.i.nosp@m.n2p3..nosp@m.fr)
Date
Tue Jul 13 11:52:58 2010

This class can be used to read any text file containing numeric data in columns such as

0.1 19 3.56 2
0.2 29 3.66 1
0.3 39 3.26 -1

or

0.1,19,3.56,2
0.2,29,3.66,1
0.3,39,3.26,-1

i.e. columns of data separated by whitespace, commas, tabs, or any other separator character

Comment lines will be ignored if the signature for the beginning of a comment is given to the constructor, e.g.

KVFileReader data_file("#"); // comment lines begin with '#'
KVFileReader data_file2("//"); // comment lines begin with '//'
Handle reading columns of numeric data in text files.
Definition: KVFileReader.h:121

To open the file to be read, give the full path (shell characters & environment variables can be used) to method OpenFileToRead():

auto ok = data_file.OpenFileToRead("$SOME_ENV/my_file.dat");
if( ok )
{
[file was opened successfully]
}

Each line of the file can then be read in using one of the following methods:

ReadLine(const KVString& pattern = "")
ReadLineAndAdd(const KVString& pattern = "")
ReadLineAndCheck(Int_t nexpect, const KVString& pattern)
ReuseLineAndCheck(Int_t nexpect, const KVString& pattern)
int Int_t
ReadStatus ReadLineAndCheck(Int_t nexpect, const KVString &pattern)
Definition: KVFileReader.h:279
ReadStatus ReadLine(const KVString &pattern="")
Definition: KVFileReader.h:243
ReadStatus ReadLineAndAdd(const KVString &pattern="")
Definition: KVFileReader.h:261
ReadStatus ReuseLineAndCheck(Int_t nexpect, const KVString &pattern)
Definition: KVFileReader.h:304
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:73

The values of the parameters which were read, if any, can then be retrieved using

double Double_t
Double_t GetDoubleReadPar(Int_t pos) const
Definition: KVFileReader.h:334
Int_t GetIntReadPar(Int_t pos) const
Definition: KVFileReader.h:338
KVString GetReadPar(Int_t pos) const
Definition: KVFileReader.h:342

Assuming we are reading either of the files given above, the following examples illustrate usage:

p.OpenFileToRead("data.txt");
while (p.IsOK()) {
if (p.ReadLine(" \t,") == KVFileReader::ReadStatus::OK) {
std::cout << p.GetDoubleReadPar(0) << " " << p.GetIntReadPar(1) << " " << p.GetDoubleReadPar(2) << " " << p.GetIntReadPar(3) << std::endl;
}
}
winID h TVirtualViewer3D TVirtualGLPainter p
@ OK
successful read and import of parameters from line

will give the following output:

0.1 19 3.56 2
0.2 29 3.66 1
0.3 39 3.26 -1

If you want to check the right number of parameters is being read:

p.OpenFileToRead("data.txt");
while (p.IsOK()) {
if (p.ReadLineAndCheck(4," \t,") == KVFileReader::ReadStatus::OK) {
std::cout << p.GetDoubleReadPar(0) << " " << p.GetIntReadPar(1) << " " << p.GetDoubleReadPar(2) << " " << p.GetIntReadPar(3) << std::endl;
}
}

will give the following output:

0.1 19 3.56 2
0.2 29 3.66 1
0.3 39 3.26 -1

And finally if you want to cumulate several lines of parameters into a single list:

p.OpenFileToRead("data.txt");
while (p.ReadLineAndAdd(" \t,") == KVFileReader::ReadStatus::OK) ;
for(int i=0; i<p.GetNparRead(); ++i){
std::cout << i << ":" << p.GetReadPar(i) << " ";
if(!((i+1)%4)) std::cout << std::endl;
}

will give the following output:

0:0.1 1:19 2:3.56 3:2
4:0.2 5:29 6:3.66 7:1
8:0.3 9:39 10:3.26 11:-1

Definition at line 121 of file KVFileReader.h.

#include <KVFileReader.h>

Inheritance diagram for KVFileReader:

Public Types

enum class  ReadStatus {
  EmptyLine , OK , ParamMismatch , CommentLine ,
  EndOfFile
}
 status returned by each method used to read a line in the file More...
 
- Public Types inherited from KVBase
enum  EKaliVedaBits { kIsKaliVedaObject = BIT(23) }
 
- Public Types inherited from TObject
enum  EDeprecatedStatusBits
 
enum  EStatusBits
 

Public Member Functions

 KVFileReader (const KVString &comments="")
 
void Clear (Option_t *="") override
 
void CloseFile ()
 
KVString GetCurrentLine ()
 
Double_t GetDoubleReadPar (Int_t pos) const
 
KVString GetFileName ()
 
Int_t GetIntReadPar (Int_t pos) const
 
Int_t GetNlineRead () const
 
Int_t GetNparRead () const
 
KVString GetReadPar (Int_t pos) const
 
KVString GetReadStatus (ReadStatus s)
 
Bool_t IsOK ()
 
Bool_t OpenFileToRead (const KVString &filename)
 
Bool_t PreparForReadingAgain ()
 
ReadStatus ReadLine (const KVString &pattern="")
 
ReadStatus ReadLineAndAdd (const KVString &pattern="")
 
ReadStatus ReadLineAndCheck (Int_t nexpect, const KVString &pattern)
 
ReadStatus ReuseLineAndCheck (Int_t nexpect, const KVString &pattern)
 
- Public Member Functions inherited from KVBase
 KVBase ()
 Default constructor. More...
 
 KVBase (const Char_t *name, const Char_t *title="")
 Ctor for object with given name and type. More...
 
 KVBase (const KVBase &)
 copy ctor More...
 
void Copy (TObject &) const override
 Make a copy of this object. More...
 
const Char_tGetLabel () const
 
UInt_t GetNumber () const
 
virtual TObjectGetObject () const
 
virtual const Char_tGetType () const
 
Bool_t HasLabel () const
 
virtual Bool_t IsCalled (const Char_t *name) const
 
Bool_t IsLabelled (const Char_t *l) const
 
virtual Bool_t IsType (const Char_t *typ) const
 
virtual void List ()
 
KVBaseoperator= (const KVBase &)
 copy assignment operator More...
 
void Print (Option_t *option="") const override
 
Double_t ProtectedGetX (const TF1 *func, Double_t val, int &status, Double_t xmin=0.0, Double_t xmax=0.0) const
 
void SetLabel (const Char_t *lab)
 
virtual void SetNumber (UInt_t num)
 
virtual void SetType (const Char_t *str)
 
- Public Member Functions inherited from TNamed
 TNamed ()
 
 TNamed (const char *name, const char *title)
 
 TNamed (const TNamed &named)
 
 TNamed (const TString &name, const TString &title)
 
virtual ~TNamed ()
 
TObjectClone (const char *newname="") const override
 
Int_t Compare (const TObject *obj) const override
 
virtual void FillBuffer (char *&buffer)
 
const char * GetName () const override
 
const char * GetTitle () const override
 
ULong_t Hash () const override
 
TClassIsA () const override
 
Bool_t IsSortable () const override
 
void ls (Option_t *option="") const override
 
TNamedoperator= (const TNamed &rhs)
 
virtual void SetName (const char *name)
 
virtual void SetNameTitle (const char *name, const char *title)
 
virtual void SetTitle (const char *title="")
 
virtual Int_t Sizeof () const
 
void Streamer (TBuffer &) override
 
void StreamerNVirtual (TBuffer &ClassDef_StreamerNVirtual_b)
 
- Public Member Functions inherited from TObject
 TObject ()
 
 TObject (const TObject &object)
 
virtual ~TObject ()
 
void AbstractMethod (const char *method) const
 
virtual void AppendPad (Option_t *option="")
 
virtual void Browse (TBrowser *b)
 
ULong_t CheckedHash ()
 
virtual const char * ClassName () const
 
virtual void Delete (Option_t *option="")
 
virtual Int_t DistancetoPrimitive (Int_t px, Int_t py)
 
virtual void Draw (Option_t *option="")
 
virtual void DrawClass () const
 
virtual TObjectDrawClone (Option_t *option="") const
 
virtual void Dump () const
 
virtual void Error (const char *method, const char *msgfmt,...) const
 
virtual void Execute (const char *method, const char *params, Int_t *error=nullptr)
 
virtual void Execute (TMethod *method, TObjArray *params, Int_t *error=nullptr)
 
virtual void ExecuteEvent (Int_t event, Int_t px, Int_t py)
 
virtual void Fatal (const char *method, const char *msgfmt,...) const
 
virtual TObjectFindObject (const char *name) const
 
virtual TObjectFindObject (const TObject *obj) const
 
virtual Option_tGetDrawOption () const
 
virtual const char * GetIconName () const
 
virtual char * GetObjectInfo (Int_t px, Int_t py) const
 
virtual Option_tGetOption () const
 
virtual UInt_t GetUniqueID () const
 
virtual Bool_t HandleTimer (TTimer *timer)
 
Bool_t HasInconsistentHash () const
 
virtual void Info (const char *method, const char *msgfmt,...) const
 
virtual Bool_t InheritsFrom (const char *classname) const
 
virtual Bool_t InheritsFrom (const TClass *cl) const
 
virtual void Inspect () const
 
void InvertBit (UInt_t f)
 
Bool_t IsDestructed () const
 
virtual Bool_t IsEqual (const TObject *obj) const
 
virtual Bool_t IsFolder () const
 
R__ALWAYS_INLINE Bool_t IsOnHeap () const
 
R__ALWAYS_INLINE Bool_t IsZombie () const
 
void MayNotUse (const char *method) const
 
virtual Bool_t Notify ()
 
void Obsolete (const char *method, const char *asOfVers, const char *removedFromVers) const
 
void operator delete (void *ptr)
 
void operator delete (void *ptr, void *vp)
 
void operator delete[] (void *ptr)
 
void operator delete[] (void *ptr, void *vp)
 
voidoperator new (size_t sz)
 
voidoperator new (size_t sz, void *vp)
 
voidoperator new[] (size_t sz)
 
voidoperator new[] (size_t sz, void *vp)
 
TObjectoperator= (const TObject &rhs)
 
virtual void Paint (Option_t *option="")
 
virtual void Pop ()
 
virtual Int_t Read (const char *name)
 
virtual void RecursiveRemove (TObject *obj)
 
void ResetBit (UInt_t f)
 
virtual void SaveAs (const char *filename="", Option_t *option="") const
 
virtual void SavePrimitive (std::ostream &out, Option_t *option="")
 
void SetBit (UInt_t f)
 
void SetBit (UInt_t f, Bool_t set)
 
virtual void SetDrawOption (Option_t *option="")
 
virtual void SetUniqueID (UInt_t uid)
 
void StreamerNVirtual (TBuffer &ClassDef_StreamerNVirtual_b)
 
virtual void SysError (const char *method, const char *msgfmt,...) const
 
R__ALWAYS_INLINE Bool_t TestBit (UInt_t f) const
 
Int_t TestBits (UInt_t f) const
 
virtual void UseCurrentStyle ()
 
virtual void Warning (const char *method, const char *msgfmt,...) const
 
virtual Int_t Write (const char *name=nullptr, Int_t option=0, Int_t bufsize=0)
 
virtual Int_t Write (const char *name=nullptr, Int_t option=0, Int_t bufsize=0) const
 

Public Attributes

std::ifstream f_in
 
- Public Attributes inherited from TObject
 kBitMask
 
 kCanDelete
 
 kCannotPick
 
 kHasUUID
 
 kInconsistent
 
 kInvalidObject
 
 kIsOnHeap
 
 kIsReferenced
 
 kMustCleanup
 
 kNoContextMenu
 
 kNotDeleted
 
 kObjInCanvas
 
 kOverwrite
 
 kSingleKey
 
 kWriteDelete
 
 kZombie
 

Private Member Functions

void AddParameters (const KVString &pattern)
 
void init ()
 
void StoreParameters (const KVString &pattern)
 

Private Attributes

KVString comment_string
 
KVString file_name
 
std::vector< std::string > items
 
Int_t nline
 
KVString reading_line
 
Bool_t skip_comments = false
 
Bool_t status
 

Additional Inherited Members

- Static Public Member Functions inherited from KVBase
static Bool_t AreEqual (Double_t x, Double_t y, Long64_t maxdif=1)
 Comparison between two 64-bit floating-point values. More...
 
static void BackupFileWithDate (const Char_t *path)
 
static void CombineFiles (const Char_t *file1, const Char_t *file2, const Char_t *newfilename, Bool_t keep=kTRUE)
 
static void Deprecated (const char *method, const char *advice)
 
static Bool_t FindClassSourceFiles (const KVString &class_name, KVString &imp_file, KVString &dec_file, const KVString &dir_name=".")
 
static Bool_t FindExecutable (TString &exec, const Char_t *path="$(PATH)")
 
static const Char_tFindFile (const Char_t *search, TString &wfil)
 
static const Char_tGetBINDIRFilePath (const Char_t *namefile="")
 
static const Char_tGetDATABASEFilePath ()
 
static const Char_tGetDATADIRFilePath (const Char_t *namefile="")
 
static KVString GetDataSetEnv (const KVString &dataset, const KVString &type, const char *defval)
 
template<typename ValType >
static ValType GetDataSetEnv (const KVString &dataset, const KVString &type, const ValType &defval)
 
static const Char_tGetETCDIRFilePath (const Char_t *namefile="")
 
static const Char_tGetExampleFilePath (const Char_t *library, const Char_t *namefile)
 Return full path to example file for given library (="KVMultiDet", "BackTrack", etc.) More...
 
static const Char_tGetINCDIRFilePath (const Char_t *namefile="")
 
static const Char_tGetKVBuildDate ()
 Returns KaliVeda build date. More...
 
static const Char_tGetKVBuildDir ()
 Returns top-level directory used for build. More...
 
static const Char_tGetKVBuildTime ()
 Returns KaliVeda build time. More...
 
static const Char_tGetKVBuildType ()
 Returns KaliVeda build type (cmake build: Release, Debug, RelWithDebInfo, ...) More...
 
static const Char_tGetKVBuildUser ()
 Returns username of person who performed build. More...
 
static int GetKVMajorVersion ()
 
static int GetKVMinorVersion ()
 
static const Char_tGetKVSourceDir ()
 Returns top-level directory of source tree used for build. More...
 
static const Char_tGetKVVersion ()
 Returns KaliVeda version string. More...
 
static const Char_tGetLIBDIRFilePath (const Char_t *namefile="")
 
static const Char_tGetListOfPlugins (const Char_t *base)
 
static const Char_tGetListOfPluginURIs (const Char_t *base)
 
static const Char_tGetPluginURI (const Char_t *base, const Char_t *plugin)
 
static void GetTempFileName (TString &base)
 
static const Char_tGetTEMPLATEDIRFilePath (const Char_t *namefile="")
 
static const Char_tGetWORKDIRFilePath (const Char_t *namefile="")
 
static const Char_tgitBranch ()
 Returns git branch of sources. More...
 
static const Char_tgitCommit ()
 Returns last git commit of sources. More...
 
static void InitEnvironment ()
 
static bool is_gnuinstall ()
 
static Bool_t IsEnvInit ()
 
static Bool_t IsThisAPlugin (const TString &uri, TString &base)
 
static TPluginHandlerLoadPlugin (const Char_t *base, const Char_t *uri="0")
 
static Bool_t OpenContextMenu (const char *method, TObject *obj, const char *alt_method_name="")
 
static void OpenTempFile (TString &base, std::ofstream &fp)
 
static void PrintSplashScreen ()
 Prints welcome message and infos on version etc. More...
 
static Bool_t SearchAndOpenKVFile (const Char_t *name, KVSQLite::database &dbfile, const Char_t *kvsubdir="")
 
static Bool_t SearchAndOpenKVFile (const Char_t *name, std::ifstream &file, const Char_t *kvsubdir="", KVLockfile *locks=0)
 
static Bool_t SearchAndOpenKVFile (const Char_t *name, std::ofstream &file, const Char_t *kvsubdir="", KVLockfile *locks=0)
 
static Bool_t SearchKVFile (const Char_t *name, TString &fullpath, const Char_t *kvsubdir="")
 
static const Char_tWorkingDirectory ()
 
- Static Public Member Functions inherited from TNamed
static TClassClass ()
 
static const char * Class_Name ()
 
static constexpr Version_t Class_Version ()
 
static const char * DeclFileName ()
 
- Static Public Member Functions inherited from TObject
static TClassClass ()
 
static const char * Class_Name ()
 
static constexpr Version_t Class_Version ()
 
static const char * DeclFileName ()
 
static Longptr_t GetDtorOnly ()
 
static Bool_t GetObjectStat ()
 
static void SetDtorOnly (void *obj)
 
static void SetObjectStat (Bool_t stat)
 
- Protected Member Functions inherited from TObject
virtual void DoError (int level, const char *location, const char *fmt, va_list va) const
 
void MakeZombie ()
 
- Protected Attributes inherited from TNamed
TString fName
 
TString fTitle
 
- Protected Attributes inherited from TObject
 kOnlyPrepStep
 

Member Enumeration Documentation

◆ ReadStatus

status returned by each method used to read a line in the file

Enumerator
EmptyLine 

last line read was empty (only whitespace)

OK 

successful read and import of parameters from line

ParamMismatch 

the number of parameters read from line does not correspond to expectations

CommentLine 

last line read was a comment line

EndOfFile 

end of file reached

Definition at line 159 of file KVFileReader.h.

Constructor & Destructor Documentation

◆ KVFileReader()

KVFileReader::KVFileReader ( const KVString comments = "")
inline
Parameters
[in]commentsif given, any lines beginning with the given string will be ignored

Definition at line 183 of file KVFileReader.h.

Member Function Documentation

◆ AddParameters()

void KVFileReader::AddParameters ( const KVString pattern)
inlineprivate

Add values of columnar data in last read line according to delimiter(s) in pattern to list of values read from a previous line with either StoreParameters() or AddParameters()

Definition at line 137 of file KVFileReader.h.

◆ Clear()

void KVFileReader::Clear ( Option_t = "")
inlineoverridevirtual

Reimplemented from KVBase.

Definition at line 195 of file KVFileReader.h.

◆ CloseFile()

void KVFileReader::CloseFile ( )
inline

Close file if it is open

Definition at line 237 of file KVFileReader.h.

◆ GetCurrentLine()

KVString KVFileReader::GetCurrentLine ( )
inline

Definition at line 320 of file KVFileReader.h.

◆ GetDoubleReadPar()

Double_t KVFileReader::GetDoubleReadPar ( Int_t  pos) const
inline

Definition at line 334 of file KVFileReader.h.

◆ GetFileName()

KVString KVFileReader::GetFileName ( )
inline

Definition at line 190 of file KVFileReader.h.

◆ GetIntReadPar()

Int_t KVFileReader::GetIntReadPar ( Int_t  pos) const
inline

Definition at line 338 of file KVFileReader.h.

◆ GetNlineRead()

Int_t KVFileReader::GetNlineRead ( ) const
inline

Definition at line 329 of file KVFileReader.h.

◆ GetNparRead()

Int_t KVFileReader::GetNparRead ( ) const
inline

Definition at line 325 of file KVFileReader.h.

◆ GetReadPar()

KVString KVFileReader::GetReadPar ( Int_t  pos) const
inline

Definition at line 342 of file KVFileReader.h.

◆ GetReadStatus()

KVString KVFileReader::GetReadStatus ( ReadStatus  s)
inline

Definition at line 167 of file KVFileReader.h.

◆ init()

void KVFileReader::init ( void  )
inlineprivate

Definition at line 123 of file KVFileReader.h.

◆ IsOK()

Bool_t KVFileReader::IsOK ( )
inline
Returns
true if file is open and ready for reading

Definition at line 231 of file KVFileReader.h.

◆ OpenFileToRead()

Bool_t KVFileReader::OpenFileToRead ( const KVString filename)
inline
Parameters
[in]filenamename or full path to file to be read. shell characters, environment variables are accepted.

Open file for reading

Returns
true if file is open for reading, false in case of a problem opening the file

Definition at line 210 of file KVFileReader.h.

◆ PreparForReadingAgain()

Bool_t KVFileReader::PreparForReadingAgain ( )
inline

Close current file, reset file reader, then reopen file for reading

Returns
true if file is open for reading, false in case of a problem opening the file

Definition at line 200 of file KVFileReader.h.

◆ ReadLine()

ReadStatus KVFileReader::ReadLine ( const KVString pattern = "")
inline

Read the next line from the file, store any parameter values read

Returns
status of file after reading
Parameters
[in]patternif given, contains separator(s) to be used to separate & store values of columnar data (see KVString::Begin())

Definition at line 243 of file KVFileReader.h.

◆ ReadLineAndAdd()

ReadStatus KVFileReader::ReadLineAndAdd ( const KVString pattern = "")
inline

Read the next line from the file, add any parameter values read to those already stored

Returns
status of file after reading
Parameters
[in]patternif given, contains separator(s) to be used to separate & store values of columnar data (see KVString::Begin())

Definition at line 261 of file KVFileReader.h.

◆ ReadLineAndCheck()

ReadStatus KVFileReader::ReadLineAndCheck ( Int_t  nexpect,
const KVString pattern 
)
inline

Read the next line from the file, store any parameter values read

Returns
status of file after reading, notably KVFileReader::ReadStatus::ParamMismatch if incorrect number of values is found
Parameters
[in]patterncontains separator(s) to be used to separate & store values of columnar data (see KVString::Begin())
[in]nexpextthe expected number of items of columnar data in the line

Definition at line 279 of file KVFileReader.h.

◆ ReuseLineAndCheck()

ReadStatus KVFileReader::ReuseLineAndCheck ( Int_t  nexpect,
const KVString pattern 
)
inline

Same as ReadLineAndCheck() except that instead of reading a new line we re-use the last read line

Returns
status of file after reading, notably KVFileReader::ReadStatus::ParamMismatch if incorrect number of values is found
Parameters
[in]patterncontains separator(s) to be used to separate & store values of columnar data (see KVString::Begin())
[in]nexpextthe expected number of items of columnar data in the line

Definition at line 304 of file KVFileReader.h.

◆ StoreParameters()

void KVFileReader::StoreParameters ( const KVString pattern)
inlineprivate

Store values of columnar data in last read line according to delimiter(s) in pattern

Definition at line 129 of file KVFileReader.h.

Member Data Documentation

◆ comment_string

KVString KVFileReader::comment_string
private

Definition at line 148 of file KVFileReader.h.

◆ f_in

std::ifstream KVFileReader::f_in

Definition at line 154 of file KVFileReader.h.

◆ file_name

KVString KVFileReader::file_name
private

Definition at line 148 of file KVFileReader.h.

◆ items

std::vector<std::string> KVFileReader::items
private

Definition at line 147 of file KVFileReader.h.

◆ nline

Int_t KVFileReader::nline
private

Definition at line 149 of file KVFileReader.h.

◆ reading_line

KVString KVFileReader::reading_line
private

Definition at line 148 of file KVFileReader.h.

◆ skip_comments

Bool_t KVFileReader::skip_comments = false
private

Definition at line 151 of file KVFileReader.h.

◆ status

Bool_t KVFileReader::status
private

Definition at line 150 of file KVFileReader.h.