er  dev
ERDigibuilder.cxx
1 #include "ERDigibuilder.h"
2 
3 #include <iostream>
4 
5 #include "TObjArray.h"
6 #include "TTree.h"
7 #include "TTreeCache.h"
8 
9 #include "FairRootManager.h"
10 #include "FairRun.h"
11 #include "FairLogger.h"
12 
13 #include "DetEventFull.h"
14 #include "DetEventDetector.h"
15 #include "DetEventStation.h"
16 #include "DetMessage.h"
17 #include "DetEventCommon.h"
18 #include "TGo4EventElement.h"
19 
20 #include "ERBeamTimeEventHeader.h"
21 
22 //--------------------------------------------------------------------------------------------------
23 ERDigibuilder::ERDigibuilder()
24  : input_chain_of_events_("accdaq")
25 {}
26 //--------------------------------------------------------------------------------------------------
27 ERDigibuilder::ERDigibuilder(const TString& tree_name)
28  : input_chain_of_events_(tree_name)
29 {}
30 //--------------------------------------------------------------------------------------------------
31 void ERDigibuilder::AddFile(const TString& path) {
32  input_chain_of_events_.Add(path);
33  if (!setup_configuration_) {
34  TFile file(path);
35  std::cerr << setup_configuration_ << std::endl;
36  setup_configuration_ = dynamic_cast<SetupConfiguration*>(file.Get("SetupConfiguration"));
37  std::cerr << setup_configuration_ << std::endl;
38  if (!setup_configuration_)
39  LOG(FATAL) << "Cannot load setup configuration from file " << path << FairLogger::endl;
40  }
41 }
42 //--------------------------------------------------------------------------------------------------
43 Bool_t ERDigibuilder::Init() {
44  if (input_chain_of_events_.GetEntriesFast() == 0)
45  LOG(FATAL) << "No events for processing in source ERDigibuilder" << FairLogger::endl;
46  ApplyUserCut();
47  CheckEventHeader();
48  InitUnpackers();
49  ConnectEventCommon();
50  return kTRUE;
51 }
52 //--------------------------------------------------------------------------------------------------
53 Bool_t ERDigibuilder::InitUnpackers(){
54  const std::map<TString, unsigned short> detectors = setup_configuration_->GetDetectorList();
55  for (const auto& detector : detectors) {
56  const auto detector_name = detector.first;
57  if (unpacks_.find(detector_name) != unpacks_.end()) {
58  unpacks_[detector_name]->Init(setup_configuration_, input_chain_of_events_);
59  } else {
60  LOG(WARNING) << "[Digibuilder] " << detector_name << " is defined in setup "
61  << "configuration, but unpacker is not added!" << FairLogger::endl;
62  }
63  }
64  for (const auto& detector_name_and_unpack : unpacks_) {
65  const auto& detector_name = detector_name_and_unpack.first;
66  const auto& unpack = detector_name_and_unpack.second;
67  if (!unpack->IsInited()) {
68  LOG(WARNING) << "[Digibuilder] Detector " << detector_name << " not found in setup file. "
69  << "Unpacker has not inited!" << FairLogger::endl;
70  }
71  }
72  return kTRUE;
73 }
74 //--------------------------------------------------------------------------------------------------
75 Int_t ERDigibuilder::ReadEvent(UInt_t) {
76  if (!common_part_of_event_)
77  LOG(FATAL) << "Object fot store common part of event was not inited" << FairLogger::endl;
78  Reset();
79  const auto event_number = EventNumber();
80  if (!LoadEvent(event_number))
81  return 1; // all events have been already processed
82  FairRun* run = FairRun::Instance();
83  auto* header = dynamic_cast<ERBeamTimeEventHeader*>(run->GetEventHeader());
84  if (!EventShouldBeProcessed(event_number, header))
85  return 0;
86  header->SetTrigger(common_part_of_event_->trigger);
87  for (auto& detector_name_and_unpack : unpacks_) {
88  const auto& detector_name = detector_name_and_unpack.first;
89  auto& unpack = detector_name_and_unpack.second;
90  if (unpack->IsInited()) {
91  unpack->DoUnpack(nullptr, 0);
92  }
93  }
94  return 0;
95 }
96 //--------------------------------------------------------------------------------------------------
97 uint ERDigibuilder::EventNumber() {
98  FairRootManager* ioman = FairRootManager::Instance();
99  if (!ioman)
100  LOG(FATAL) << "No FairRootManager" << FairLogger::endl;
101  return static_cast<uint>(ioman->GetEntryNr());
102 }
103 //--------------------------------------------------------------------------------------------------
104 bool ERDigibuilder::LoadEvent(const uint event_number) {
105  if (event_number % 10000 == 0)
106  LOG(INFO) << "[Digibuilder] Event " << event_number << FairLogger::endl;
107  return input_chain_of_events_.GetEntry(event_number);
108 }
109 //--------------------------------------------------------------------------------------------------
110 void ERDigibuilder::Close() {
111  LOG(INFO) << "[Digibuilder] " << EventNumber() << " events were processed" << FairLogger::endl;
112 }
113 //--------------------------------------------------------------------------------------------------
114 void ERDigibuilder::Reset(){
115  for (auto& detector_name_and_unpack : unpacks_) {
116  auto& unpack = detector_name_and_unpack.second;
117  unpack->Reset();
118  }
119 }
120 //--------------------------------------------------------------------------------------------------
121 void ERDigibuilder::ApplyUserCut(){
122  if (!UserCutIsDefined())
123  return;
124  events_for_processing_ = new TH1I("events_for_processing", "Events for processing",
125  input_chain_of_events_.GetEntries(), 1,
126  input_chain_of_events_.GetEntries());
127  if (input_chain_of_events_.Draw("Entry$>>events_for_processing", user_cut_, "goff") == -1)
128  LOG(FATAL) << "[Digibuilder] Error in user cut expression: " << user_cut_ << FairLogger::endl;
129 }
130 //--------------------------------------------------------------------------------------------------
131 bool ERDigibuilder::UserCutIsDefined() const {
132  return user_cut_ != "";
133 }
134 //--------------------------------------------------------------------------------------------------
135 bool ERDigibuilder::EventShouldBeProcessed(const uint event_number,
136  ERBeamTimeEventHeader* header) const {
137  if (!UserCutIsDefined())
138  return true;
139  if (!events_for_processing_->GetBinContent(event_number)) {
140  LOG(DEBUG) << "[Digibuilder] Event is skipped due user cut. " << FairLogger::endl;
141  if (header)
142  header->SetTrigger(-1);
143  if (!hold_events_count_) {
144  FairRun* run = FairRun::Instance();
145  run->MarkFill(kFALSE);
146  }
147  return false;
148  }
149  return true;
150 }
151 //--------------------------------------------------------------------------------------------------
152 void ERDigibuilder::CheckEventHeader() {
153  FairRun* run = FairRun::Instance();
154  if (!dynamic_cast<ERBeamTimeEventHeader*>(run->GetEventHeader())) {
155  LOG(WARNING) << "[Digibuilder] ERBeamTimeEventHeader is not used in digibuilding. "
156  << "Trigger and another common information will not be written to output file."
157  << FairLogger::endl;
158  }
159 }
160 //--------------------------------------------------------------------------------------------------
161 void ERDigibuilder::ConnectEventCommon() {
162  if (!input_chain_of_events_.FindBranch("common")) {
163  LOG(FATAL) << "Input file does not contain branch common with trigger"
164  << " and another common information" << FairLogger::endl;
165  }
166  common_part_of_event_ = new EventCommon();
167  input_chain_of_events_.SetBranchAddress("common", &common_part_of_event_);
168 }
169 //--------------------------------------------------------------------------------------------------
170 void ERDigibuilder::SetUserCut(const TCut& cut, const bool hold_events_count/*= true*/){
171  user_cut_ = cut;
172  hold_events_count_ = hold_events_count;
173 }
174 //--------------------------------------------------------------------------------------------------
175 ClassImp(ERDigibuilder)