20 #include <QStringList>
21 #include <QFileInfoList>
33 unsigned int fileLimit,
const std::string&
outputDir,
bool forceMakeOutputDir,
bool removeInputFile):
35 m_fileLimit(fileLimit),
37 m_forceMakeOutputDir(forceMakeOutputDir),
38 m_removeInputFile(removeInputFile)
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());
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...";
56 throw std::runtime_error(errMessage.c_str());
71 const std::string& textLabel)
74 QString srcName(sourceFile.c_str());
78 throw std::runtime_error(
"Source file does not exist!");
84 QString newFileName = inpDirName;
85 std::ostringstream newFileStr;
92 QString latestEventFileName = inpDirName + QString(newFileStr.str().c_str()) + QString(
"latest_vp1event");
94 if (textLabel !=
"" ) {
100 newFileName += QString(newFileStr.str().c_str());
104 if (outDirName !=
"")
105 newFileName = outDirName + QDir::separator() + newFileName;
108 std::cout <<
"VP1FileUtilities -- copying '" << (
srcFile.fileName()).toStdString() <<
"' to: '" << newFileName.toStdString() <<
"'..." << std::endl;
112 qint64 inputSize =
srcFile.size();
113 std::cout <<
"VP1FileUtilities -- Size of the input file to be copied: " << inputSize << std::endl;
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");
121 std::cout <<
"VP1FileUtilities -- delete temp file? --> " <<
m_removeInputFile << std::endl;
123 bool copyDone =
false;
125 typedef std::chrono::duration<int,std::milli> millisecs_t ;
135 std::cout <<
"VP1FileUtilities -- Size of the file to be deleted: " <<
checkFile.size() << std::endl;
138 std::cerr <<
"VP1FileUtilities -- WARNING! Unable to delete " << sourceFile << std::endl;
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));
145 millisecs_t
duration( std::chrono::duration_cast<millisecs_t>(
end-
start) ) ;
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" ;
156 QFile latestEvent(latestEventFileName);
157 if(latestEvent.exists() && !latestEvent.remove())
158 throw std::runtime_error(
"VP1FileUtilities -- Unable to overwrite the existing latest event file");
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());
167 std::cout <<
"VP1FileUtilities -- cleaning up..." << std::endl;
182 QStringList nameFilters;
183 nameFilters <<
"vp1_*.pool.root";
186 dir.setNameFilters(nameFilters);
188 QFileInfoList
list =
dir.entryInfoList();
192 const QFileInfo& fileInfo =
list.at(0);
194 if(!
dir.remove(fileInfo.fileName()))
195 throw std::runtime_error(
"VP1FileUtilities::cleanup() - WARNING: Unable to do the clean up!");
199 std::string poolCatalogStr =
poolCatalog.toStdString();
200 std::cout <<
"looking for " << poolCatalogStr <<
" in " << QDir::currentPath().toStdString() << std::endl;
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();
205 std::cerr <<
"VP1FileUtilities WARNING! Unable to delete " << poolCatalogStr << std::endl;
213 return !
file.empty() && QFileInfo(
file.c_str()).exists();