ra4_draw  4bd0201e3d922d42bd545d4b045ed44db33454a4
event_scan.cpp
Go to the documentation of this file.
1 #include "core/event_scan.hpp"
2 
3 #include <iostream>
4 #include <iomanip>
5 
6 #include <sys/stat.h>
7 
8 #include "core/utilities.hpp"
9 
10 using namespace std;
11 
13  const shared_ptr<Process> &process):
14  FigureComponent(event_scan, process),
15  out_((CodeToPlainText(event_scan.name_+"_SCAN_"+process->name_)+".txt").c_str()),
16  full_cut_(event_scan.cut_ && process->cut_),
17  cut_vector_(),
18  val_vectors_(event_scan.columns_.size()),
19  row_(0){
20  out_.precision(event_scan.Precision());
21 }
22 
24  const EventScan &scan = static_cast<const EventScan&>(figure_);
25  int w = scan.width_;
26 
27  if(full_cut_.IsScalar()){
28  if(!full_cut_.GetScalar(baby)) return;
29  }else{
31  }
32 
33  size_t max_size = 0;
34  for(size_t icol = 0; icol < scan.columns_.size(); ++icol){
35  const NamedFunc& col = scan.columns_.at(icol);
36  if(col.IsScalar()){
37  if(max_size < 1) max_size = 1;
38  }else{
39  val_vectors_.at(icol) = col.GetVector(baby);
40  if(val_vectors_.at(icol).size() > max_size){
41  max_size = val_vectors_.at(icol).size();
42  }
43  }
44  }
45  if(full_cut_.IsVector() && max_size > cut_vector_.size()){
46  max_size = cut_vector_.size();
47  }
48 
49  if(max_size > 0 && !(row_ & 0x7)){
50  out_ << " Row Instance";
51  for(const auto &col: scan.columns_){
52  out_ << ' ' << setw(w) << col.Name().substr(0,scan.width_);
53  }
54  out_.put('\n');
55  }
56 
57  for(size_t instance = 0; instance < max_size; ++instance){
58  out_ << setw(9) << row_ << ' ' << setw(8) << instance;
59  for(size_t icol = 0; icol < scan.columns_.size(); ++icol){
60  const NamedFunc& col = scan.columns_.at(icol);
61  if(col.IsScalar()){
62  out_ << ' ' << setw(w) << col.GetScalar(baby);
63  }else{
64  if(instance < val_vectors_.at(icol).size()){
65  out_ << ' ' << setw(w) << val_vectors_.at(icol).at(instance);
66  }else{
67  out_ << ' ' << setw(w) << ' ';
68  }
69  }
70  }
71  out_.put('\n');
72  }
73 
74  if(max_size > 0) ++row_;
75 }
76 
77 void EventScan::SingleScan::Precision(unsigned precision){
78  out_.precision(precision);
79 }
80 
81 EventScan::EventScan(const string &name,
82  const NamedFunc &cut,
83  const vector<NamedFunc> &columns,
84  const vector<shared_ptr<Process> > &processes,
85  unsigned precision):
86  name_(name),
87  cut_(cut),
88  columns_(columns),
89  scans_(),
90  precision_(precision),
91  width_(precision+6){
92  for(const auto& proc: processes){
93  scans_.emplace_back(new SingleScan(*this, proc));
94  }
95 }
96 
97 void EventScan::Print(double /*luminosity*/,
98  const std::string & /*subdir*/){
99  for(const auto &scan: scans_){
100  cout << "less " << (CodeToPlainText(name_+"_SCAN_"+scan->process_->name_)+".txt") << endl;
101  }
102 }
103 
104 set<const Process*> EventScan::GetProcesses() const{
105  set<const Process *> processes;
106  for(const auto &scan: scans_){
107  processes.insert(scan->process_.get());
108  }
109  return processes;
110 }
111 
113  for(const auto &scan: scans_){
114  if(scan->process_.get() == process) return scan.get();
115  }
116  return nullptr;
117 }
118 
119 unsigned EventScan::Precision() const{
120  return precision_;
121 }
122 
123 EventScan & EventScan::Precision(unsigned precision){
124  precision_ = precision;
125  width_ = precision + 6;
126  for(auto &scan: scans_){
127  scan->Precision(precision);
128  }
129  return *this;
130 }
unsigned precision_
Decimal places to print.
Definition: event_scan.hpp:63
std::set< const Process * > GetProcesses() const final
Definition: event_scan.cpp:104
std::string CodeToPlainText(std::string code)
Definition: utilities.cpp:79
unsigned Precision() const
Definition: event_scan.cpp:119
Abstract base class for access to ntuple variables.
Definition: baby.hpp:16
ScalarType GetScalar(const Baby &b) const
Evaluate scalar function with b as argument.
Definition: named_func.cpp:460
STL namespace.
Combines a callable function taking a Baby and returning a scalar or vector with its string represent...
Definition: named_func.hpp:13
const Figure & figure_
Reference to figure containing this component.
Definition: figure.hpp:22
EventScan()=delete
bool IsScalar() const
Check if scalar function is valid.
Definition: named_func.cpp:442
void RecordEvent(const Baby &baby) final
Definition: event_scan.cpp:23
FigureComponent * GetComponent(const Process *process) final
Definition: event_scan.cpp:112
void Print(double luminosity, const std::string &subdir) final
Definition: event_scan.cpp:97
std::vector< NamedFunc::VectorType > val_vectors_
Values for each column (to avoid creating new vectors each event)
Definition: event_scan.hpp:34
std::ofstream out_
File to which results are printed.
Definition: event_scan.hpp:31
VectorType GetVector(const Baby &b) const
Evaluate vector function with b as argument.
Definition: named_func.cpp:470
void Precision(unsigned precision)
Definition: event_scan.cpp:77
NamedFunc cut_
Cut restricting printed events/objects.
Definition: event_scan.hpp:58
NamedFunc::VectorType cut_vector_
Cut results (to avoid creating new vector each event)
Definition: event_scan.hpp:33
std::vector< std::unique_ptr< SingleScan > > scans_
One scan for each process.
Definition: event_scan.hpp:62
unsigned width_
Width of column in characters. Determined from precision.
Definition: event_scan.hpp:64
bool IsVector() const
Check if vectorr function is valid.
Definition: named_func.cpp:450
std::string name_
Name of scan for saving to file.
Definition: event_scan.hpp:57
std::vector< NamedFunc > columns_
Variables to print.
Definition: event_scan.hpp:59
NamedFunc full_cut_
Cached scan&&process cut.
Definition: event_scan.hpp:32