~mdrews/maus/my-branch

« back to all changes in this revision

Viewing changes to src/output/OutputCppRoot/test_output_cpp_root.py

  • Committer: Durga Rajaram
  • Date: 2013-10-17 14:45:06 UTC
  • mfrom: (659.1.76 rc)
  • Revision ID: durga@fnal.gov-20131017144506-orvzitqqdbj08ohy
Tags: MAUS-v0.7.3
MAUS-v0.7.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#  This file is part of MAUS: http://micewww.pp.rl.ac.uk:8080/projects/maus
 
1
#  This file is part of MAUS: http://micewww.pp.rl.ac.uk/projects/maus
2
2
#
3
3
#  MAUS is free software: you can redistribute it and/or modify
4
4
#  it under the terms of the GNU General Public License as published by
43
43
        Define cards and initialise Output
44
44
        """
45
45
        self.output = OutputCppRoot.OutputCppRoot()
46
 
        self.outfile = os.path.join \
47
 
                 (os.environ["MAUS_ROOT_DIR"], "tmp", "test_outputCppRoot.root")
 
46
        self.outdir = os.environ["MAUS_ROOT_DIR"]+"/tmp/test_output_cpp_root/"
 
47
        self.outfile = self.outdir+"test_outputCppRoot.root"
 
48
        try:
 
49
            os.mkdir(self.outdir)
 
50
        except OSError:
 
51
            pass
 
52
        try:
 
53
            os.mkdir(self.outdir+"/end_of_run/")
 
54
        except OSError:
 
55
            pass
48
56
        self.on_error_standard = ErrorHandler.DefaultHandler().on_error
49
57
        #ref = {"$ref":"#test_branch/double_by_value"}
50
58
        self.test_data = {
55
63
            "daq_event_type":"physics_event",
56
64
            "recon_events":[],
57
65
            "mc_events":[],
58
 
            #"test_branch":{"test_child_by_value":{},
59
 
            #               "test_child_by_ref":ref,
60
 
            #               "test_child_array":[None, ref, None, ref]},
61
66
            "maus_event_type":"Spill",
62
67
        }
63
68
        self.test_job_header = {
84
89
        self.cards = Configuration.Configuration().getConfigJSON()
85
90
        self.cards = json.loads(self.cards)
86
91
        self.cards["output_root_file_name"] = self.outfile
 
92
        self.cards["output_root_file_mode"] = "one_big_file"
 
93
        self.cards["end_of_run_output_root_directory"] = \
 
94
                                                      self.outdir+"/end_of_run/"
87
95
        self.cards["verbose_level"] = 2
88
96
        self.cards = json.dumps(self.cards)
89
97
        if maus_cpp.globals.has_instance():
90
98
            maus_cpp.globals.death()
91
99
        maus_cpp.globals.birth(self.cards)
92
100
        self.output.birth(self.cards)
 
101
        #ErrorHandler.DefaultHandler().on_error = 'raise'
93
102
 
94
103
    def tearDown(self): # pylint: disable=C0103, C0202
95
104
        """
123
132
        Check that we can birth and death properly
124
133
        """
125
134
        an_output = OutputCppRoot.OutputCppRoot()
126
 
        #self.assertRaises(ErrorHandler.CppError, an_output.birth, '{}')
127
 
        #self.assertRaises(ErrorHandler.CppError, an_output.birth,
128
 
        #                  '{"output_root_file_name":""}')
129
 
        # for now this does NOT return an exception... needs fixing
 
135
        # for now this does NOT return an exception... looks like it is caught
 
136
        # by the API... set ErrorHandler.on_error to 'raise' and we raise a
 
137
        # std::exception killing the tests (oops)
130
138
        an_output.birth(json.dumps({}))
131
 
        an_output.birth(json.dumps({'output_root_file_name':''}))
 
139
        an_output.birth(json.dumps({'output_root_file_name':'',
 
140
                                    'output_root_file_mode':'one_big_file',
 
141
                                    'end_of_run_output_root_directory':'tmp'}))
 
142
        an_output.birth(json.dumps({'output_root_file_name':'test.root',
 
143
                                    'output_root_file_mode':'',
 
144
                                    'end_of_run_output_root_directory':'tmp'}))
 
145
        an_output.birth(json.dumps({'output_root_file_name':'test.root',
 
146
                                    'output_root_file_mode':'one',
 
147
                                    'end_of_run_output_root_directory':'tmp'}))
 
148
        # this is okay (dir can just be '')
 
149
        an_output.birth(json.dumps({'output_root_file_name':'test.root',
 
150
                                    'output_root_file_mode':'one_big_file',
 
151
                                    'end_of_run_output_root_directory':''}))
132
152
        an_output.birth(self.cards)
133
153
        an_output.death()
134
154
 
169
189
        self.assertFalse(self._save_event(json.dumps({"":{}})))
170
190
        self.assertFalse(self._save_event(''))
171
191
 
172
 
    def __check_job_header(self, json_conf_text, n_entries, entry):
 
192
    def __check_job_header(self, json_conf_text, n_entries, entry, fname=None):
173
193
        """
