Project
Loading...
Searching...
No Matches
MemFileHelper.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
14
15#ifndef O2_MEMFILE_UTILS_H
16#define O2_MEMFILE_UTILS_H
17
18#include <typeinfo>
19#include <utility>
20#include <TMemFile.h>
21#include <TTree.h>
22#include "Framework/Logger.h"
24
25namespace o2
26{
27namespace utils
28{
29
32
34 using FileImage = std::vector<char>;
35
36 //________________________________________________________________
38 template <typename T>
39 inline static std::string getClassName(const T& obj)
40 {
41 return getClassName(typeid(T));
42 }
43
44 //________________________________________________________________
46 template <typename T>
47 inline static std::unique_ptr<TMemFile> createTMemFile(const T& obj, const std::string& fName, const std::string& optName = "")
48 {
49 return createTMemFile(&obj, typeid(T), fName, optName);
50 }
51
52 //________________________________________________________________
55 static std::unique_ptr<FileImage> createFileImage(const TObject& obj, const std::string& fileName, const std::string& objName)
56 {
57 auto memfUPtr = createTMemFile(obj, fileName, objName);
58 std::unique_ptr<FileImage> img = std::make_unique<FileImage>(memfUPtr->GetSize());
59 auto sz = memfUPtr->CopyTo(img->data(), memfUPtr->GetSize());
60 img->resize(sz);
61 return img;
62 }
63
64 //________________________________________________________________
67 template <typename T>
68 inline static std::unique_ptr<FileImage> createFileImage(const T& obj, const std::string& fileName, const std::string& optName = "")
69 {
70 return createFileImage(&obj, typeid(T), fileName, optName);
71 }
72
73 //________________________________________________________________
75 static std::string getClassName(const std::type_info& tinfo)
76 {
77 auto tcl = TClass::GetClass(tinfo);
78 std::string clname;
79 if (!tcl) {
80 LOG(error) << "Could not retrieve ROOT dictionary for type " << tinfo.name();
81 } else {
82 clname = tcl->GetName();
84 }
85 return clname;
86 }
87
88 //________________________________________________________________
90 static std::unique_ptr<TMemFile> createTMemFile(const TObject& rootObject, const std::string& fName, const std::string& objName)
91 {
92 std::unique_ptr<TMemFile> uptr;
93#if ROOT_VERSION_CODE < ROOT_VERSION(6, 18, 0)
94 uptr = std::make_unique<TMemFile>(fName.c_str(), "RECREATE");
95#else
96 uptr = std::make_unique<TMemFile>(fName.c_str(), "RECREATE", "", ROOT::RCompressionSetting::EDefaults::kUseGeneralPurpose, 1024);
97#endif
98 if (uptr->IsZombie()) {
99 uptr->Close();
100 uptr.reset();
101 throw std::runtime_error(std::string("Error opening memory file ") + fName.c_str());
102 } else {
103 rootObject.Write(objName.c_str());
104 uptr->Close();
105 }
106 return uptr;
107 }
108
109 //________________________________________________________________
111 static std::unique_ptr<TMemFile> createTMemFile(const void* obj, const std::type_info& tinfo, const std::string& fName, const std::string& optName = "")
112 {
113 std::unique_ptr<TMemFile> uptr;
114 auto tcl = TClass::GetClass(tinfo);
115 std::string clsName;
116 if (!tcl) {
117 LOG(error) << "Could not retrieve ROOT dictionary for type " << tinfo.name();
118 return uptr;
119 } else {
120 clsName = tcl->GetName();
121 o2::utils::Str::trim(clsName);
122 }
123 bool isTree = tcl->InheritsFrom("TTree");
124 int compressLevel = isTree ? ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault : ROOT::RCompressionSetting::EDefaults::kUseGeneralPurpose;
125 Long64_t blSize = isTree ? 0LL : 1024LL;
126
127 std::string objName = optName.empty() ? clsName : optName;
128 std::string fileName = fName.empty() ? (objName + ".root") : fName;
129#if ROOT_VERSION_CODE < ROOT_VERSION(6, 18, 0)
130 uptr = std::make_unique<TMemFile>(fileName.c_str(), "RECREATE");
131#else
132 uptr = std::make_unique<TMemFile>(fileName.c_str(), "RECREATE", "", compressLevel, blSize);
133#endif
134 if (uptr->IsZombie()) {
135 uptr->Close();
136 uptr.reset();
137 throw std::runtime_error(std::string("Error opening memory file ") + fileName.c_str());
138 } else {
139 const TTree* treePtr = nullptr;
140 if (isTree) { // trees are special: need to create a new file-resident tree
141 treePtr = const_cast<TTree*>((const TTree*)obj)->CloneTree(-1, "");
142 obj = treePtr;
143 }
144 uptr->WriteObjectAny(obj, clsName.c_str(), objName.c_str());
145 delete treePtr;
146 uptr->Close();
147 }
148 return uptr;
149 }
150
151 //________________________________________________________________
154 static std::unique_ptr<FileImage> createFileImage(const void* obj, const std::type_info& tinfo, const std::string& fileName, const std::string& optName = "")
155 {
156 auto memfUPtr = createTMemFile(obj, tinfo, fileName, optName);
157 std::unique_ptr<FileImage> img = std::make_unique<FileImage>(memfUPtr->GetSize());
158 auto sz = memfUPtr->CopyTo(img->data(), memfUPtr->GetSize());
159 img->resize(sz);
160 return img;
161 }
162
164};
165
166} // namespace utils
167} // namespace o2
168
169#endif
GLint void * img
Definition glcorearb.h:550
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
Common utility functions.
static std::string getClassName(const T &obj)
get the class name of the object
static std::unique_ptr< FileImage > createFileImage(const void *obj, const std::type_info &tinfo, const std::string &fileName, const std::string &optName="")
ClassDefNV(MemFileHelper, 1)
static std::unique_ptr< FileImage > createFileImage(const TObject &obj, const std::string &fileName, const std::string &objName)
static std::string getClassName(const std::type_info &tinfo)
get the class name of the object
static std::unique_ptr< TMemFile > createTMemFile(const T &obj, const std::string &fName, const std::string &optName="")
dump object into the TMemFile named fileName. The stored object will be named according to the optNam...
static std::unique_ptr< TMemFile > createTMemFile(const TObject &rootObject, const std::string &fName, const std::string &objName)
dump TObject the TMemFile named fileName. The stored object will be named according to the optName or...
static std::unique_ptr< TMemFile > createTMemFile(const void *obj, const std::type_info &tinfo, const std::string &fName, const std::string &optName="")
dump object into the TMemFile named fileName. The stored object will be named according to the optNam...
std::vector< char > FileImage
static std::unique_ptr< FileImage > createFileImage(const T &obj, const std::string &fileName, const std::string &optName="")
static void trim(std::string &s)
Definition StringUtils.h:71
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"