Skip to content

Commit

Permalink
Create output directories (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
kjvbrt authored Mar 25, 2024
1 parent bc9bfc7 commit 29fad56
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
18 changes: 18 additions & 0 deletions k4FWCore/components/PodioOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
* limitations under the License.
*/
#include <cstdlib>
#include <filesystem>
#include <system_error>

#include "GaudiKernel/MsgStream.h"

#include "PodioOutput.h"
#include "k4FWCore/PodioDataSvc.h"
Expand All @@ -38,6 +42,20 @@ StatusCode PodioOutput::initialize() {
return StatusCode::FAILURE;
}

// check whether output directory needs to be created and eventually create it
auto outDirPath = std::filesystem::path(m_filename.value()).parent_path();
if (!outDirPath.empty() && !std::filesystem::is_directory(outDirPath)) {
std::error_code ec;
std::filesystem::create_directories(outDirPath, ec);
if (ec.value() != 0) {
error() << "Output directory \"" << outDirPath << "\" was not created!" << endmsg;
error() << "Error " << ec.value() << ": " << ec.message() << endmsg;

return StatusCode::FAILURE;
}
debug() << "Created output directory: " << outDirPath << endmsg;
}

m_framewriter = std::make_unique<podio::ROOTWriter>(m_filename);
m_switch = KeepDropSwitch(m_outputCommands);

Expand Down
1 change: 1 addition & 0 deletions test/k4FWCoreTest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function(add_test_with_env testname)
endfunction()

add_test_with_env(CreateExampleEventData options/createExampleEventData.py)
add_test_with_env(CreateExampleEventDataInDirectory options/createExampleEventDataInDirectory.py)

add_test_with_env(CheckExampleEventData options/checkExampleEventData.py PROPERTIES DEPENDS CreateExampleEventData)
add_test_with_env(CheckExampleEventData_noCollections options/checkExampleEventData.py --collections= PROPERTIES DEPENDS CreateExampleEventData)
Expand Down
41 changes: 41 additions & 0 deletions test/k4FWCoreTest/options/createExampleEventDataInDirectory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#
# Copyright (c) 2014-2024 Key4hep-Project.
#
# This file is part of Key4hep.
# See https://key4hep.github.io/key4hep-doc/ for further info.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from Gaudi.Configuration import INFO
from Configurables import ApplicationMgr
from Configurables import k4FWCoreTest_CreateExampleEventData
from Configurables import k4DataSvc
from Configurables import PodioOutput

podioevent = k4DataSvc("EventDataSvc")

producer = k4FWCoreTest_CreateExampleEventData()

out = PodioOutput("out")
out.filename = "output/dir/output_k4test_exampledata.root"
out.outputCommands = ["keep *"]


ApplicationMgr(
TopAlg=[producer, out],
EvtSel="NONE",
EvtMax=100,
ExtSvc=[podioevent],
OutputLevel=INFO,
StopOnSignal=True,
)

0 comments on commit 29fad56

Please sign in to comment.