Project
Loading...
Searching...
No Matches
MilleRecordWriter.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
13
14#include <TFile.h>
15#include <TTree.h>
16
17#include "Framework/Logger.h"
18
20
21using namespace o2::fwdalign;
22
24
25//__________________________________________________________________________
27 : mDataTree(nullptr),
28 mDataFile(nullptr),
29 mIsSuccessfulInit(false),
30 mIsConstraintsRec(false),
31 mNEntriesAutoSave(10000),
32 mDataFileName("millerecords.root"),
33 mDataTreeName("o2sim"),
34 mDataBranchName("data"),
35 mRecord(nullptr),
36 mCurrentDataID(-1)
37{
39}
40
41//__________________________________________________________________________
43{
44 if (mDataFile) {
45 mDataFile->Close();
46 LOG(info) << "MilleRecordWriter - closed file "
47 << mDataFileName.Data();
48 }
49 if (mRecord) {
50 delete mRecord;
51 }
52}
53
54//__________________________________________________________________________
55void MilleRecordWriter::setCyclicAutoSave(const long nEntries)
56{
57 if (nEntries <= 0) {
58 return;
59 }
60 mNEntriesAutoSave = nEntries;
61}
62
63//__________________________________________________________________________
64void MilleRecordWriter::changeDataBranchName(const bool isConstraintsRec)
65{
66 mIsConstraintsRec = isConstraintsRec;
67 if (!mIsConstraintsRec) {
68 mDataBranchName = TString("data");
69 } else {
70 mDataBranchName = TString("constraints");
71 }
72}
73
74//__________________________________________________________________________
76{
77 mIsSuccessfulInit = false;
78
79 if (mDataFile == nullptr) {
80 mDataFile = new TFile(mDataFileName.Data(), "recreate", "", 505);
81 }
82
83 if ((!mDataFile) || (mDataFile->IsZombie())) {
84 LOGF(fatal,
85 "MilleRecordWriter::init() - failed to initialise records file %s!",
86 mDataFileName.Data());
87 return;
88 }
89 if (mDataTree == nullptr) {
90 mDataFile->cd();
91 mDataTree = new TTree(mDataTreeName.Data(), "records for MillePede2");
92 mDataTree->SetAutoSave(mNEntriesAutoSave); // flush the TTree to disk every N entries
93 const int bufsize = 32000;
94 const int splitLevel = 99; // "all the way"
95 mDataTree->Branch(mDataBranchName.Data(), "MillePedeRecord", mRecord, bufsize, splitLevel);
96 }
97 if (!mDataTree) {
98 LOG(fatal) << "MilleRecordWriter::init() - failed to initialise TTree !";
99 return;
100 }
101
102 if (!mIsConstraintsRec) {
103 LOGF(info,
104 "MilleRecordWriter::init() - file %s used for derivatives records",
105 mDataFileName.Data());
106 } else {
107 LOGF(info,
108 "MilleRecordWriter::init() - file %s used for constraints records",
109 mDataFileName.Data());
110 }
111 mIsSuccessfulInit = true;
112}
113
114//__________________________________________________________________________
115void MilleRecordWriter::fillRecordTree(const bool doPrint)
116{
117 if (!isInitOk()) {
118 LOG(warning) << "MilleRecordWriter::fillRecordTree() - aborted, init was not ok !";
119 return;
120 }
121 mDataTree->Fill();
123 if (doPrint) {
124 LOGF(info, "MilleRecordWriter::fillRecordTree() - added entry %i", mCurrentDataID);
125 mRecord->Print();
126 }
127 mRecord->Reset();
128}
129
130//__________________________________________________________________________
132{
133 if (mDataFile && mDataFile->IsWritable() && mDataTree) {
134 mDataFile->cd();
135 mDataTree->Write();
136 LOG(info) << "MilleRecordWriter::terminate() - wrote tree "
137 << mDataTreeName.Data();
138 mDataFile->Close();
139 }
140}
141
142//_____________________________________________________________________________
144{
145 mRecord->SetWeight(wgh);
146}
147
148//_____________________________________________________________________________
150{
151 mRecord->SetRunID(run);
152}
ClassImp(o2::fwdalign::MilleRecordWriter)
Class dedicated to write MillePedeRecords to output file for FWDALIGN.
Store residuals and local/global deriavtives from a single track processing.
void Print(const Option_t *opt="") const override
print itself
TString mDataFileName
name of the output file that will store the record TTree
void setCyclicAutoSave(const long nEntries)
Set the number of entries to be used by TTree::AutoSave()
bool isInitOk() const
check if init went well
Long64_t mCurrentDataID
counter increasing when adding a record to the tree
void init()
init output file and tree
TTree * mDataTree
TTree container that stores the records.
bool mIsSuccessfulInit
boolean to monitor the success of the initialization
o2::fwdalign::MillePedeRecord * mRecord
the running record
bool mIsConstraintsRec
boolean to know if these are data records or constraints records
TFile * mDataFile
output file where the records are written
TString mDataTreeName
name of the record TTree
void setRecordRun(int run)
assign run
TString mDataBranchName
name of the branch where records will be stored
long mNEntriesAutoSave
max entries in the buffer after which TTree::AutoSave() is automatically used
void terminate()
write tree and close output file
void changeDataBranchName(const bool isConstraintsRec=true)
choose data records filename
void setRecordWeight(double wgh)
assign weight
void fillRecordTree(const bool doPrint=false)
fill tree
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"