~mdrews/maus/my-branch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* This file is part of MAUS: http://micewww.pp.rl.ac.uk:8080/projects/maus
 *
 * MAUS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MAUS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MAUS.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <sys/stat.h>
#include <sys/types.h>

#include <algorithm>

#include "src/legacy/Interface/Squeal.hh"

#include "src/common_cpp/Utils/JsonWrapper.hh"
#include "src/common_cpp/JsonCppStreamer/ORStream.hh"

#include "src/common_cpp/DataStructure/JobHeader.hh"
#include "src/common_cpp/Converter/DataConverters/JsonCppJobHeaderConverter.hh"

#include "src/common_cpp/DataStructure/RunHeader.hh"
#include "src/common_cpp/Converter/DataConverters/JsonCppRunHeaderConverter.hh"

#include "src/common_cpp/DataStructure/Spill.hh"
#include "src/common_cpp/Converter/DataConverters/JsonCppSpillConverter.hh"

#include "src/common_cpp/DataStructure/RunFooter.hh"
#include "src/common_cpp/Converter/DataConverters/JsonCppRunFooterConverter.hh"

#include "src/common_cpp/DataStructure/JobFooter.hh"
#include "src/common_cpp/Converter/DataConverters/JsonCppJobFooterConverter.hh"

#include "src/output/OutputCppRoot/OutputCppRoot.hh"

