R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
neuland_map_to_cal.cxx
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) 2019 GSI Helmholtzzentrum für Schwerionenforschung GmbH *
3 * Copyright (C) 2019-2023 Members of R3B Collaboration *
4 * *
5 * This software is distributed under the terms of the *
6 * GNU General Public Licence (GPL) version 3, *
7 * copied verbatim in the file "LICENSE". *
8 * *
9 * In applying this license GSI does not waive the privileges and immunities *
10 * granted to it by virtue of its status as an Intergovernmental Organization *
11 * or submit itself to any jurisdiction. *
12 ******************************************************************************/
13
14#include "R3BEventHeader.h"
16#include "R3BException.h"
17#include "R3BFileSource2.h"
20#include "R3BShared.h"
21#include <FairLogger.h>
22#include <FairParRootFileIo.h>
23#include <FairRootFileSink.h>
24#include <FairRunAna.h>
25#include <FairRuntimeDb.h>
26#include <R3BLogger.h>
28#include <R3BProgramOptions.h>
29#include <TStopwatch.h>
30#include <cstdlib>
31#include <exception>
32#include <filesystem>
33#include <fmt/core.h>
34#include <fmt/format.h>
35#include <iostream>
36#include <memory>
37#include <string>
38
39namespace fs = std::filesystem;
40constexpr int DEFAULT_EVENT_NUM = -1;
41constexpr int DEFAULT_RUN_ID = 999;
42constexpr int DEFAULT_MIN_STAT = 20;
43
44using namespace std::string_literals;
45auto main(int argc, char** argv) -> int
46{
47 auto timer = TStopwatch{};
48 timer.Start();
49
50 auto programOptions = R3B::ProgramOptions("options for neuland exp data analysis");
51 auto help = programOptions.create_option<bool>("help,h", "help message", false);
52 auto enable_mille = programOptions.create_option<bool>("mille", "enable millepede", false);
53 auto logLevel = programOptions.create_option<std::string>("logLevel,v", "set log level of fairlog", "info");
54 auto input_file = programOptions.create_option<std::string>("in,i", "set the input files (regex)");
55 auto input_par = programOptions.create_option<std::string>("in-par,p", "set the input parameter");
56 auto output_file = programOptions.create_option<std::string>("out,o", "set the output file");
57 auto event_num = programOptions.create_option<int>("eventNum,n", "set the event number", DEFAULT_EVENT_NUM);
58 auto min_stat =
59 programOptions.create_option<int>("min-stat,m", "set minimun statistics for calibration", DEFAULT_MIN_STAT);
60 auto run_num = programOptions.create_option<int>("runNum,r", "set the number of runs", 1);
61 // Paula: additional flag
62
63 auto disable_task = programOptions.create_option<bool>("no-map2cal", "disable MapToCal task", false);
64
65 if (!programOptions.verify(argc, argv))
66 {
67 return EXIT_FAILURE;
68 }
69
70 FairLogger::GetLogger()->SetLogScreenLevel(logLevel().c_str());
71 const auto outputDir = R3B::GetParentDir(output_file());
72
73 const auto outputfile_path = fs::path{ output_file() };
74 const auto outputParFileName =
75 outputDir / fmt::format("{}.par{}", outputfile_path.stem().string(), outputfile_path.extension().string());
76 const auto input_filenames = R3B::GetFilesFromRegex(input_file());
77
78 R3BLOG(debug, fmt::format("input data file: {}", fmt::join(input_filenames, ";")).c_str());
79 R3BLOG(debug, fmt::format("input data par file: {}", input_par()).c_str());
80 R3BLOG(debug, fmt::format("output data file: {}", output_file()).c_str());
81 R3BLOG(debug, fmt::format("output data par file: {}", outputParFileName).c_str());
82
83 auto source = std::make_unique<R3BFileSource2>();
84 for (const auto& filename : input_filenames)
85 {
86 source->AddFile(filename);
87 }
88
89 auto sinkFile = R3B::make_rootfile(outputfile_path.c_str(), "RECREATE");
90 auto sink = std::make_unique<FairRootFileSink>(sinkFile.release());
91
92 auto run = std::make_unique<FairRunAna>();
93 source->SetRunId(DEFAULT_RUN_ID);
94 FairRootManager::SetTreeName("evt");
95
96 try
97 {
98 auto EvntHeader = std::make_unique<R3BEventHeader>();
99 run->SetEventHeader(EvntHeader.release());
100 run->SetSource(source.release());
101 run->SetSink(sink.release());
102
103 // Add analysis task --------------------------------------------------------
104
105 if (not disable_task.value())
106 {
107 auto runIdTask = std::make_unique<R3BEventHeaderPropagator>();
108 run->AddTask(runIdTask.release());
109 auto map2Cal = std::make_unique<R3B::Neuland::Map2CalTask>();
110 map2Cal->SetTrigger(R3B::Neuland::CalTrigger::all);
111 run->AddTask(map2Cal.release());
112 }
113
114 auto cal2hit_method =
116 auto cal2hitParTask = std::make_unique<R3B::Neuland::Cal2HitParTask>(cal2hit_method);
117 auto* cal2hitParTaskPtr = cal2hitParTask.get();
118 cal2hitParTaskPtr->SetTrigger(R3B::Neuland::CalTrigger::all);
119 cal2hitParTaskPtr->SetMinStat(min_stat.value());
120 run->AddTask(cal2hitParTask.release());
121
122 // set par input/output--------------------------------------------------------
123 auto* rtdb = run->GetRuntimeDb();
124
125 auto fileio = std::make_unique<FairParRootFileIo>();
126 fileio->open(input_par().c_str());
127 auto parOut = std::make_unique<FairParRootFileIo>(true);
128 parOut->open(outputParFileName.c_str());
129 rtdb->setFirstInput(fileio.release());
130 rtdb->setOutput(parOut.release());
131 rtdb->saveOutput();
132
133 run->Init();
134
135 // const auto error_scales = std::array{ 10.F };
136 // for (const auto error_scale : error_scales)
137 // {
138 // cal2hitParTaskPtr->SetErrorScale(error_scale);
139 // fmt::print("\nStarting run with error_scale {} ...\n\n", error_scale);
140 run->Run(0, event_num.value() <= 0 ? 0 : event_num.value());
141 // }
142
143 timer.Stop();
144 std::cout << "Cal level data written to file " << outputfile_path << "\n";
145 std::cout << "Cal level parameter written to file " << outputParFileName << "\n";
146 std::cout << "Macro finished successfully.\n";
147 std::cout << "Real time: " << timer.RealTime() << "s, CPU time: " << timer.CpuTime() << "s\n";
148 }
149 catch (R3B::runtime_error& ex)
150 {
151 std::cout << "A runtime error has occured: \n";
152 std::cerr << ex.what();
153 std::cout << "\n\n";
154 }
155 catch (R3B::logic_error& ex)
156 {
157 std::cout << "A logic error has occured: \n";
158 std::cerr << ex.what();
159 std::cout << "\n\n";
160 }
161 catch (std::exception& ex)
162 {
163 std::cout << "A error has occured: \n";
164 std::cerr << ex.what();
165 std::cout << "\n\n";
166 }
167 catch (...)
168 {
169 std::cout << "An unrecognized error has occured: \n";
170 std::cout << "\n\n";
171 }
172
173 auto* sinkfile = run->GetSink();
174 if (sinkfile != nullptr)
175 {
176 sinkfile->Close();
177 }
178 run->GetRuntimeDb()->writeContainers();
179
180 return 0;
181}
#define R3BLOG(severity, x)
Definition R3BLogger.h:32
auto make_rootfile(Args &&... args)
Definition R3BShared.h:94
auto GetParentDir(std::string_view filename) -> fs::path
Definition R3BShared.h:184
auto GetFilesFromRegex(std::string_view filename_regex) -> std::vector< std::string >
Definition R3BShared.h:204
constexpr int DEFAULT_EVENT_NUM
constexpr int DEFAULT_RUN_ID
auto main(int argc, char **argv) -> int
constexpr int DEFAULT_MIN_STAT