CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CalibrationScanTask.cc
Go to the documentation of this file.
10 
11 #include <arpa/inet.h>
12 #include <sys/unistd.h>
13 #include <sys/socket.h>
14 #include <netdb.h>
15 #include <stdio.h>
16 #include <fstream>
17 
18 // -----------------------------------------------------------------------------
19 //
22  const sistrip::RunType& rtype,
23  const char* filename,
24  uint32_t run,
25  const edm::EventSetup& setup ) :
26  CommissioningTask( dqm, conn, "CalibrationScanTask" ),
27  runType_(rtype),
28  calib1_(),calib2_(),
29  nBins_(65),lastISHA_(1000),lastVFS_(1000),lastCalchan_(1000),
30  filename_(filename),
31  run_(run)
32 {
33  LogDebug("Commissioning") << "[CalibrationScanTask::CalibrationScanTask] Constructing object...";
34  // load the pedestals
35  edm::ESHandle<SiStripPedestals> pedestalsHandle;
36  setup.get<SiStripPedestalsRcd>().get(pedestalsHandle);
37  SiStripPedestals::Range detPedRange = pedestalsHandle->getRange(conn.detId());
38  int start = conn.apvPairNumber()*256;
39  int stop = start + 256;
40  int value = 0;
41  ped.reserve(256);
42  LogDebug("Commissioning") << "[CalibrationScanTask::CalibrationScanTask] Loading pedestal for " << conn.detId();
43  if(conn.detId()==0) return;
44  for(int strip = start; strip < stop; ++strip) {
45  value = int(pedestalsHandle->getPed(strip,detPedRange));
46  if(value>895) value -= 1024;
47  ped.push_back(value);
48  }
49 }
50 
51 // -----------------------------------------------------------------------------
52 //
54  LogDebug("Commissioning") << "[CalibrationScanTask::CalibrationScanTask] Destructing object...";
55 }
56 
57 // -----------------------------------------------------------------------------
58 //
60  LogDebug("Commissioning") << "[CalibrationScanTask::book]";
61 
62  // construct the histo titles and book two histograms: one per APV
64  runType_,
66  connection().detId(),
67  sistrip::APV,
68  connection().i2cAddr(0) ).title();
69  calib1_.histo( dqm()->book1D( title1, title1, nBins_, 0, 203.125) );
70  calib1_.isProfile_=false;
71  calib1_.vNumOfEntries_.resize(nBins_,0);
73  runType_,
75  connection().detId(),
76  sistrip::APV,
77  connection().i2cAddr(1) ).title();
78  calib2_.histo( dqm()->book1D( title2, title2, nBins_, 0, 203.125) );
79  calib2_.isProfile_=false;
80  calib2_.vNumOfEntries_.resize(nBins_,0);
81 
82  // book the isha, vfs values
83  std::string pwd = dqm()->pwd();
84  std::string rootDir = pwd.substr(0,pwd.find(std::string(sistrip::root_) + "/")+(sizeof(sistrip::root_) - 1));
85  dqm()->setCurrentFolder( rootDir );
86  std::vector<std::string> existingMEs = dqm()->getMEs();
87  if(find(existingMEs.begin(),existingMEs.end(),"isha")!=existingMEs.end()) {
88  ishaElement_ = dqm()->get(dqm()->pwd()+"/isha");
89  } else {
90  ishaElement_ = dqm()->bookInt("isha");
91  }
92  if(find(existingMEs.begin(),existingMEs.end(),"isha")!=existingMEs.end()) {
93  vfsElement_ = dqm()->get(dqm()->pwd()+"/vfs");
94  } else {
95  vfsElement_ = dqm()->bookInt("vfs");
96  }
97  if(find(existingMEs.begin(),existingMEs.end(),"calchan")!=existingMEs.end()) {
98  calchanElement_ = dqm()->get(dqm()->pwd()+"/calchan");
99  } else {
100  calchanElement_ = dqm()->bookInt("calchan");
101  }
102 
103  LogDebug("Commissioning") << "[CalibrationScanTask::book] done";
104 
105 }
106 
107 // -----------------------------------------------------------------------------
108 //
110  const edm::DetSet<SiStripRawDigi>& digis ) {
111 // LogDebug("Commissioning") << "[CalibrationScanTask::fill]: isha/vfs = " << summary.isha() << "/" << summary.vfs();
112  // Check if ISHA/VFS changed. In that case, save, reset histo, change title, and continue
113  checkAndSave(summary.isha(),summary.vfs());
114  // retrieve the delay from the EventSummary
115  int bin = (100-summary.latency())*8+(7-summary.calSel());
116  // loop on the strips to fill the histogram
117  // digis are obtained for an APV pair.
118  // strips 0->127 : calib1_
119  // strips 128->255: calib2_
120  // then, only some strips are fired at a time.
121  // We use calChan to know that
122  int isub,ical = summary.calChan();
123  isub = ical<4 ? ical+4 : ical-4;
124  lastCalchan_ = ical;
125  for (int k=0;k<16;++k) {
126  // all strips of the APV are merged in
127  updateHistoSet( calib1_,bin,digis.data[ical+k*8].adc()-ped[ical+k*8]-(digis.data[isub+k*8].adc()-ped[isub+k*8]));
128  updateHistoSet( calib2_,bin,digis.data[128+ical+k*8].adc()-ped[128+ical+k*8]-(digis.data[128+isub+k*8].adc()-ped[128+isub+k*8]));
129  }
130  update(); //TODO: temporary: find a better solution later
131 }
132 
133 // -----------------------------------------------------------------------------
134 //
136  // LogDebug("Commissioning") << "[CalibrationScanTask::update]"; // huge output
139 }
140 
141 // -----------------------------------------------------------------------------
142 //
143 void CalibrationScanTask::checkAndSave(const uint16_t& isha, const uint16_t& vfs ) {
144  //for the first time
145  if(lastISHA_==1000 && lastVFS_==1000) {
146  lastISHA_ = isha;
147  lastVFS_ = vfs;
148  }
149 
150  // set the calchan value in the correspond string
154 
155  // check if ISHA/VFS has changed
156  // in that case, save the histograms, reset them, change the title and continue
157  if(lastISHA_!=isha || lastVFS_!=vfs) {
158 
159  // set histograms before saving
160  update(); //TODO: we must do this for all tasks, otherwise only the first is updated.
161 
162  // change the title
163  std::stringstream complement;
164  complement << "ISHA" << isha << "_VFS" << vfs;
166  runType_,
168  connection().detId(),
169  sistrip::APV,
170  connection().i2cAddr(0),
171  complement.str() ).title();
172  calib1_.histo()->setTitle(title1);
173 
175  runType_,
177  connection().detId(),
178  sistrip::APV,
179  connection().i2cAddr(1),
180  complement.str() ).title();
181  calib2_.histo()->setTitle(title2);
182 
183  // Strip filename of ".root" extension
185  if ( filename_.find(".root",0) == std::string::npos ) { name = filename_; }
186  else { name = filename_.substr( 0, filename_.find(".root",0) ); }
187 
188  // Retrieve SCRATCH directory
189  std::string scratch = "SCRATCH"; //@@ remove trailing slash!!!
190  std::string dir = "";
191  if ( getenv(scratch.c_str()) != NULL ) {
192  dir = getenv(scratch.c_str());
193  }
194 
195  // Save file with appropriate filename
196  std::stringstream ss;
197  if ( !dir.empty() ) { ss << dir << "/"; }
198  else { ss << "/tmp/"; }
199  ss << name << "_";
200  directory(ss,run_); // Add filename with run number, host ip, pid and .root extension
201  ss << "_ISHA" << lastISHA_ << "_VFS" << lastVFS_; // Add ISHA and VFS values
202  ss << "_000"; // Add FU instance number (fake)
203  ss << ".root"; // Append ".root" extension
204  if ( !filename_.empty() ) {
205  if(std::ifstream(ss.str().c_str(),std::ifstream::in).fail()) { //save only once. Skip if the file already exist
206  dqm()->save( ss.str() );
207  LogTrace("DQMsource")
208  << "[SiStripCommissioningSource::" << __func__ << "]"
209  << " Saved all histograms to file \""
210  << ss.str() << "\"";
211  } else {
212  LogTrace("DQMsource")
213  << "[SiStripCommissioningSource::" << __func__ << "]"
214  << " Skipping creation of file \""
215  << ss.str() << "\" that already exists" ;
216  }
217  } else {
218  edm::LogWarning("DQMsource")
219  << "[SiStripCommissioningSource::" << __func__ << "]"
220  << " NULL value for filename! No root file saved!";
221  }
222 
223  // reset
224  calib1_.vNumOfEntries_.clear();
225  calib1_.vNumOfEntries_.resize(nBins_,0);
226  calib2_.vNumOfEntries_.clear();
227  calib2_.vNumOfEntries_.resize(nBins_,0);
228 
229  // set new parameter values
230  lastISHA_=isha;
231  lastVFS_=vfs;
232  // set the calchan value in the correspond string
234  vfsElement_->Fill(lastVFS_);
236  }
237 }
238 
239 // -----------------------------------------------------------------------------
240 //
241 void CalibrationScanTask::directory( std::stringstream& dir,
242  uint32_t run_number ) {
243 
244  // Get details about host
245  char hn[256];
246  gethostname( hn, sizeof(hn) );
247  struct hostent* he;
248  he = gethostbyname(hn);
249 
250  // Extract host name and ip
251  std::string host_name;
252  std::string host_ip;
253  if ( he ) {
254  host_name = std::string(he->h_name);
255  host_ip = std::string( inet_ntoa( *(struct in_addr*)(he->h_addr) ) );
256  } else {
257  host_name = "unknown.cern.ch";
258  host_ip = "255.255.255.255";
259  }
260 
261  // Reformat IP address
262  std::string::size_type pos = 0;
263  std::stringstream ip;
264  //for ( uint16_t ii = 0; ii < 4; ++ii ) {
265  while ( pos != std::string::npos ) {
266  std::string::size_type tmp = host_ip.find(".",pos);
267  if ( tmp != std::string::npos ) {
268  ip << std::setw(3)
269  << std::setfill('0')
270  << host_ip.substr( pos, tmp-pos )
271  << ".";
272  pos = tmp+1; // skip the delimiter "."
273  } else {
274  ip << std::setw(3)
275  << std::setfill('0')
276  << host_ip.substr( pos );
277  pos = std::string::npos;
278  }
279  }
280 
281  // Get pid
282  pid_t pid = getpid();
283 
284  // Construct string
285  dir << std::setw(8)
286  << std::setfill('0')
287  << run_number
288  << "_"
289  << ip.str()
290  << "_"
291  << std::setw(5)
292  << std::setfill('0')
293  << pid;
294 
295 }
#define LogDebug(id)
std::vector< float > vNumOfEntries_
const uint32_t & vfs() const
tuple start
Check for commandline option errors.
Definition: dqm_diff.py:58
Utility class that holds histogram title.
const std::string & title() const
const uint32_t & latency() const
sistrip::RunType runType_
uint16_t apvPairNumber() const
MonitorElement * vfsElement_
#define NULL
Definition: scimark2.h:8
std::pair< ContainerIterator, ContainerIterator > Range
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
uint16_t size_type
std::vector< uint16_t > ped
void Fill(long long x)
void directory(std::stringstream &, uint32_t run_number=0)
const uint32_t & isha() const
void updateHistoSet(HistoSet &, const uint32_t &bin, const float &value)
const uint32_t & detId() const
Class containning control, module, detector and connection information, at the level of a FED channel...
MonitorElement * get(const std::string &path) const
get ME from full pathname (e.g. &quot;my/long/dir/my_histo&quot;)
Definition: DQMStore.cc:1760
#define LogTrace(id)
void checkAndSave(const uint16_t &isha, const uint16_t &vfs)
const uint32_t & calSel() const
MonitorElement * ishaElement_
DQMStore *const dqm() const
const T & get() const
Definition: EventSetup.h:55
tuple pid
Definition: sysUtil.py:22
virtual void fill(const SiStripEventSummary &, const edm::DetSet< SiStripRawDigi > &)
std::vector< std::string > getMEs(void) const
get list of (non-dir) MEs of current directory
Definition: DQMStore.cc:1734
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
collection_type data
Definition: DetSet.h:78
MonitorElement * calchanElement_
void histo(MonitorElement *)
void save(const std::string &filename, const std::string &path="", const std::string &pattern="", const std::string &rewrite="", const uint32_t run=0, const uint32_t lumi=0, SaveReferenceTag ref=SaveWithReference, int minStatus=dqm::qstatus::STATUS_OK, const std::string &fileupdate="RECREATE", const bool resetMEsAfterWriting=false)
Definition: DQMStore.cc:2608
tuple filename
Definition: lut2db_cfg.py:20
static const char root_[]
const uint32_t & calChan() const
const FedChannelConnection & connection() const
dbl *** dir
Definition: mlp_gen.cc:35
MonitorElement * bookInt(const char *name)
Book int.
Definition: DQMStore.cc:913
CalibrationScanTask(DQMStore *, const FedChannelConnection &, const sistrip::RunType &, const char *filename, uint32_t run, const edm::EventSetup &setup)
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
tuple conn
Definition: results_mgr.py:53
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:719
const std::string & pwd(void) const
Definition: DQMStore.cc:691