BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
run_ana_bmn.C
Go to the documentation of this file.
1// Macro for physics analysis of simulated and experimental BM@N events
2// dstFileName - input DST file with data (MC or exp. data), a ROOT file with a TChain object of DST files or
3// text file with a list of DST files; run info, mag.field and pars are taken from the first file
4// anaFileName - output file with analysis results
5// nStartEvent - number of first event to process (starts with zero), default: 0
6// nEvents - number of events to process after nStartEvent, 0 - all events of the given file(s) after nStartEvent
7void run_ana_bmn(TString dstFileName = "$VMCWORKDIR/macro/run/bmndst.root",
8 TString anaFileName = "$VMCWORKDIR/macro/run/bmnana.root",
9 Int_t nStartEvent = 0,
10 Int_t nEvents = 10)
11{
12 gDebug = 0; // ROOT debug option
13 // Verbosity level (0 = summary only, 1 = event-level, 2 = track-level, 3 = full debug)
14 Int_t iVerbose = 0;
15 // Log level for LOG(severity): "fatal", "error", "alarm", "important", "warn", "state", "info", "detail", "debug",
16 // "debug[1-4]", "trace", "nolog"
17 FairLogger::GetLogger()->SetLogScreenLevel("info");
18
19 // ----- Timer ---------------------------------------------------------
20 TStopwatch timer;
21 timer.Start();
22
23 // get the list of input DST files (input file can also contain a TChain or a text list of DST files)
24 vector<TString> dstFileList;
25 if (BmnFunctionSet::GetInputFileList(dstFileName, dstFileList) < 0)
26 exit(-1);
27 // the first input file is used to get the run information, magnetic field and parameters
28 TString firstFileName = dstFileList[0];
29
30 // ----- Analysis run --------------------------------------------
31 FairRunAna* fRunAna = new FairRunAna();
32 // fRunAna->SetEventHeader(new AnaEventHeader());
33
34 // 'isExp' flag for FairRunAna tasks whether simulation or experimental data are used
35 Bool_t isExp = (BmnFunctionSet::isSimulationFile(firstFileName) == 0);
36
37 // get field value and run information (beam, target, energy) from the DST file
38 Int_t fieldType = -1;
39 Double_t fieldScale = 0., beam_energy = 0.;
40 TString beam = "", target = "";
41 FairField* magField =
42 BmnFunctionAna::GetDSTField(firstFileName, fieldScale, fieldType, &beam, &target, &beam_energy);
43 if (magField != nullptr)
44 fRunAna->SetField(magField);
45
46 // Declare input source as simulation file or experimental data
47 Int_t run_period = -1, run_number = -1;
48 BmnFileSource* fFileSource = new BmnFileSource(firstFileName, run_period, run_number);
49 // the rest input files are chained to the first one (their branch structure must be the same)
50 for (size_t i = 1; i < dstFileList.size(); i++)
51 fFileSource->AddFile(dstFileList[i]);
52 fRunAna->SetSource(fFileSource);
53 // parameters are read only from the first file, so do not reinitialize them when the run ID changes in the chain
54 if (dstFileList.size() > 1)
55 fRunAna->SetContainerStatic(kTRUE);
56
57 // print the run summary for both experimental and simulation data
58 TString title = isExp ? "EXPERIMENTAL RUN SUMMARY" : "SIMULATION RUN SUMMARY";
59 Int_t nSideBars = (58 - title.Length() - 2) / 2; // the summary box is 58 symbols wide
60 cout << "\n\n"
61 << string(nSideBars, '|') << " " << title << " " << string(58 - nSideBars - title.Length() - 2, '|') << endl;
62 cout << "||\t\t\t\t\t\t\t||" << endl;
63 cout << left;
64 cout << "||\t\tPeriod:\t\t" << setw(24) << run_period << "||" << endl;
65 if (isExp)
66 cout << "||\t\tNumber:\t\t" << setw(24) << run_number << "||" << endl;
67 cout << "||\t\tBeam:\t\t" << setw(24) << (beam == "" ? "-" : beam.Data()) << "||" << endl;
68 cout << "||\t\tEnergy:\t\t" << setw(24)
69 << ((beam_energy > 0) ? TString::Format("%.2f AGeV", beam_energy).Data() : "-") << "||" << endl;
70 cout << "||\t\tTarget:\t\t" << setw(24) << (target == "" ? "-" : target.Data()) << "||" << endl;
71 cout << "||\t\tField scale:\t" << setw(24) << setprecision(4) << fieldScale << "||" << endl;
72 if (dstFileList.size() > 1)
73 cout << "||\t\tInput files:\t" << setw(24) << dstFileList.size() << "||" << endl;
74 cout << "||\t\t\t\t\t\t\t||" << endl;
75 cout << string(58, '|') << "\n\n" << endl;
76
77 // if directory for the output file does not exist, then create
78 if (BmnFunctionSet::CreateDirectoryTree(anaFileName, 1) < 0)
79 exit(-2);
80 fRunAna->SetSink(new FairRootFileSink(anaFileName));
81 fRunAna->SetGenerateRunInfo(false);
82
83 // if nEvents is equal 0 then all events of the given file(s) starting with "nStartEvent" should be processed
84 if (nEvents == 0) {
85 for (auto& fileName : dstFileList) {
86 Int_t nFileEvents = MpdGetNumEvents::GetNumROOTEvents(fileName.Data());
87 if (nFileEvents < 0)
88 exit(-3);
89 nEvents += nFileEvents;
90 }
91 nEvents -= nStartEvent;
92 }
93
94 // Add TObjectString file names to a TList which is passed as input to the FairParAsciiFileIo
95 // The FairParAsciiFileIo creates a concatenated input parameter file all_<pid>.par, which is then can be used
96 // TList* parFileNameList = new TList();
97
98 // ====================================================================== //
99 // === Analysis Tasks === //
100 // ====================================================================== //
101 CbmKF* kalman_filter = new CbmKF();
102 fRunAna->AddTask(kalman_filter);
103
104 FairTask* ana_task = new BmnAnalysisTask();
105 fRunAna->AddTask(ana_task);
106
107 // ----- Parameter database (input) -------------------------------------
108 FairRuntimeDb* rtdb = fRunAna->GetRuntimeDb();
109 FairParRootFileIo* parIo1 = new FairParRootFileIo();
110 // FairParAsciiFileIo* parIo2 = new FairParAsciiFileIo();
111 parIo1->open(firstFileName.Data());
112 // parIo2->open(parFileNameList, "in");
113 rtdb->setFirstInput(parIo1);
114 // rtdb->setSecondInput(parIo2);
115 // -------------------------------------------------------------------------
116
117 // ----- Initialize FaiRunAna --------------------------------------------
118 fRunAna->GetMainTask()->SetVerbose(iVerbose);
119 fRunAna->Init(); // geometry is loaded from DST FairGeoParSet container
120 // -------------------------------------------------------------------------
121
122 // ----- Parameter database (output) ------------------------------------
123 FairParRootFileIo* parOut = new FairParRootFileIo(kTRUE);
124 parOut->open(gFile);
125 rtdb->setOutput(parOut);
126 rtdb->writeContainer(rtdb->getContainer("FairGeoParSet"), rtdb->getCurrentRun());
127 rtdb->saveOutput();
128 // rtdb->print();
129 // -------------------------------------------------------------------------
130
131 // ----- Run FairRunAna -------------------------------------------------
132 fRunAna->Run(nStartEvent, nStartEvent + nEvents);
133 // -------------------------------------------------------------------------
134
135 // ----- Finish --------------------------------------------------------
136 delete fRunAna;
137 timer.Stop();
138 Double_t rtime = timer.RealTime();
139 Double_t ctime = timer.CpuTime();
140 cout << endl << endl;
141 cout << "Macro finished successfully." << endl; // marker of successful execution
142 if (dstFileList.size() > 1)
143 cout << "Input file list is " << dstFileName << " (" << dstFileList.size() << " files)" << endl;
144 else
145 cout << "Input file is " + firstFileName << endl;
146 cout << "Output file is " + anaFileName << endl;
147 cout << "Real time " << rtime << " s, CPU time " << ctime << " s" << endl;
148 cout << endl;
149 // ------------------------------------------------------------------------
150}
int i
Definition P4_F32vec4.h:22
bool Bool_t
int Int_t
Int_t gDebug
double Double_t
constexpr Bool_t kTRUE
#define gFile
BmnFileSource.h.
void AddFile(TString FileName)
Add ROOT file to input, the file will be chained to already added files.
static FairField * GetDSTField(TString fileName, Double_t &fieldScale, Int_t &fieldType, TString *beam=nullptr, TString *target=nullptr, Double_t *beam_energy=nullptr, Int_t *beam_a=nullptr, Int_t *beam_z=nullptr, Int_t *target_a=nullptr, Int_t *target_z=nullptr)
static int CreateDirectoryTree(TString &fileName, int iVerbose=0, EAccessMode mode=kWritePermission)
static int isSimulationFile(TString fileName="")
static int GetInputFileList(TString inputFileName, std::vector< TString > &fileList)
The CbmKF Kalman Filter algorithm.
Definition CbmKF.h:28
static Int_t GetNumROOTEvents(const char *filename, const char *treename="", int iVerbose=2)
Double_t RealTime()
void Start(Bool_t reset=kTRUE)
Double_t CpuTime()
void Stop()
Ssiz_t Length() const
const char * Data() const
static TString Format(const char *fmt,...)
void run_ana_bmn(TString dstFileName="$VMCWORKDIR/macro/run/bmndst.root", TString anaFileName="$VMCWORKDIR/macro/run/bmnana.root", Int_t nStartEvent=0, Int_t nEvents=10)
Definition run_ana_bmn.C:7