QwAnalysis
QwMockDataAnalysis.cc
Go to the documentation of this file.
1 /*------------------------------------------------------------------------*//*!
2 
3  \file QwMockDataAnalysis.cc
4 
5  \brief Parity mock data analysis, test code
6 
7 *//*-------------------------------------------------------------------------*/
8 
9 // C and C++ headers
10 #include <iostream>
11 
12 // Boost math library for random number generation
13 #include <boost/random.hpp>
14 
15 // ROOT headers
16 #include <TROOT.h>
17 #include <TFile.h>
18 #include <TTree.h>
19 
20 
21 // Qweak headers
22 #include "QwBeamLine.h"
23 #include "QwOptionsParity.h"
24 #include "QwEventBuffer.h"
25 #include "QwParityDB.h"
26 #include "QwHelicity.h"
27 #include "QwHelicityPattern.h"
28 #include "QwHistogramHelper.h"
29 #include "QwMainCerenkovDetector.h"
30 #include "QwLumi.h"
31 #include "QwSubsystemArrayParity.h"
32 
33 
34 // Number of events to generate
35 #define NEVENTS 1000
36 // Number of variables to correlate
37 #define NVARS 4
38 
39 
40 // Multiplet structure
41 static const int kMultiplet = 4;
42 
43 // Debug level
44 static bool bDebug = false;
45 
46 // Activate components
47 static bool bTree = true;
48 static bool bHisto = true;
49 static bool bHelicity = true;
50 static bool bDatabase = false;
51 
52 int main(int argc, char* argv[])
53 {
54  // First, we set the command line arguments and the configuration filename,
55  // and we define the options that can be used in them (using QwOptions).
56  gQwOptions.SetCommandLine(argc, argv);
57  // gQwOptions.SetConfigFile("qwmockdataanalysis.conf");
58  gQwOptions.SetConfigFile("Parity/prminput/qwanalysis_beamline.conf");
59  gQwOptions.SetConfigFile("Parity/prminput/qweak_mysql.conf");
60  // Define the command line options
62 
63  /// Fill the search paths for the parameter files; this sets a static
64  /// variable within the QwParameterFile class which will be used by
65  /// all instances.
66  /// The "scratch" directory should be first.
68  QwParameterFile::AppendToSearchPath(getenv_safe_string("QWANALYSIS")+"/Parity/prminput");
69  QwParameterFile::AppendToSearchPath(getenv_safe_string("QWANALYSIS") + "/Analysis/prminput");
70 
71  // Load histogram definitions
72  gQwHists.LoadHistParamsFromFile("qweak_parity_hists.in");
73 
74  // Detector array
76  detectors.ProcessOptions(gQwOptions);
77 
78  // Helicity pattern
79  QwHelicityPattern helicitypattern(detectors);
80  helicitypattern.ProcessOptions(gQwOptions);
81 
82  // Running sum
83  QwSubsystemArrayParity runningsum(detectors);
84 
85  // Get the helicity
86  QwHelicity* helicity = dynamic_cast<QwHelicity*>(detectors.GetSubsystemByName("Helicity info"));
87 
88  // Event buffer
89  QwEventBuffer eventbuffer;
90 
91 
92  // Loop over all runs
93  UInt_t runnumber_min = (UInt_t) gQwOptions.GetIntValuePairFirst("run");
94  UInt_t runnumber_max = (UInt_t) gQwOptions.GetIntValuePairLast("run");
95  for (UInt_t run = runnumber_min;
96  run <= runnumber_max;
97  run++) {
98 
99  // Data file (input)
100  TString datafilename = TString("QwMock_") + Form("%d.log",run);
101  if (eventbuffer.OpenDataFile(datafilename,"R") != CODA_OK) {
102  std::cout << "Error: could not open file!" << std::endl;
103  return 0;
104  }
105 
106 
107  // ROOT file output (histograms)
108  TString rootfilename = getenv_safe_TString("QW_ROOTFILES") + TString("/QwMock_") + Form("%d.root",run);
109  TFile rootfile(rootfilename, "RECREATE", "QWeak ROOT file");
110  if (bHisto) {
111  rootfile.cd();
112  detectors.ConstructHistograms(rootfile.mkdir("mps_histo"));
113  if (bHelicity) {
114  rootfile.cd();
115  helicitypattern.ConstructHistograms(rootfile.mkdir("hel_histo"));
116  }
117  }
118 
119  // ROOT file output (trees)
120  TTree *mpstree;
121  TTree *heltree;
122  Int_t eventnumber;
123  std::vector <Double_t> mpsvector;
124  std::vector <Double_t> helvector;
125  if (bTree) {
126  rootfile.cd();
127  mpstree = new TTree("MPS_Tree","MPS event data tree");
128  mpsvector.reserve(6000);
129  mpstree->Branch("eventnumber",&eventnumber,"eventnumber/F");
130  TString dummystr="";
131  detectors.ConstructBranchAndVector(mpstree, dummystr, mpsvector);
132  rootfile.cd();
133  if (bHelicity) {
134  rootfile.cd();
135  heltree = new TTree("HEL_Tree","Helicity event data tree");
136  helvector.reserve(6000);
137  TString dummystr="";
138  helicitypattern.ConstructBranchAndVector(heltree, dummystr, helvector);
139  }
140  }
141 
142 
143  // Loop over events in this CODA file
144  Int_t eventnumber_min = gQwOptions.GetIntValuePairFirst("event");
145  Int_t eventnumber_max = gQwOptions.GetIntValuePairLast("event");
146  while (eventbuffer.GetEvent() == CODA_OK) {
147 
148  // First, do processing of non-physics events...
149  if (eventbuffer.IsROCConfigurationEvent()) {
150  // Send ROC configuration event data to the subsystem objects.
151  eventbuffer.FillSubsystemConfigurationData(detectors);
152  }
153 
154  // Now, if this is not a physics event, go back and get a new event.
155  if (! eventbuffer.IsPhysicsEvent()) continue;
156 
157  // Check to see if we want to process this event.
158  eventnumber = eventbuffer.GetEventNumber();
159  if (eventnumber < eventnumber_min) continue;
160  else if (eventnumber > eventnumber_max) break;
161 
162  // Fill the subsystem objects with their respective data for this event.
163  eventbuffer.FillSubsystemData(detectors);
164 
165  // Process this events
166  detectors.ProcessEvent();
167  detectors.IncrementErrorCounters();
168 
169  // Helicity pattern
170  if (bHelicity)
171  helicitypattern.LoadEventData(detectors);
172 
173  // Print the helicity information
174  if (bHelicity && bDebug) {
175  // - actual helicity
176  if (helicity->GetHelicityActual() == 0) std::cout << "-";
177  else if (helicity->GetHelicityActual() == 1) std::cout << "+";
178  else std::cout << "?";
179  // - delayed helicity
180  if (helicity->GetHelicityDelayed() == 0) std::cout << "(-) ";
181  else if (helicity->GetHelicityDelayed() == 1) std::cout << "(+) ";
182  else std::cout << "(?) ";
183  if (helicity->GetPhaseNumber() == kMultiplet) {
184  std::cout << std::hex << helicity->GetRandomSeedActual() << std::dec << ", \t";
185  std::cout << std::hex << helicity->GetRandomSeedDelayed() << std::dec << std::endl;
186  }
187  }
188 
189  // Check for helicity validity (TODO I'd prefer to use kUndefinedHelicity)
190  if (bHelicity && helicity->GetHelicityDelayed() == -9999) continue;
191 
192  // Fill the histograms
193  if (bHisto) detectors.FillHistograms();
194 
195  // Accumulate the running sum to calculate the event based running average
196  runningsum.AccumulateRunningSum(detectors);
197 
198  // Fill the MPS tree
199  if (bTree) {
200  eventnumber = eventbuffer.GetEventNumber();
201  detectors.FillTreeVector(mpsvector);
202  mpstree->Fill();
203  }
204  // Fill the helicity tree
205  if (bHelicity && helicitypattern.IsCompletePattern()) {
206  helicitypattern.CalculateAsymmetry();
207  if (bHisto) helicitypattern.FillHistograms();
208  if (bTree) {
209  helicitypattern.FillTreeVector(helvector);
210  heltree->Fill();
211  }
212  helicitypattern.ClearEventData();
213  }
214 
215  // Periodically print event number
216  if ((bDebug && eventbuffer.GetEventNumber() % 1000 == 0)
217  || eventbuffer.GetEventNumber() % 10000 == 0)
218  std::cout << "Number of events processed so far: "
219  << eventbuffer.GetEventNumber() << std::endl;
220 
221  // TODO (wdc) QwEventBuffer should have Bool_t AtEndOfBurst()
222  //if (QwEvt.AtEndOfBurst()){
223  if (eventbuffer.GetEventNumber() % 1000 == 0) {
224  {
225  helicitypattern.AccumulateRunningBurstSum();
226  helicitypattern.CalculateBurstAverage();
227  helicitypattern.ClearBurstSum();
228  }
229  }
230 
231  } // end of loop over events
232 
233  // Calculate the running averages
234  helicitypattern.CalculateRunningAverage();
235  helicitypattern.CalculateRunningBurstAverage();
236  runningsum.CalculateRunningAverage();
237 
238  // Close ROOT file
239  rootfile.Write(0,TObject::kOverwrite);
240 
241  // Close data file and print run summary
242  eventbuffer.CloseDataFile();
243  eventbuffer.ReportRunSummary();
244 
245  // Write to database
246  if (bDatabase) {
247  QwParityDB* qweak_database = new QwParityDB();
248  QwMessage << "GetMonitorID(qwk_batext2) = " << qweak_database->GetMonitorID("qwk_batext2") << QwLog::endl;
249  QwMessage << "GetMonitorID(phasemonitor) = " << qweak_database->GetMonitorID("phasemonitor") << QwLog::endl;
250  QwMessage << "GetMonitorID(qwk_junk) = " << qweak_database->GetMonitorID("qwk_junk") << QwLog::endl;
251  QwMessage << "GetMainDetectorID(md1neg) = " << qweak_database->GetMainDetectorID("md1neg") << QwLog::endl;
252  QwMessage << "GetMainDetectorID(spare3) = " << qweak_database->GetMainDetectorID("spare3") << QwLog::endl;
253  QwMessage << "GetMainDetectorID(combinationallmd) = " << qweak_database->GetMainDetectorID("combinationallmd") << QwLog::endl;
254  QwMessage << "GetLumiDetectorID(dlumi8) = " << qweak_database->GetLumiDetectorID("dlumi8") << QwLog::endl;
255  QwMessage << "GetVersion() = " << qweak_database->GetVersion() << QwLog::endl;
256  // GetRunID() and GetAnalysisID have their own Connect() and Disconnect() functions.
257  UInt_t run_id = qweak_database->GetRunID(eventbuffer);
258  UInt_t analysis_id = qweak_database->GetAnalysisID(eventbuffer);
259 
260  QwMessage << "QwMockDataAnalysis.cc::"
261  << " Run Number " << QwColor(Qw::kBoldMagenta) << eventbuffer.GetRunNumber() << QwColor(Qw::kNormal)
262  << " Run ID " << QwColor(Qw::kBoldMagenta) << run_id<< QwColor(Qw::kNormal)
263  << " Analysis ID " << QwColor(Qw::kBoldMagenta) << analysis_id
264  << QwLog::endl;
265 
266  // Each sussystem has its own Connect() and Disconnect() functions.
267  helicitypattern.FillDB(qweak_database);
268 
269  delete qweak_database; qweak_database = NULL;
270 
271  } // end of database write
272 
273 
274  } // end of loop over runs
275 
276 
277 }
int GetIntValuePairFirst(const std::string &key)
Get the first of a pair of integer values.
Definition: QwOptions.h:266
#define QwMessage
Predefined log drain for regular messages.
Definition: QwLog.h:50
void FillHistograms()
Fill the histograms for this subsystem.
UInt_t GetMonitorID(const string &name, Bool_t zero_id_is_error=kTRUE)
Definition: QwParityDB.cc:618
Int_t GetPhaseNumber()
Definition: QwHelicity.cc:1084
static void AppendToSearchPath(const TString &searchdir)
Add a directory to the search path.
Bool_t FillSubsystemConfigurationData(QwSubsystemArray &subsystems)
void ConstructHistograms()
Construct the histograms for this subsystem.
static bool bHisto
UInt_t GetMainDetectorID(const string &name, Bool_t zero_id_is_error=kTRUE)
Definition: QwParityDB.cc:661
static bool bHelicity
void DefineOptionsParity(QwOptions &options)
const TString getenv_safe_TString(const char *name)
Definition: QwOptions.h:40
void AccumulateRunningSum(const QwSubsystemArrayParity &value)
Update the running sums for devices accumulated for the global error non-zero events/patterns.
void ConstructBranchAndVector(TTree *tree, TString &prefix, std::vector< Double_t > &values)
void ConstructBranchAndVector(TTree *tree, TString &prefix, std::vector< Double_t > &values)
Construct a branch and vector for this subsystem with a prefix.
UInt_t GetLumiDetectorID(const string &name, Bool_t zero_id_is_error=kTRUE)
Definition: QwParityDB.cc:790
QwOptions gQwOptions
Definition: QwOptions.cc:27
void CalculateRunningAverage()
Calculate the average for all good events.
UInt_t GetAnalysisID()
Definition: QwParityDB.h:71
Bool_t IsCompletePattern() const
void ProcessOptions(QwOptions &options)
Process configuration options (default behavior)
const string GetVersion()
Definition: QwDatabase.cc:304
UInt_t GetRandomSeedActual()
Definition: QwHelicity.h:86
Int_t OpenDataFile(UInt_t current_run, Short_t seg)
Int_t GetEventNumber()
Int_t GetHelicityDelayed()
Definition: QwHelicity.cc:1069
Virtual base class for the parity subsystems.
UInt_t GetRunID()
Definition: QwParityDB.h:69
void SetConfigFile(const std::string &configfile)
Set a configuration file.
Definition: QwOptions.h:192
void FillTreeVector(std::vector< Double_t > &values) const
static bool bTree
Int_t GetRunNumber() const
Return CODA file run number.
Definition: QwEventBuffer.h:80
Load the options for the parity subsystems.
void ProcessOptions(QwOptions &options)
Process the configuration options.
QwHistogramHelper gQwHists
Globally defined instance of the QwHistogramHelper class.
Bool_t FillSubsystemData(QwSubsystemArray &subsystems)
A color changing class for the output stream.
Definition: QwColor.h:98
UInt_t GetRandomSeedDelayed()
Definition: QwHelicity.h:87
static std::ostream & endl(std::ostream &)
End of the line.
Definition: QwLog.cc:299
Int_t CloseDataFile()
void ProcessEvent()
Process the decoded data in this event.
const std::string getenv_safe_string(const char *name)
Definition: QwOptions.h:37
void LoadEventData(QwSubsystemArrayParity &event)
Bool_t IsPhysicsEvent()
VQwSubsystemParity * GetSubsystemByName(const TString &name)
Get the subsystem with the specified name.
void IncrementErrorCounters()
Update the data elements&#39; error counters based on their internal error flags.
static const int kMultiplet
static bool bDatabase
static bool bDebug
void FillDB(QwParityDB *db)
Int_t GetHelicityActual()
Definition: QwHelicity.cc:1064
int main(int argc, char **argv)
Definition: QwRoot.cc:20
void LoadHistParamsFromFile(const std::string &filename)
int GetIntValuePairLast(const std::string &key)
Get the last of a pair of integer values.
Definition: QwOptions.h:271
Bool_t IsROCConfigurationEvent()
void SetCommandLine(int argc, char *argv[], bool default_config_file=true)
Set the command line arguments.
Definition: QwOptions.cc:112
void FillTreeVector(std::vector< Double_t > &values) const
Fill the vector for this subsystem.