Diana Software
LHDRGlobalReader.cc
Go to the documentation of this file.
1 #include "LHDRGlobalReader.hh"
2 #include "QRunData.hh"
3 #include "QTime.hh"
4 #include <sstream>
5 #include <cmath>
6 
8 
9 using namespace Diana;
10 using std::stringstream;
11 using std::string;
12 using std::iostream;
13 using std::ios;
14 using std::vector;
15 
17 {
18  fQObjectCache = new std::map<std::string, QObject*>;
19 }
20 
22 {
23  Close();
24  delete fQObjectCache;
25  fQObjectCache = 0;
26 }
27 
28 QError LHDRGlobalReader::Open(const std::string& filename, const std::string& opt)
29 {
31  fFile.open(filename.c_str(),ios::in);
32  if(!fFile)
33  {
35  err.SetDescription(filename);
36  fFile.close();
37  } else {
38  fName = filename;
39  Parse();
40  }
41  return err;
42 }
43 
44 
46 {
48  fFile.close();
49  fName = "";
50  if(!fQObjectCache) return err;
51 
52  std::map<std::string, QObject*>::iterator iter = fQObjectCache->begin();
53  while(iter != fQObjectCache->end()) {
54  if(iter->second) delete iter->second;
55  iter++;
56  }
57  fQObjectCache->clear();
58  return err;
59 }
60 
61 const QObject* LHDRGlobalReader::GetQObject(const std::string& name,const std::string& className, QError& err) const
62 {
63 
64  err = QERR_SUCCESS;
65  if(className == "QRunData") {
66  std::map<std::string, QObject*>::const_iterator object = fQObjectCache->find(name);
67  if(object != fQObjectCache->end()) {
68  return object->second;
69  } else {
71  err.SetDescription(GetName(),__LINE__,std::string("Could not find object \"")+name+"\"");
72  }
73 
74  } else {
76  err.SetDescription(GetName(),__LINE__,std::string("Invalid type \"")+className+"\"");
77  }
78  return 0;
79 }
80 
82 {
83  // basics
84  std::string suffix = fName.substr(fName.size()-9 ,5);
85  DetectorName detector;
86  detector = DN_UNKNOWN;
87  if(suffix.at(0) == 'c') {
88  detector = DN_HALLC;
89  }
90  else if(suffix.at(0) == 'q') {
91  detector = DN_CUORICINO;
92  }
93  string sRunNumber = fName;
94  sRunNumber = sRunNumber.substr(sRunNumber.size()-8,4);
95  int runNumber = atoi(sRunNumber.c_str())+(int)detector*100000;
96 
97 
98  float minADC,maxADC;
99  int numSamples,pulseDurationMs;
100  fFile>>minADC>>maxADC;
101  fFile>>numSamples>>pulseDurationMs;
102  float samplingFrequency = float(numSamples) / pulseDurationMs * 1000;
103 
104  std::string line;
105  getline(fFile,line);
106  // channels
107  vector<int> channels;
108  while(1) {
109  getline(fFile,line);
110  if(line.size() > 0) line = line.substr(0,line.size()-1);
111  bool blank = false;
112  if(line.empty() || line[0] ==' ' || line[0] =='\n' || line[0] == '\0') blank = true;
113  if(blank) break;
114  else {
115  std::stringstream str(line);
116  int chan, active;
117  str>>chan>>active;
118  if(active) channels.push_back(chan);
119  }
120  }
121  // start time, duration;
122  std::string date;
123  float duration;
124  while(1) {
125  getline(fFile,line);
126  std::stringstream str(line);
127  std::string word1,word2,word3;
128  str>>word1;
129  if(word1 == "Start") {
130  str>>word2;
131  str>>word3;
132  if(word2 == "date") {
133  date += word3 + " ";
134  }
135  else if(word2 == "time") {
136  date += word3;
137  }
138  }
139  if(word1 == "Timer") {
140  str>>duration;
141  }
142  if(!fFile.good()) break;
143  }
144 
145  QTime time;
146  int month=atoi(date.substr(0,2).c_str());
147  int day=atoi(date.substr(3,2).c_str());
148  int year=atoi(date.substr(6,4).c_str());
149  int hour=atoi(date.substr(11,2).c_str());
150  int min=atoi(date.substr(14,2).c_str());
151  int sec=atoi(date.substr(17,2).c_str());
152  time.SetStartRunUnix(year,month,day,hour,min,sec);
153  int ADCScale = (int)pow(2,16);
154 
155  QRunData* runData = new QRunData;
156  runData->fNumber = runNumber;
157  runData->fDetector = detector;
158  runData->fType = RT_UNKNOWN;
159  runData->fStartDate = time.GetStartRunUnix();
160  runData->fStopDate = runData->fStartDate+(int)duration;
161  runData->fDuration = (int)duration;
162  runData->fThermalDetectorChannels = channels;
163  runData->fBolometerChannels = channels;
164 
165  for(size_t i = 0; i < channels.size(); i++) {
166  QChannelRunData cr;
167  cr.fADC2mV = (maxADC-minADC)*1000./ADCScale;
168  cr.fSamplingFrequency = samplingFrequency;
169  cr.fADCMax = (int)ceil(maxADC*1000./cr.fADC2mV);
170  cr.fADCMin = (int)floor(minADC*1000./cr.fADC2mV);
171  cr.fNumberOfSamples = (int)(pulseDurationMs*samplingFrequency)/1000;
172  cr.fIsBolometer = true;
173  cr.fIsThermometer = false;
174 
175  runData->SetChannelRunData(channels[i],cr);
176  }
177 
178  std::string name="DAQ@RunData_run";
179  char runss[128];
180  sprintf(runss,"%06d",runNumber);
181  name += runss;
182  runData->Validate();
183  (*fQObjectCache)[name] = runData;
184 }
err
Definition: CheckOF.C:114
global reader for hdr files (headers of runs acquired with old Milano DAQ)
Diana::QComplex pow(const Diana::QComplex &z, double a)
Raise a complex number to a real power.
Definition: QComplex.cc:246
@ DN_UNKNOWN
@ DN_CUORICINO
@ DN_HALLC
@ QERR_UNKNOWN_ERR
Definition: QError.hh:108
@ QERR_CANNOT_OPEN_FILE
Definition: QError.hh:33
@ QERR_SUCCESS
Definition: QError.hh:27
#define REGISTER_GLOBAL_READER(clazz, ext)
@ RT_UNKNOWN
Definition: QRunType.hh:14
double min(const Diana::QVector &v)
Definition: QVector.cc:878
class wrapper to DetectorName_
QError Close()
Close file, called by QGlobalReaderDispatcher.
const Diana::QObject * GetQObject(const std::string &name, const std::string &className, QError &err) const
std::map< std::string, Diana::QObject * > * fQObjectCache
QError Open(const std::string &filename, const std::string &opt="")
Open file, called by QGlobalReaderDispatcher.
std::ifstream fFile
basic channel and run based info. Used in the QRunData object.
double fSamplingFrequency
sampling frequency in Hz
int fADCMin
ADC minimum in ADC units.
bool fIsThermometer
is thermometer
int fNumberOfSamples
number of samples in ADC window
int fADCMax
ADC maximum in ADC units.
double fADC2mV
conversion: mV = ADC * fADC2mV
bool fIsBolometer
is bolometer
error class with error type and description
Definition: QError.hh:115
Abstract class for global readers.
const std::string & GetName() const
Definition: QNamed.hh:19
Basic run based info.
Definition: QRunData.hh:20
int fNumber
Run Number.
Definition: QRunData.hh:41
time_t fStartDate
StartTime.
Definition: QRunData.hh:62
std::vector< int > fThermalDetectorChannels
Thermal detector channels: union of fBolometerChannels and fThermometerChannels.
Definition: QRunData.hh:47
RunType fType
Run Type.
Definition: QRunData.hh:45
time_t fStopDate
StopTime.
Definition: QRunData.hh:64
time_t fDuration
Duration.
Definition: QRunData.hh:66
std::vector< int > fBolometerChannels
Bolometers channels (thermistors glued to a crystal)
Definition: QRunData.hh:49
DetectorName fDetector
Source Run Number.
Definition: QRunData.hh:43
void SetChannelRunData(const int channel, const QChannelRunData &chanRunData)
set channel based run data quantities
Definition: QRunData.cc:352
Diana time.
Definition: QTime.hh:17
void SetStartRunUnix(unsigned int time)
set time of start of run in seconds from 1/1/1970
Definition: QTime.hh:32
time_t GetStartRunUnix() const
retrieve time of start of run in seconds from 1/1/1970
Definition: QTime.hh:41
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...