174
194
        Check that json_header entry has json_conf_text 
175
195
        """
176
 
        root_file = ROOT.TFile(self.outfile, "READ") # pylint: disable = E1101
 
196
        if fname == None:
 
197
            fname = self.outfile
 
198
        root_file = ROOT.TFile(fname, "READ") # pylint: disable = E1101
177
199
        job_header = ROOT.MAUS.JobHeaderData() # pylint: disable = E1101
178
200
        tree = root_file.Get("JobHeader")
179
201
        tree.SetBranchAddress("job_header", job_header)
183
205
                         json_conf_text)
184
206
        root_file.Close()
185
207
 
186
 
    def __check_job_footer(self, datetime_string, n_entries, entry):
 
208
    def __check_job_footer(self, datetime_string, n_entries, entry, fname=None):
187
209
        """
188
210
        Check that json_footer entry has datetime_string
189
211
        """
190
 
        root_file = ROOT.TFile(self.outfile, "READ") # pylint: disable = E1101
 
212
        if fname == None:
 
213
            fname = self.outfile
 
214
        root_file = ROOT.TFile(fname, "READ") # pylint: disable = E1101
191
215
        job_footer = ROOT.MAUS.JobFooterData() # pylint: disable = E1101
192
216
        tree = root_file.Get("JobFooter")
193
217
        tree.SetBranchAddress("job_footer", job_footer)
197
221
                         datetime_string)
198
222
        root_file.Close()
199
223
 
200
 
    def __check_run_header(self, run_number, n_entries, entry):
 
224
    def __check_run_header(self, run_number, n_entries, entry, fname=None):
201
225
        """
202
226
        Check that json_header entry has json_conf_text 
203
227
        """
204
 
        root_file = ROOT.TFile(self.outfile, "READ") # pylint: disable = E1101
 
228
        if fname == None:
 
229
            fname = self.outfile
 
230
        root_file = ROOT.TFile(fname, "READ") # pylint: disable = E1101
205
231
        run_header = ROOT.MAUS.RunHeaderData() # pylint: disable = E1101
206
232
        tree = root_file.Get("RunHeader")
207
233
        tree.SetBranchAddress("run_header", run_header)
274
300
        self.__check_job_header("output cpp root test 2", 2, 1)
275
301
        self.__check_job_footer("1977-04-04T00:00:00.000000", 2, 1)
276
302
 
 
303
    def test_one_file_per_run(self):
 
304
        """
 
305
        test_OutputCppRoot.test_one_file_per_run: one_file_per_run option
 
