GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
FixRunInfo.cxx
Go to the documentation of this file.
1// little script to read run info from .odb file and write to root file(s)
2
3#include <iostream>
4#include <iomanip>
5#include <fstream>
6
7#include "TFile.h"
8#include "TRunInfo.h"
9
10int main(int argc, char** argv)
11{
12 // need at least two arguments
13 if(argc < 3) {
14 std::cout << "Usage: " << argv[0] << " <odb file> <root file(s)>" << std::endl;
15 return 1;
16 }
17
18 std::ifstream odbFile(argv[1]);
19
20 std::string line;
21 std::string runComment;
22 std::string runTitle;
23 std::string subString;
24 int runNumber = 0;
25 int subRunNumber = 0;
26 uint32_t runStart = 0;
27 uint32_t runStop = 0;
28
29 while(std::getline(odbFile, line)) {
30 // Expected format for run parameters (comment and title)
31 // [/Experiment/Run parameters]
32 // Comment = STRING : [256]
33 // Run Title = STRING : [256]
34 if(line == "[/Experiment/Run parameters]") {
35 std::cout << "found line " << line << std::endl;
36 std::getline(odbFile, line);
37 runComment = line.substr(25, std::string::npos);
38 std::cout << "got run comment '" << runComment << "' from line " << line << std::endl;
39 std::getline(odbFile, line);
40 runTitle = line.substr(27, std::string::npos);
41 std::cout << "got run title '" << runTitle << "' from line " << line << std::endl;
42 }
43 // [/Runinfo]
44 // State = INT : 3
45 // Online Mode = INT : 1
46 // Run number = INT : 21360
47 // Transition in progress = INT : 2
48 // Start abort = INT : 0
49 // Requested transition = INT : 0
50 // Start time = STRING : [32] Fri Nov 26 13:53:32 2021
51 // Start time binary = DWORD : 1637963612
52 // Stop time = STRING : [32] Fri Nov 26 14:56:26 2021
53 // Stop time binary = DWORD : 1637967386
54 if(line == "[/Runinfo]") {
55 std::cout << "found line " << line << std::endl;
56 std::getline(odbFile, line); // skipping state
57 std::getline(odbFile, line); // skipping online mode
58 std::getline(odbFile, line); // run number
59 subString = line.substr(25, std::string::npos);
60 runNumber = std::stoi(subString);
61 std::cout << "got run number " << runNumber << " from substring '" << subString << "' from line " << line << std::endl;
62 std::getline(odbFile, line); // skipping transition in progress
63 std::getline(odbFile, line); // skipping start abort
64 std::getline(odbFile, line); // skipping requested transition
65 std::getline(odbFile, line); // skipping start time
66 std::getline(odbFile, line); // start time binary
67 subString = line.substr(39, std::string::npos);
68 runStart = std::stoi(subString);
69 std::cout << "got run start " << runStart << " from substring '" << subString << "' from line " << line << std::endl;
70 std::getline(odbFile, line); // skipping stop time
71 std::getline(odbFile, line); // stop time binary
72 subString = line.substr(38, std::string::npos);
73 runStop = std::stoi(subString);
74 std::cout << "got run stop " << runStop << " from substring '" << subString << "' from line " << line << std::endl;
75
76 break;
77 }
78 }
79
80 TRunInfo* runInfo = TRunInfo::Get();
81 std::cout << "old run info:" << std::endl;
82 runInfo->Print();
83 TRunInfo::SetRunNumber(runNumber);
84 TRunInfo::SetRunTitle(runTitle.c_str());
85 TRunInfo::SetRunComment(runComment.c_str());
86 TRunInfo::SetRunStart(runStart);
87 TRunInfo::SetRunStop(runStop);
89
90 for(int i = 2; i < argc; ++i) {
91 // parse file name to get run number and sub-run number
92 line = argv[i];
93 std::size_t dot = line.rfind('_');
94 std::size_t underscore = line.rfind('.');
95 std::size_t start = line.rfind("fragment");
96 if(start != std::string::npos) {
97 start += 8;
98 } else {
99 start = line.rfind("analysis");
100 if(start != std::string::npos) {
101 start += 8;
102 } else {
103 std::cerr << "malformed file name(" << line << "), should contain either 'fragment' or 'analysis'" << std::endl;
104 continue;
105 }
106 }
107 // check that the order is start - underscore - dot
108 if(start < underscore && underscore < dot && dot != std::string::npos) {
109 // format should be path/<fragment or analysis>#####_###.root
110 subString = line.substr(start, underscore - start);
111 runNumber = std::stoi(subString);
112 std::cout << "got run number " << runNumber << " from substring '" << subString << "' from line " << line << std::endl;
113 if(runNumber != TRunInfo::RunNumber()) {
114 std::cerr << "Mismatch between the run number of this file (" << runNumber << ") and the run number in the ODB (" << TRunInfo::RunNumber() << "), skipping " << line << std::endl;
115 continue;
116 }
117 subString = line.substr(underscore + 1, dot - underscore - 1);
118 subRunNumber = std::stoi(subString);
119 std::cout << "got sub run number " << runNumber << " from substring '" << subString << "' from line " << line << std::endl;
120 TRunInfo::SetSubRunNumber(subRunNumber);
121 std::cout << "new run info:" << std::endl;
122 runInfo->Print();
123 TFile f(argv[i]);
124 auto* fileRunInfo = static_cast<TRunInfo*>(f.Get("RunInfo"));
125 if(fileRunInfo == nullptr) {
126 std::cout << "no run info found in " << line << std::endl;
127 } else {
128 std::cout << "run info found in " << line << ": " << std::endl;
129 fileRunInfo->Print();
130 }
131 runInfo->Write("RunInfo", TObject::kOverwrite);
132 f.Close();
133 } else {
134 std::cerr << "malformed file name, expected 'path/<fragment or analysis>#####_###.root', not " << line << std::endl;
135 }
136 }
137
138 return 0;
139}
int main(int argc, char **argv)
static void SetRunComment(const char *run_comment)
Definition TRunInfo.h:214
static void SetRunStart(double tmp)
Definition TRunInfo.h:222
static void SetRunStop(double tmp)
Definition TRunInfo.h:223
static int RunNumber()
Definition TRunInfo.h:201
static void SetSubRunNumber(int tmp)
Definition TRunInfo.h:199
void Print(Option_t *opt="") const override
Definition TRunInfo.cxx:72
static void SetRunNumber(int tmp)
Definition TRunInfo.h:198
static void SetRunTitle(const char *run_title)
Definition TRunInfo.h:210
static void SetRunLength()
Definition TRunInfo.h:225
static TRunInfo * Get(bool verbose=false)
Definition TSingleton.h:33