ATLAS Offline Software
Loading...
Searching...
No Matches
VP1FileUtilities Class Reference

#include <VP1FileUtilities.h>

Collaboration diagram for VP1FileUtilities:

Public Member Functions

 VP1FileUtilities (const std::string &inputDirectory, unsigned int fileLimit, const std::string &outputDir="", bool forceMakeOutputDir=false, bool removeInputFile=true)
virtual ~VP1FileUtilities ()
void produceNewFile (const std::string &sourceFile, unsigned int runNumber, unsigned long long eventNumber, unsigned int timeStamp, const std::string &textLabel="")

Static Public Member Functions

static bool fileExistsAndReadable (const std::string &)

Private Member Functions

void cleanUp ()

Private Attributes

std::string m_inputDirectory
int m_fileLimit
std::string m_outputDirectory
bool m_forceMakeOutputDir
bool m_removeInputFile

Detailed Description

Definition at line 24 of file VP1FileUtilities.h.

Constructor & Destructor Documentation

◆ VP1FileUtilities()

VP1FileUtilities::VP1FileUtilities ( const std::string & inputDirectory,
unsigned int fileLimit,
const std::string & outputDir = "",
bool forceMakeOutputDir = false,
bool removeInputFile = true )

Definition at line 32 of file VP1FileUtilities.cxx.

33 :
34 m_inputDirectory(inputDirectory),
35 m_fileLimit(fileLimit),
36 m_outputDirectory(outputDir),
37 m_forceMakeOutputDir(forceMakeOutputDir),
38 m_removeInputFile(removeInputFile)
39{
40 // Check if the input directory exists and is writable
41 QFileInfo inpDir(m_inputDirectory.c_str());
42 if(!inpDir.exists()||!inpDir.isDir()||!inpDir.isReadable()||!inpDir.isWritable()) {
43 std::string errMessage = std::string("VP1FileUtilities: ERROR!! The directory ") + inputDirectory + std::string(" either does not exist or is not writable");
44 throw std::runtime_error(errMessage.c_str());
45 }
46
47 // Check if the output directory exists and is writable
48 if (m_outputDirectory != "") {
49 QFileInfo inpDir(m_outputDirectory.c_str());
50 if(!inpDir.exists()||!inpDir.isDir()||!inpDir.isReadable()||!inpDir.isWritable()) {
51 std::string errMessage = std::string("VP1FileUtilities: ERROR!! The directory ") + m_outputDirectory + std::string(" does not exist.");
53 errMessage += "\nforceMakeOutputDir=True --> Creating the output folder now...";
54 QDir().mkdir(m_outputDirectory.c_str());
55 } else {
56 throw std::runtime_error(errMessage.c_str());
57 }
58 }
59 }
60
61}
std::string m_inputDirectory
std::string m_outputDirectory

◆ ~VP1FileUtilities()

VP1FileUtilities::~VP1FileUtilities ( )
virtual

Definition at line 63 of file VP1FileUtilities.cxx.

64{
65}

Member Function Documentation

◆ cleanUp()

void VP1FileUtilities::cleanUp ( )
private

Definition at line 175 of file VP1FileUtilities.cxx.

176{
177 //std::cout << "VP1FileUtilities::cleanUp()" << std::endl;
178
179 QDir dir;
180 dir.setPath(QString(m_outputDirectory.c_str()));
181
182 QStringList nameFilters;
183 nameFilters << "vp1_*.pool.root";
184
185 dir.setFilter(QDir::Files);
186 dir.setNameFilters(nameFilters);
187 dir.setSorting(QDir::Time|QDir::Reversed);
188 QFileInfoList list = dir.entryInfoList();
189
190 if(int(list.size()) > m_fileLimit)
191 {
192 const QFileInfo& fileInfo = list.at(0);
193
194 if(!dir.remove(fileInfo.fileName()))
195 throw std::runtime_error("VP1FileUtilities::cleanup() - WARNING: Unable to do the clean up!");
196 }
197
198 QString poolCatalog("PoolFileCatalog.xml");
199 std::string poolCatalogStr = poolCatalog.toStdString();
200 std::cout << "looking for " << poolCatalogStr << " in " << QDir::currentPath().toStdString() << std::endl;
201 if ( QDir::current().entryList().contains( poolCatalog ) ) {
202 std::cout << "VP1FileUtilities::cleanUp() - removing the file '" << poolCatalogStr << "' because it causes problems for subsequent Athena commands opening the copied file." << std::endl;
203 QDir cwd = QDir::currentPath();
204 if ( ! cwd.remove( poolCatalog ) )
205 std::cerr << "VP1FileUtilities WARNING! Unable to delete " << poolCatalogStr << std::endl;
206 }
207
208 return;
209}
bool contains(const std::string &s, const std::string &regx)
does a string contain the substring
Definition hcg.cxx:114
std::string cwd
Definition listroot.cxx:38
list(name, path='/')
Definition histSizes.py:38
poolCatalog(poolDir, catalog=None, runNow=True)

◆ fileExistsAndReadable()

bool VP1FileUtilities::fileExistsAndReadable ( const std::string & file)
static

Definition at line 211 of file VP1FileUtilities.cxx.

212{
213 return !file.empty() && QFileInfo(file.c_str()).exists();
214}
TFile * file

◆ produceNewFile()

void VP1FileUtilities::produceNewFile ( const std::string & sourceFile,
unsigned int runNumber,
unsigned long long eventNumber,
unsigned int timeStamp,
const std::string & textLabel = "" )

