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