306
        """
 
307
        my_output = OutputCppRoot.OutputCppRoot()
 
308
        cards_py = json.loads(self.cards)
 
309
        cards_py["output_root_file_mode"] = "one_file_per_run"
 
310
        self.cards = json.dumps(cards_py)
 
311
        my_output.birth(self.cards)
 
312
        self.test_data["run_number"] = 10
 
313
        self.test_run_header["run_number"] = 10
 
314
        for i in range(3): # pylint: disable = W0612
 
315
            self.assertTrue(my_output.save(
 
316
                json.dumps(self.test_data)
 
317
            ))
 
318
        for i in range(2): # pylint: disable = W0612
 
319
            self.assertTrue(my_output.save(
 
320
                json.dumps(self.test_run_header)
 
321
            ))
 
322
        for i in range(3): # pylint: disable = W0612
 
323
            self.assertTrue(my_output.save(
 
324
                json.dumps(self.test_data)
 
325
            ))
 
326
        self.test_run_header["run_number"] = 9
 
327
        for i in range(3): # pylint: disable = W0612
 
328
            self.assertTrue(my_output.save(
 
329
                json.dumps(self.test_run_header)
 
330
            ))
 
331
        for i in range(3): # pylint: disable = W0612
 
332
            self.assertTrue(my_output.save(
 
333
                json.dumps(self.test_job_header)
 
334
            ))
 
335
        my_output.death()
 
336
        self.__check_job_header("output cpp root test", 3, 0,
 
337
                                        self.outdir+"test_outputCppRoot_0.root")
 
338
        self.__check_run_header(10, 2, 0,
 
339
                                       self.outdir+"test_outputCppRoot_10.root")
 
340
        self.__check_run_header(9, 3, 0,
 
341
                                        self.outdir+"test_outputCppRoot_9.root")
 
342
 
 
343
    def test_end_of_run_file_per_run(self):
 
344
        """
 
345
        test_OutputCppRoot.test_one_file_per_run: end_of_run_file_per_run option
 
346
        """
 
347
        my_output = OutputCppRoot.OutputCppRoot()
 
348
        cards_py = json.loads(self.cards)
 
349
        cards_py["output_root_file_mode"] = "end_of_run_file_per_run"
 
350
        cards_py["output_root_file_name"] = "test_outputCppRoot.root"
 
351
        cards_py["end_of_run_output_root_directory"] = \
 
352
                                                      self.outdir+"/end_of_run/"
 
353
        self.cards = json.dumps(cards_py)
 
354
        my_output.birth(self.cards)
 
355
        self.test_data["run_number"] = 10
 
356
        self.test_run_header["run_number"] = 10
 
357
        for i in range(3): # pylint: disable = W0612
 
358
            self.assertTrue(my_output.save(
 
359
                json.dumps(self.test_data)
 
360
            ))
 
361
        for i in range(2): # pylint: disable = W0612
 
362
            self.assertTrue(my_output.save(
 
363
                json.dumps(self.test_run_header)
 
364
            ))
 
365
        for i in range(3): # pylint: disable = W0612
 
366
            self.assertTrue(my_output.save(
 
367
                json.dumps(self.test_data)
 
368
            ))
 
369
        self.test_run_header["run_number"] = 9
 
370
        for i in range(3): # pylint: disable = W0612
 
371
            self.assertTrue(my_output.save(
 
372
                json.dumps(self.test_run_header)
 
373
            ))
 
374
        self.assertTrue(my_output.save(
 
375
            json.dumps(self.test_job_footer)
 
376
        ))
 
377
        self.test_run_footer["run_number"] = 9
 
378
        self.assertTrue(my_output.save(
 
379
            json.dumps(self.test_run_footer)
 
380
        ))
 
381
        for i in range(3): # pylint: disable = W0612
 
382
            self.assertTrue(my_output.save(
 
383
                json.dumps(self.test_job_header)
 
384
            ))
 
385
        my_output.death()
 
386
        self.__check_job_header("output cpp root test", 3, 0,
 
387
                           self.outdir+"/end_of_run/0/test_outputCppRoot.root")
 
388
        self.__check_run_header(10, 2, 0,
 
389
                           self.outdir+"/end_of_run/10/test_outputCppRoot.root")
 
390
        self.__check_run_header(9, 3, 0,
 
391
                           self.outdir+"/end_of_run/9/test_outputCppRoot.root")
 
392
        self.__check_job_footer("1977-04-04T00:00:00.000000", 1, 0,
 
393
                           self.outdir+"/end_of_run/0/test_outputCppRoot.root")
 
394
 
 
395
 
277
396
if __name__ == "__main__":
278
397
    unittest.main()
279
398