namespace MAUS {
OutputCppRoot::OutputCppRoot() : OutputBase<std::string>("OutputCppRoot"),
     _outfile(NULL), _fname(""), _end_of_run_dir("./"), _outfile_branch(""),
     _run_numbers(), _mode(one_big_file) {
}

void OutputCppRoot::_birth(const std::string& json_datacards) {
  try {
    // clean up any allocated memory or throw an exception
    death();
    // load datacards
    Json::Value datacards = JsonWrapper::StringToJson(json_datacards);
    _fname = JsonWrapper::GetProperty(datacards,
                  "output_root_file_name", JsonWrapper::stringValue).asString();
    if (_fname == "") {
        throw(Squeal(
          Squeal::recoverable,
          "output_root_file_name is empty",
          "OutputCppRoot::birth"
        ));
    }
    std::string mode = JsonWrapper::GetProperty(datacards,
                  "output_root_file_mode", JsonWrapper::stringValue).asString();
    if (mode == "one_big_file") {
        _mode = one_big_file;
    } else if (mode == "one_file_per_run") {
        _mode = one_file_per_run;
    } else if (mode == "end_of_run_file_per_run") {
        _mode = end_of_run_file_per_run;
    } else {
        throw(Squeal(
          Squeal::recoverable,
          "output_root_file_name '"+mode+"' is not valid; should be one of\n"+
          std::string("   one_big_file\n")+
          std::string("   one_file_per_run\n")+
          std::string("   end_of_run_file_per_run\n"),
          "OutputCppRoot::birth"
        ));
    }
    _end_of_run_dir = JsonWrapper::GetProperty(
                                          datacards,
                                          "end_of_run_output_root_directory",
                                          JsonWrapper::stringValue).asString();
  } catch(...) {
    death();
    throw;
  }
}

template <class ConverterT, class DataT>
bool OutputCppRoot::write_event(MAUSEvent<DataT>* data_cpp,
                                const Json::Value& data_json,
                                std::string branch_name) {
    // watch for double frees (does close() delete data_cpp?)
    if (branch_name == "")
        return false;

    ConverterT conv;
    data_cpp = conv.convert(&data_json);
    check_file_exists(data_cpp);
    if (_outfile == NULL) {
        throw(Squeal(
          Squeal::recoverable,
          "OutputCppRoot was not initialised properly",
          "OutputCppRoot::write_event"
        ));
    }

    std::string data_type = data_cpp->GetEventType();
    if (_outfile_branch != data_type) {
        if (_outfile->is_open())
            _outfile->close();
        int run = run_number(data_cpp);
        _outfile->open(file_name(run).c_str(), data_type.c_str(),
                       "MAUS output data", "UPDATE");
        _outfile_branch = data_type;
    }
    (*_outfile) << branchName(branch_name.c_str()) << data_cpp;

    if (data_cpp->GetEvent() == NULL) {  // failed on conversion
        return false;
    }
    try {
        (*_outfile) << fillEvent;
    } catch(...) {
        if (data_cpp != NULL)
            data_cpp->SetEvent(NULL);  // double free?
        throw; // raise the exception
    }
    return true;
}

bool OutputCppRoot::_save(std::string data_str) {
    Json::Value data_json = JsonWrapper::StringToJson(data_str);
    event_type my_tp = get_event_type(data_json);
    switch (my_tp) {
        case _job_header_tp:
            return write_event<JsonCppJobHeaderConverter, JobHeader>
                                    (&_job_header_cpp, data_json, "job_header");
        case _run_header_tp:
            return write_event<JsonCppRunHeaderConverter, RunHeader>
                                    (&_run_header_cpp, data_json, "run_header");
        case _spill_tp:
            return write_event<JsonCppSpillConverter, Spill>
                                               (&_spill_cpp, data_json, "data");
        case _run_footer_tp:
            return write_event<JsonCppRunFooterConverter, RunFooter>
                                    (&_run_footer_cpp, data_json, "run_footer");
        case _job_footer_tp:
            return write_event<JsonCppJobFooterConverter, JobFooter>
                                    (&_job_footer_cpp, data_json, "job_footer");
    }
    return false;
}

OutputCppRoot::event_type OutputCppRoot::get_event_type
                                                (const Json::Value& data_json) {
    std::string type = JsonWrapper::GetProperty
             (data_json, "maus_event_type", JsonWrapper::stringValue).asString();
    if (type == "Spill") {
        return _spill_tp;
    } else if (type == "JobHeader") {
        return _job_header_tp;
    } else if (type == "JobFooter") {
        return _job_footer_tp;
    } else if (type == "RunHeader") {
        return _run_header_tp;
    } else if (type == "RunFooter") {
        return _run_footer_tp;
    } else {
        throw Squeal(Squeal::recoverable,
                     "Failed to recognise maus_event_type "+type,
                     "OutputCppRoot::get_event_type");
    }
}

void OutputCppRoot::_death() {
  if (_outfile != NULL) {
    if (_outfile->is_open())
        _outfile->close();
    delete _outfile;
    _outfile = NULL;  // deletes spill
  }
}

std::string OutputCppRoot::file_name(int run_number) {
    std::string run_numb_str = STLUtils::ToString(run_number);
    switch (_mode) {
        case one_big_file:
            return _fname;
        case one_file_per_run: {
            size_t dot = _fname.find_last_of('.');
            if (dot == std::string::npos) {   // file -> file_1234
                return _fname+"_"+run_numb_str;
            } else {  // file.root -> file_1234.root
                std::string one = _fname.substr(0, dot);
                std::string two = _fname.substr(dot, _fname.size() - dot);
                return one+"_"+run_numb_str+two;
            }
            }
        case end_of_run_file_per_run:
            return _end_of_run_dir+"/"+run_numb_str+"/"+_fname;
    }
    return "";
}

std::string OutputCppRoot::dir_name(int run_number) {
    std::string file = file_name(run_number);
      size_t slash = file.find_last_of('/');
      if (slash == std::string::npos) {   // file -> file_1234
          return "./";
      } else {  // file.root -> file_1234.root
          return file.substr(0, slash);
      }
}

template <class DataT>
void OutputCppRoot::check_file_exists(DataT data_cpp) {
    std::string event = data_cpp->GetEventType();
    int run = run_number(data_cpp);
    switch (_mode) {
        case one_big_file:
            if (_run_numbers.size() > 0)
                return;
        case one_file_per_run:
        case end_of_run_file_per_run:
            if (std::binary_search(_run_numbers.begin(), _run_numbers.end(), run))
                return;
    }
    if (_outfile != NULL) {
        if (_outfile->is_open())
            _outfile->close();
        delete _outfile;
    }
    struct stat attributes;
    std::string dir = dir_name(run);
    if (stat(dir.c_str(), &attributes) < 0 || !S_ISDIR(attributes.st_mode)) {
        // if stat fails, maybe the directory doesnt exists
        if (mkdir(dir.c_str(), ACCESSPERMS) == -1) {
            throw Squeal(Squeal::recoverable, "Failed to make directory "+dir,
                         "OutputCppRoot::check_file_exists");
        }
    }
    // S_ISDIR(attributes.st_mode) - returns True if it's a directory...
    _outfile = new orstream(file_name(run).c_str(),
                            data_cpp->GetEventType().c_str(),
                            "MAUS output data", "RECREATE");
    _outfile_branch = data_cpp->GetEventType();
    std::vector<int>::iterator it = std::upper_bound(_run_numbers.begin(), _run_numbers.end(), run);
    _run_numbers.insert(it, run);
}

template <>
int OutputCppRoot::run_number(MAUSEvent<Spill>* data_cpp) {
    return data_cpp->GetEvent()->GetRunNumber();
}

template <>
int OutputCppRoot::run_number(MAUSEvent<RunHeader>* data_cpp) {
    return data_cpp->GetEvent()->GetRunNumber();
}

template <>
int OutputCppRoot::run_number(MAUSEvent<RunFooter>* data_cpp) {
    return data_cpp->GetEvent()->GetRunNumber();
}

template <>
int OutputCppRoot::run_number(MAUSEvent<JobHeader>* data_cpp) {
    return 0;
}

template <>
int OutputCppRoot::run_number(MAUSEvent<JobFooter>* data_cpp) {
    return 0;
}
}