-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathORFileWriter.cc
109 lines (95 loc) · 2.79 KB
/
ORFileWriter.cc
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
// ORFileWriter.cc
#include "ORFileWriter.hh"
#include "TROOT.h"
#include "TObjString.h"
#include "ORLogger.hh"
#include "ORRunContext.hh"
using namespace std;
ORFileWriter::ORFileWriter(string label)
{
fLabel = label;
fSavedName = "";
fFile = NULL;
}
ORDataProcessor::EReturnCode ORFileWriter::StartRun()
{
if(fFile != NULL) {
if(UpdateFilePointer() == NULL) {
ORLog(kError) << "Lost track of fFile!" << endl;
return kFailure;
}
fFile->Close();
delete fFile;
}
if (!fRunContext) {
ORLog(kError) << "fRunContext is NULL!" << endl;
return kFailure;
}
string filename = fLabel + ::Form("_run%d.root", fRunContext->GetRunNumber());
fFile = new TFile(filename.c_str(), "RECREATE");
fSavedName = fFile->GetName();
fSavedName.erase( fSavedName.size() - 5, 5 ); // Removing .root from the end
TObjString headerXML(fRunContext->GetHeader()->GetRawXML().Data());
headerXML.Write("headerXML");
fLastSubRunNumber = 0;
return kSuccess;
}
ORDataProcessor::EReturnCode ORFileWriter::EndRun()
{
if(fFile == NULL) {
ORLog(kWarning) << "EndRun(): ending a run that never began!" << endl;
return kBreak;
}
if(UpdateFilePointer() == NULL) {
ORLog(kError) << "Lost track of fFile!" << endl;
return kFailure;
}
fFile->cd();
// other processors will write their data after this processor; close the file at
// the beginning of the next run or at the end of processing.
return kSuccess;
}
ORDataProcessor::EReturnCode ORFileWriter::ProcessDataRecord(UInt_t*)
{
if (fRunContext->GetSubRunNumber()!=fLastSubRunNumber)
{
fFile->cd();
TObjString headerXML(fRunContext->GetHeader()->GetRawXML().Data());
headerXML.Write(::Form("headerXML_%d",fRunContext->GetSubRunNumber()));
fLastSubRunNumber = fRunContext->GetSubRunNumber();
}
return kSuccess;
}
ORDataProcessor::EReturnCode ORFileWriter::EndProcessing()
{
if(fFile == NULL) {
ORLog(kWarning) << "EndProcessing(): no file was open!" << endl;
return kBreak;
}
if(UpdateFilePointer() == NULL) {
ORLog(kError) << "Lost track of fFile!" << endl;
return kFailure;
}
fFile->Close();
delete fFile;
return kSuccess;
}
TFile* ORFileWriter::UpdateFilePointer()
{
TSeqCollection* listOfFiles = gROOT->GetListOfFiles();
if(listOfFiles->IndexOf(fFile) == -1) {
// fFile has been deleted by stupid ROOT. Search for the updated pointer
fFile = NULL;
for(int i=0; fFile == NULL && i<=listOfFiles->LastIndex(); i++) {
TFile* file = (TFile*) listOfFiles->At(i);
std::string nameOfFile = file->GetName();
if ( nameOfFile.find( fSavedName ) != string::npos ) {
fFile = file;
break;
}
}
}
// If the updated pointer is not found, fFile is NULL here. Could be a
// bug, let the calling function decide.
return fFile;
}