Belle II Software development
setbgtime.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <framework/logging/Logger.h>
10#include <framework/gearbox/Unit.h>
11#include <framework/dataobjects/BackgroundMetaData.h>
12
13#include <TFile.h>
14#include <TTree.h>
15#include <TError.h>
16
17#include <boost/program_options.hpp>
18
19#include <string>
20#include <iostream>
21
22using namespace std;
23using namespace Belle2;
24namespace prog = boost::program_options;
25
26int main(int argc, char* argv[])
27{
28 // define command line options
29 prog::options_description options("Options");
30 options.add_options()
31 ("help,h", "print all available options")
32 ("file", prog::value<string>(), "file name")
33 ("time,t", prog::value<double>(), "real time of background sample in microseconds")
34 ("name,n", prog::value<string>(), "name of the background component")
35 ;
36
37 prog::positional_options_description posOptDesc;
38 posOptDesc.add("file", -1);
39
40 prog::variables_map varMap;
41 prog::store(prog::command_line_parser(argc, argv).
42 options(options).positional(posOptDesc).run(), varMap);
43 prog::notify(varMap);
44
45 // check for help option
46 if (varMap.count("help")) {
47 cout << "Usage: " << argv[0] << " [OPTIONS] [FILE]\n";
48 cout << options << endl;
49 return 0;
50 }
51
52 // check parameters
53 for (auto param : {"file", "time"}) {
54 if (!varMap.count(param)) {
55 B2ERROR("The " << param << " parameter is missing.");
56 return 1;
57 }
58 }
59 bool setName = (varMap.count("name") > 0);
60
61 // read parameters
62 string fileName = varMap["file"].as<string>();
63 double realTime = varMap["time"].as<double>();
64 string compName;
65 if (setName)
66 compName = varMap["name"].as<string>();
67
68 // open the root file
69 gErrorIgnoreLevel = kError;
70 TFile* file = TFile::Open(fileName.c_str(), "UPDATE");
71 if (!file || !file->IsOpen()) {
72 B2ERROR("Failed to open the file " << fileName);
73 return 1;
74 }
75
76 // read the BackgroundMetaData object or create a new one if it doesn't exist
77 BackgroundMetaData* bgMetaData = 0;
78 TTree* tree = (TTree*) file->Get("persistent");
79 TTree* newTree = 0;
80 if (!tree) {
81 bgMetaData = dynamic_cast<BackgroundMetaData*>(file->Get("BackgroundMetaData"));
82 if (!bgMetaData) {
83 B2WARNING("Failed to get persistent tree in the file " << fileName);
84 tree = new TTree("persistent", "persistent");
85 bgMetaData = new BackgroundMetaData;
86 tree->Branch("BackgroundMetaData", &bgMetaData);
87 newTree = tree;
88 }
89 } else {
90 tree->SetBranchAddress("BackgroundMetaData", &bgMetaData);
91 newTree = tree->CloneTree(0);
92 tree->GetEntry(0);
93 }
94
95 // update the IDs and write the updated BackgroundMetaData to the file
96 bgMetaData->setRealTime(realTime * Unit::us);
97 if (setName)
98 bgMetaData->setBackgroundType(compName);
99 if (newTree) {
100 newTree->Fill();
101 newTree->Write();
102 } else {
103 bgMetaData->Write("BackgroundMetaData");
104 }
105
106 cout << "File: " << fileName << endl;
107 cout << "Real time set to " << realTime << " microseconds." << endl << endl;
108 if (setName)
109 cout << "Background type set to " << compName.c_str() << endl;
110
111 return 0;
112}
113
Metadata information about the beam background file.
void setRealTime(float time)
Sets real time that corresponds to this background sample.
void setBackgroundType(const std::string &type)
Sets background type.
static const double us
[microsecond]
Definition: Unit.h:97
Abstract base class for different kinds of events.
STL namespace.