HighLAND
Experiment.cxx
1 #include "Experiment.hxx"
2 
3 #include <sstream>
4 #include <iostream>
5 #include <fstream>
6 
7 //********************************************************************
8 SampleGroup::SampleGroup(const std::string& name){
9 //********************************************************************
10 
11  _name = name;
12  _dataSamples.clear();
13 }
14 
15 //********************************************************************
16 SampleGroup::SampleGroup(const std::string& name, const std::string& dataFile, std::map<std::string, std::string>& mcFiles){
17 //********************************************************************
18 
19  _name = name;
20  AddDataSample(dataFile);
21  AddMCSamples(mcFiles);
22 }
23 
24 //********************************************************************
25 SampleGroup::SampleGroup(const std::string& name, DataSample* dataSample, std::map<std::string, DataSample*>& mcSamples){
26 //********************************************************************
27 
28  _name = name;
29  AddDataSample(dataSample);
30  AddMCSamples(mcSamples);
31 }
32 
33 //**************************************************
34 void SampleGroup::AddDataSample(const std::string& file){
35 //**************************************************
36 
37  _dataSamples.push_back(new DataSample(file));
38 }
39 
40 //**************************************************
42 //**************************************************
43 
44  _dataSamples.push_back(sample);
45 }
46 
47 //**************************************************
48 void SampleGroup::AddMCSample(const std::string& name, const std::string& file){
49 //**************************************************
50 
51  _mcSamples[name] = new DataSample(file);
52 }
53 
54 //**************************************************
55 void SampleGroup::AddMCSample(const std::string& name, DataSample* sample){
56 //**************************************************
57 
58  _mcSamples[name] = sample;
59 }
60 
61 
62 //**************************************************
63 void SampleGroup::AddMCSamples(std::map<std::string, std::string>& mcFiles){
64 //**************************************************
65 
66  std::map< std::string, std::string>::iterator it;
67  for (it = mcFiles.begin(); it != mcFiles.end(); it++) {
68  std::string name = it->first;
69  std::string file = it->second;
70  AddMCSample(name,file);
71  }
72 }
73 
74 //**************************************************
75 void SampleGroup::AddMCSamples(std::map<std::string, DataSample*>& mcSamples){
76 //**************************************************
77 
78  std::map< std::string, DataSample*>::iterator it;
79  for (it = mcSamples.begin(); it != mcSamples.end(); it++) {
80  std::string name = it->first;
81  DataSample* sample = it->second;
82  AddMCSample(name,sample);
83  }
84 }
85 
86 //********************************************************************
87 bool SampleGroup::HasMCSample(const std::string& name){
88 //********************************************************************
89 
90  if (_mcSamples.find(name)==_mcSamples.end()){
91  std::cout << "MC sample '" << name << "' does not exists in SampleGroup '" << _name << "' !!!" << std::endl;
92  return false;
93  }
94  return true;
95 }
96 
97 //**************************************************
98 void SampleGroup::GetPOT(Float_t& POTdata, Float_t& POTmc) {
99 //**************************************************
100 
101  Float_t POTsand;
102  GetPOT(POTdata, POTmc, POTsand);
103 }
104 
105 //**************************************************
106 void SampleGroup::GetPOT(Float_t& POTdata, Float_t& POTmc, Float_t& POTsand) {
107 //**************************************************
108 
109  if(GetDataSample() != NULL){
110  POTdata = GetDataSample()->GetPOT();
111  }
112 
113  POTmc = 0;
114  std::map< std::string, DataSample*>::iterator mit;
115  std::map< std::string, DataSample*>& mcSamples = GetMCSamples();
116 
117  for (mit = mcSamples.begin(); mit != mcSamples.end(); mit++) {
118  if((mit->first).find("and") != std::string::npos){
119  DataSample* sample = mit->second;
120  Float_t pot = sample->GetPOT();
121  if(sample->GetPOT() < 1e17) pot = pot*2.5e17;
122  POTsand += pot;
123  }
124  else{
125  DataSample* sample = mit->second;
126  POTmc += sample->GetPOT();
127  }
128  }
129 }
130 
131 //**************************************************
132 void SampleGroup::SetCurrentTree(const std::string& name){
133 //**************************************************
134 
135  std::vector<DataSample*>::iterator it;
136  for (it = _dataSamples.begin(); it != _dataSamples.end(); it++) {
137  (*it)->SetCurrentTree(name);
138  }
139 
140 
141  std::map< std::string, DataSample*>::iterator mit;
142  for (mit = _mcSamples.begin(); mit != _mcSamples.end(); mit++) {
143  DataSample* sample = mit->second;
144  sample->SetCurrentTree(name);
145  }
146 }
147 
148 //********************************************************************
149 Experiment::Experiment(const std::string& name){
150 //********************************************************************
151 
152  _name = name;
153  _filePath = "";
154  _tree="default";
155 }
156 
157 //**************************************************
158 Experiment::Experiment(const std::string& name, const std::string& file1, const std::string& file2) {
159 //**************************************************
160 
161  _name=name;
162  std::map<std::string, std::string> files2;
163  files2["mc"]=file2;
164  _sampleGroups["group"] = SampleGroup("group", file1, files2);
165  _filePath = file1;
166 }
167 
168 //**************************************************
169 Experiment::Experiment(const std::string& name, const std::string& configfile) {
170 //**************************************************
171  _name = name;
172  _tree="default";
173 
174 
175  _dataFiles.clear();
176  _mcFiles.clear();
177 
178  std::ifstream infile(configfile.c_str());
179 
180  while (infile) {
181  std::string line;
182  if (!getline(infile, line))
183  break;
184 
185  std::istringstream ssline(line);
186  std::vector<std::string> record;
187 
188  while (ssline) {
189  std::string token;
190  if (!getline(ssline, token, ' ')) break;
191  record.push_back(token);
192  }
193 
194  if (record.size() < 3) {
195  std::cerr << "Invalid format (should be sampleName dataName:dataFile mcName:mcFile [mcName2:mcFile2])" << std::endl;
196  std::cerr << "Saw: " << line << std::endl;
197  continue;
198  }
199 
200  std::string groupName = record[0];
201  std::string dataFile = record[1].replace(0, record[1].find(":") + 1, "");
202  std::cout << "Adding group " << groupName << " with data file " << dataFile << " and MC files ";
203 
204  std::map<std::string, std::string> mcFiles;
205  for (unsigned int i = 2; i < record.size(); i++) {
206  std::string mcName = record[i];
207  std::string mcFile = record[i];
208  mcName = mcName.replace(mcName.find(":"), mcName.size(), "");
209  mcFile = mcFile.replace(0, mcFile.find(":") + 1, "");
210  mcFiles[mcName] = mcFile;
211  std::cout << mcFile << " ";
212  }
213 
214  std::cout << std::endl;
215 
216  _sampleGroups[groupName] = SampleGroup(groupName, dataFile, mcFiles);
217  _filePath = dataFile;
218  }
219 }
220 
221 //********************************************************************
222 bool Experiment::HasSampleGroup(const std::string& name){
223 //********************************************************************
224 
225  if (_sampleGroups.find(name)==_sampleGroups.end()){
226  std::cout << "SampleGroup '" << name << "' does not exists in Experiment '" << _name << "' !!!" << std::endl;
227  return false;
228  }
229  return true;
230 }
231 
232 //**************************************************
234 //**************************************************
235 
236  std::map< std::string, DataSample*>& mcSamples = sampleGroup.GetMCSamples();
237  for (std::map< std::string, DataSample*>::iterator it = mcSamples.begin(); it != mcSamples.end(); it++) {
238  _mcFiles.push_back(it->second->GetFilePath());
239  }
240 
241  std::vector<DataSample*>& dataSamples = sampleGroup.GetDataSamples();
242  for (std::vector<DataSample*>::iterator it2 = dataSamples.begin(); it2 != dataSamples.end(); it2++) {
243  _dataFiles.push_back((*it2)->GetFilePath());
244  }
245 }
246 
247 //**************************************************
248 void Experiment::AddSampleGroup(const std::string& name, SampleGroup& sampleGroup){
249 //**************************************************
250 
251  AddSampleFilesToVector(sampleGroup);
252  _sampleGroups[name] = sampleGroup;
253 
254 }
255 
256 //**************************************************
257 void Experiment::AddSampleGroup(const std::string& name, const std::string& dataFile, std::map<std::string, std::string>& mcFiles){
258 //**************************************************
259 
260  _sampleGroups[name] = SampleGroup(name, dataFile, mcFiles);
261  AddSampleFilesToVector(_sampleGroups[name]);
262  _filePath = dataFile;
263 }
264 
265 //**************************************************
266 void Experiment::AddSampleGroup(const std::string& name, DataSample* dataSample, std::map<std::string, DataSample*>& mcSamples){
267 //**************************************************
268 
269  _sampleGroups[name] = SampleGroup(name, dataSample, mcSamples);
270  AddSampleFilesToVector(_sampleGroups[name]);
271 
272 }
273 
274 //**************************************************
275 void Experiment::SetCurrentTree(const std::string& name){
276 //**************************************************
277 
278  std::map< std::string, SampleGroup >::iterator sit;
279  for (sit = _sampleGroups.begin(); sit != _sampleGroups.end(); sit++) {
280  SampleGroup& sampleGroup = sit->second;
281  sampleGroup.SetCurrentTree(name);
282  }
283  _tree = name;
284 }
285 
286 //**************************************************
287 void Experiment::DumpPOT(const std::string& name){
288 //**************************************************
289 
290 
291  SampleGroup& sampleGroup = _sampleGroups[name];
292 
293  std::cout << "---------------------------------------------" << std::endl;
294  std::cout << "-------- POT info for sample pair '"<< name << "' ---------" << std::endl;
295  std::cout << "---------------------------------------------" << std::endl;
296  if (sampleGroup.GetDataSample()){
297  std::cout << "data sample:" << std::endl;
298  sampleGroup.GetDataSample()->DumpPOT();
299  std::cout << std::endl;
300  }
301 
302  std::cout << "-------- MC samples -------------------------" << std::endl;
303  std::map< std::string, DataSample*>::iterator it;
304 
305  std::map< std::string, DataSample*>& mcSamples = sampleGroup.GetMCSamples();
306  for (it = mcSamples.begin(); it != mcSamples.end(); it++) {
307  std::string name = it->first;
308  DataSample* sample = it->second;
309 
310  std::cout << "MC sample: " << name << std::endl;
311  sample->DumpPOT();
312  }
313  std::cout << "--------------------------------------------" << std::endl;
314  std::cout << "--------------------------------------------" << std::endl;
315 
316 }
317 
318 
319 //**************************************************
321 //**************************************************
322  std::map< std::string, SampleGroup >::iterator sit;
323 
324  for (sit = _sampleGroups.begin(); sit != _sampleGroups.end(); sit++) {
325  SampleGroup& sampleGroup = sit->second;
326  Float_t POTdata, POTmc;
327  sampleGroup.GetPOT(POTdata, POTmc);
328 
329  Float_t ratio;
330 
331  if (POTmc == 0) {
332  std::cout << "Warning: MC POT is zero! Setting ratio to 1." << std::endl;
333  ratio = 1.;
334  } else if (POTdata == 0) {
335  std::cout << "Warning: data POT is zero! Setting ratio to 1." << std::endl;
336  ratio = 1.;
337  } else {
338  ratio = POTmc / POTdata;
339  }
340 
341  std::cout << sit->first << ": \t" << ratio << " \t(" << POTmc << " / " << POTdata << " )" << std::endl;
342  }
343 }
344 
345 //**************************************************
347 //**************************************************
348  Float_t totalData = 0;
349  Float_t totalMC = 0;
350 
351  std::map< std::string, SampleGroup >::iterator sit;
352 
353  for (sit = _sampleGroups.begin(); sit != _sampleGroups.end(); sit++) {
354  SampleGroup& sampleGroup = sit->second;
355  Float_t POTdata, POTmc;
356  sampleGroup.GetPOT(POTdata, POTmc);
357  totalData += POTdata;
358  totalMC += POTmc;
359  }
360 
361  return totalMC / totalData;
362 }
363 
364 
365 //**************************************************
367 //**************************************************
368 
369  std::map< std::string, SampleGroup >::iterator sit;
370  for (sit = _sampleGroups.begin(); sit != _sampleGroups.end(); sit++) {
371  SampleGroup& sampleGroup = sit->second;
372 
373  std::vector<DataSample*>& dataSamples = sampleGroup.GetDataSamples();
374  std::vector<DataSample*>::iterator it;
375  for (it = dataSamples.begin(); it != dataSamples.end(); it++) {
376  DataSample* sample = *it;
377  sample->DumpPOT();
378  }
379 
380  std::map< std::string, DataSample*>& mcSamples = sampleGroup.GetMCSamples();
381  std::map< std::string, DataSample*>::iterator it2;
382  for (it2 = mcSamples.begin(); it2 != mcSamples.end(); it2++) {
383  std::string name = it2->first;
384  DataSample* sample = it2->second;
385  sample->DumpPOT();
386  }
387  }
388 
389 
390 }
bool HasMCSample(const std::string &name)
Check whether the group has a given MC sample.
Definition: Experiment.cxx:87
void DumpPOTRatios()
Dump total good POT for all sample pairs, and the POT ratio for each pair.
Definition: Experiment.cxx:320
void SetCurrentTree(const std::string &name)
Set the current configuration to all samples in the SampleGroup.
Definition: Experiment.cxx:132
std::vector< DataSample * > & GetDataSamples()
Get all MC samples in a group.
Definition: Experiment.hxx:47
Experiment(const std::string &name)
Definition: Experiment.cxx:149
void GetPOT(Float_t &POTdata, Float_t &POTmc)
Get the good data and MC POT for this sample group.
Definition: Experiment.cxx:98
std::map< std::string, DataSample * > & GetMCSamples()
Get all MC samples in a group.
Definition: Experiment.hxx:50
void Dump()
Dump info about the experiment.
Definition: Experiment.cxx:366
Double_t GetPOT()
This is the method used externaly. It corresponds to POT that passed beam and ND280 quality cuts...
Definition: Header.hxx:36
void SetCurrentTree(Int_t index)
Sets the current tree provided the index.
Definition: TreeManager.hxx:58
void DumpPOT(const std::string &name)
Dump detailed POT information for a given sample pair.
Definition: Experiment.cxx:287
void AddMCSample(const std::string &name, const std::string &file)
Add a new MC Sample to a group specifying its name (spill, sand, etc)
Definition: Experiment.cxx:48
void SetCurrentTree(const std::string &name)
Set the current configuration to all samples in all SampleGroups.
Definition: Experiment.cxx:275
bool HasSampleGroup(const std::string &name)
Checks if the Experiment has a SampleGroup with a given name.
Definition: Experiment.cxx:222
void AddSampleFilesToVector(SampleGroup &sampleGroup)
Method to add file paths to the MC and Data vectors that store them.
Definition: Experiment.cxx:233
void AddMCSamples(std::map< std::string, std::string > &mcFiles)
Add a set of MC Samples to a group specifying their names (spill, sand, etc)
Definition: Experiment.cxx:63
DataSample * GetDataSample()
Get the data sample in a group.
Definition: Experiment.hxx:38
void AddSampleGroup(const std::string &name, SampleGroup &sampleGroup)
Definition: Experiment.cxx:248
void AddDataSample(const std::string &file)
Add a new Data Sample to a group (for the moment only one data sample can be added) ...
Definition: Experiment.cxx:34
void DumpPOT()
Print the POT information.
Definition: Header.cxx:226
Float_t GetOverallPOTRatio()
Get the overall POT ratio.
Definition: Experiment.cxx:346