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