ATLAS Offline Software
Loading...
Searching...
No Matches
DiskWriterLocal.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7
8//
9// includes
10//
11
13
14#include <exception>
15#include <iostream>
16#include <stdexcept>
17#include <TFile.h>
18#include <TSystem.h>
20
21//
22// method implementations
23//
24
25namespace SH
26{
27 void DiskWriterLocal ::
28 testInvariant () const
29 {
30 }
31
32
33
34 DiskWriterLocal ::
35 DiskWriterLocal (const std::string& val_path)
36 : m_path (val_path)
37 {
38 RCU_REQUIRE (!val_path.empty());
39
40 std::string dir = gSystem->DirName (val_path.c_str());
41 if (gSystem->mkdir (dir.c_str(), true) != 0 &&
42 gSystem->AccessPathName (dir.c_str()) != 0)
43 throw std::runtime_error ("failed to create directory: " + dir);
44 m_file = std::make_unique<TFile> (val_path.c_str(), "RECREATE");
45 if (m_file->IsZombie() || !m_file->IsOpen())
46 {
47 m_file.reset();
48 throw std::runtime_error ("failed to create file: " + val_path);
49 }
50
51 RCU_NEW_INVARIANT (this);
52 }
53
54
55
56 DiskWriterLocal ::
57 ~DiskWriterLocal ()
58 {
60
61 if (m_file)
62 {
63 try
64 {
65 close ();
66 } catch (std::exception& e)
67 {
68 std::cerr << "exception closing file " << m_path << ": "
69 << e.what() << std::endl;
70
71 } catch (...)
72 {
73 std::cerr << "unknown exception closing file " << m_path << std::endl;
74 }
75 }
76 }
77
78
79
80 std::string DiskWriterLocal ::
81 getPath () const
82 {
83 RCU_READ_INVARIANT (this);
84 return m_path;
85 }
86
87
88
89 TFile *DiskWriterLocal ::
90 getFile ()
91 {
93 RCU_REQUIRE2_SOFT (m_file != nullptr, "file already closed");
94 return m_file.get();
95 }
96
97
98
99 void DiskWriterLocal ::
100 doClose ()
101 {
103 RCU_REQUIRE2_SOFT (m_file != nullptr, "file already closed");
104
105 m_file->Write ();
106 if (m_file->TestBit (TFile::kWriteError))
107 throw std::runtime_error ("failed to write to file: " + m_path);
108 m_file->Close ();
109 m_file.reset();
110 }
111}
#define RCU_DESTROY_INVARIANT(x)
Definition Assert.h:223
#define RCU_CHANGE_INVARIANT(x)
Definition Assert.h:219
#define RCU_REQUIRE2_SOFT(x, y)
Definition Assert.h:143
#define RCU_NEW_INVARIANT(x)
Definition Assert.h:221
#define RCU_REQUIRE(x)
Definition Assert.h:196
#define RCU_READ_INVARIANT(x)
Definition Assert.h:217
std::unique_ptr< TFile > m_file
the actual file object
std::string m_path
the path being used
void close()
closes the file we are writing to
This module provides a lot of global definitions, forward declarations and includes that are used by ...
Definition PrunDriver.h:15