Definition at line 67 of file VP1FileUtilities.cxx.

72{
73 // Check if the sourceFile exists
74 QString srcName(sourceFile.c_str());
75 QFile srcFile(srcName);
76
77 if(!srcFile.exists())
78 throw std::runtime_error("Source file does not exist!");
79
80 QString inpDirName(m_inputDirectory.c_str());
81 QString outDirName(m_outputDirectory.c_str());
82
83 // Construct new filename
84 QString newFileName = inpDirName;
85 std::ostringstream newFileStr;
86
87 // if input directory name is empty don't add / to the file name
88 // also take into account a possibility of trailing slash in directory name
89 if(m_inputDirectory!="" && m_inputDirectory.rfind('/')!=m_inputDirectory.size()-1)
90 newFileStr << "/";
91
92 QString latestEventFileName = inpDirName + QString(newFileStr.str().c_str()) + QString("latest_vp1event");
93
94 if (textLabel != "" ) {
95 newFileStr << "vp1_r" << runNumber << "_ev" << eventNumber << "_u" << timeStamp << "_t" << textLabel << ".pool.root";
96 } else {
97 newFileStr << "vp1_r" << runNumber << "_ev" << eventNumber << "_u" << timeStamp << ".pool.root";
98 }
99
100 newFileName += QString(newFileStr.str().c_str());
101
102
103 // adding the output folder, if not empty
104 if (outDirName != "")
105 newFileName = outDirName + QDir::separator() + newFileName;
106
107 // do copy
108 std::cout << "VP1FileUtilities -- copying '" << (srcFile.fileName()).toStdString() << "' to: '" << newFileName.toStdString() << "'..." << std::endl;
109
110
111 // get size of the file to be copied
112 qint64 inputSize = srcFile.size();
113 std::cout << "VP1FileUtilities -- Size of the input file to be copied: " << inputSize << std::endl;
114
115
116 if(!srcFile.copy(newFileName)) {
117 throw std::runtime_error("VP1FileUtilities -- Unable to copy the temp file to the new file, so unable to produce the new vp1 event file");
118 }
119
120 // remove the input file (if not disabled by user)
121 std::cout << "VP1FileUtilities -- delete temp file? --> " << m_removeInputFile << std::endl;
122 if (m_removeInputFile) {
123 bool copyDone = false;
124 std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now() ;
125 typedef std::chrono::duration<int,std::milli> millisecs_t ;
126 while (!copyDone) {
127
128 // get handle on new file
129 QFileInfo checkFile(newFileName);
130
131
132 // check if file exists (and it is a file, and not a directory)
133 if (checkFile.exists() && checkFile.isFile() && (checkFile.size() == inputSize) )
134 {
135 std::cout << "VP1FileUtilities -- Size of the file to be deleted: " << checkFile.size() << std::endl;
136 copyDone = true;
137 if(!srcFile.remove())
138 std::cerr << "VP1FileUtilities -- WARNING! Unable to delete " << sourceFile << std::endl;
139 }
140 else
141 {
142 std::cout << "VP1FileUtilities -- I could not find the output file, so probably the copy action is not finished yet. I'll wait for a short while and I will retry..." << std::endl;
143 std::this_thread::sleep_for(std::chrono::milliseconds(500)); //make the program waiting for 0.5 seconds
144 std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now() ;
145 millisecs_t duration( std::chrono::duration_cast<millisecs_t>(end-start) ) ;
146 if (duration.count() > 2000.0 )
147 {
148 std::cout << "VP1FileUtilities -- WARNING!!! " << duration.count() << " milliseconds passed and still I cannot find the output file. Probably there was a problem. Giving up with the removal of the source file...\n" ;
149 copyDone = true;
150 }
151 }
152 }
153 }
154
155 // create/update the latest event file
156 QFile latestEvent(latestEventFileName);
157 if(latestEvent.exists() && !latestEvent.remove())
158 throw std::runtime_error("VP1FileUtilities -- Unable to overwrite the existing latest event file");
159
160 if(!latestEvent.open(QIODevice::WriteOnly | QIODevice::Text))
161 throw std::runtime_error("VP1FileUtilities -- Unable to create new latest event file");
162 latestEvent.write(newFileName.toStdString().c_str());
163 latestEvent.close();
164
165 // do cleanup if requested. '-1' means 'KEEP ALL FILES'
166 if (m_fileLimit != -1) {
167 std::cout << "VP1FileUtilities -- cleaning up..." << std::endl;
168 cleanUp();
169 }
170}
srcFile
Definition fitman.py:373
checkFile(fileName, the_type, requireTree)
setEventNumber timeStamp

Member Data Documentation

◆ m_fileLimit

int VP1FileUtilities::m_fileLimit
private

Definition at line 64 of file VP1FileUtilities.h.

◆ m_forceMakeOutputDir

bool VP1FileUtilities::m_forceMakeOutputDir
private

Definition at line 66 of file VP1FileUtilities.h.

◆ m_inputDirectory

std::string VP1FileUtilities::m_inputDirectory
private

Definition at line 63 of file VP1FileUtilities.h.

◆ m_outputDirectory

std::string VP1FileUtilities::m_outputDirectory
private

Definition at line 65 of file VP1FileUtilities.h.

◆ m_removeInputFile

bool VP1FileUtilities::m_removeInputFile
private

Definition at line 67 of file VP1FileUtilities.h.


The documentation for this class was generated from the following files: