test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CalibrationTask.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, "CalibrationTask" ),
27  runType_(rtype),
28  nBins_(65),lastCalChan_(8),
29  filename_(filename),
30  run_(run)
31 {
32  LogDebug("Commissioning") << "[CalibrationTask::CalibrationTask] Constructing object...";
33  // load the pedestals
34  edm::ESHandle<SiStripPedestals> pedestalsHandle;
35  setup.get<SiStripPedestalsRcd>().get(pedestalsHandle);
36  SiStripPedestals::Range detPedRange = pedestalsHandle->getRange(conn.detId());
37  int start = conn.apvPairNumber()*256;
38  int stop = start + 256;
39  int value = 0;
40  ped.reserve(256);
41  for(int strip = start; strip < stop; ++strip) {
42  value = int(pedestalsHandle->getPed(strip,detPedRange));
43  if(value>895) value -= 1024;
44  ped.push_back(value);
45  }
46 }
47 
48 // -----------------------------------------------------------------------------
49 //
51  LogDebug("Commissioning") << "[CalibrationTask::CalibrationTask] Destructing object...";
52 }
53 
54 // -----------------------------------------------------------------------------
55 //
57  LogDebug("Commissioning") << "[CalibrationTask::book]";
58 
59  // book 32 histograms, one for each strip in a calibration group on 2 APVs
60  calib_.resize(32);
61  for(int apv=0;apv<2;++apv) {
62  for(int i=0;i<16;++i) {
64  runType_,
66  connection().detId(),
67  sistrip::APV,
68  connection().i2cAddr(apv) ).title();
69  std::stringstream complement;
70  complement << "_" << i ;
71  title += complement.str();
72  calib_[apv*16+i].histo( dqm()->book1D( title, title, nBins_, 0, 203.125) );
73  calib_[apv*16+i].isProfile_=false;
74  calib_[apv*16+i].vNumOfEntries_.resize(nBins_,0);
75  }
76  }
77 
78  // book the calchan values
79  std::string pwd = dqm()->pwd();
80  std::string rootDir = pwd.substr(0,pwd.find(std::string(sistrip::root_) + "/")+(sizeof(sistrip::root_) - 1));
81  dqm()->setCurrentFolder( rootDir );
82  std::vector<std::string> existingMEs = dqm()->getMEs();
83  if(find(existingMEs.begin(),existingMEs.end(),"calchan")!=existingMEs.end()) {
84  calchanElement_ = dqm()->get(dqm()->pwd()+"/calchan");
85  } else {
86  calchanElement_ = dqm()->bookInt("calchan");
87  }
88  dqm()->setCurrentFolder(pwd);
89 
90  LogDebug("Commissioning") << "[CalibrationTask::book] done";
91 
92 }
93 
94 // -----------------------------------------------------------------------------
95 //
97  const edm::DetSet<SiStripRawDigi>& digis ) {
98  LogDebug("Commissioning") << "[CalibrationTask::fill]";
99  // Check if CalChan changed. In that case, save, reset histo, change title, and continue
100  int isub,ical = summary.calChan();
101  isub = ical<4 ? ical+4 : ical-4;
102  checkAndSave(ical);
103  // retrieve the delay from the EventSummary
104  int bin = (100-summary.latency())*8+(7-summary.calSel());
105  // Fill the histograms.
106  // data-ped -(data-ped)_isub
107  // the second term corresponds to the common mode substraction, looking at a strip far away.
108  for (int apv=0; apv<2; ++apv) {
109  for (int k=0;k<16;++k) {
110  updateHistoSet( calib_[apv*16+k],bin,digis.data[apv*128+ical+k*8].adc()-ped[apv*128+ical+k*8]-(digis.data[apv*128+isub+k*8].adc()-ped[apv*128+isub+k*8]));
111  }
112  }
113  update(); //TODO: temporary: find a better solution later
114 }
115 
116 // -----------------------------------------------------------------------------
117 //
119  // LogDebug("Commissioning") << "[CalibrationTask::update]"; // huge output
120  for(std::vector<HistoSet>::iterator it=calib_.begin();it<calib_.end();++it) {
121  updateHistoSet( *it );
122  }
123 }
124 
125 // -----------------------------------------------------------------------------
126 //
127 void CalibrationTask::checkAndSave(const uint16_t& calchan) {
128  //for the first time
129  if(lastCalChan_==8) {
130  // set new parameter value
131  lastCalChan_ = calchan;
133  return;
134  }
135 
136  //check if CDRV register (ical) has changed
137  // in that case, save the histograms, reset them, change the title and continue
138  if(calchan!=lastCalChan_) {
139 
140  // set histograms before saving
141  update(); //TODO: we must do this for all tasks, otherwise only the first is updated.
142 
143  // change the title
144  for(int apv=0;apv<2;++apv) {
145  for(int i=0;i<16;++i) {
146  std::stringstream complement;
147  complement << "STRIP_" << (apv*128+lastCalChan_+i*8) ;
149  runType_,
151  connection().detId(),
152  sistrip::APV,
153  connection().i2cAddr(apv),
154  complement.str() ).title();
155  calib_[apv*16+i].histo()->setTitle(title);
156  calib_[apv*16+i].histo()->setAxisTitle(title);
157  }
158  }
159 
160  // Strip filename of ".root" extension
162  if ( filename_.find(".root",0) == std::string::npos ) { name = filename_; }
163  else { name = filename_.substr( 0, filename_.find(".root",0) ); }
164 
165  // Retrieve SCRATCH directory
166  std::string scratch = "SCRATCH"; //@@ remove trailing slash!!!
167  std::string dir = "";
168  if ( getenv(scratch.c_str()) != NULL ) {
169  dir = getenv(scratch.c_str());
170  }
171 
172  // Save file with appropriate filename
173  std::stringstream ss;
174  if ( !dir.empty() ) { ss << dir << "/"; }
175  else { ss << "/tmp/"; }
176  ss << name << "_";
177  directory(ss,run_); // Add filename with run number, host ip, pid and .root extension
178  ss << "_CALCHAN" << lastCalChan_; // Add CalChan value
179  ss << "_000"; // Add FU instance number (fake)
180  ss << ".root"; // Append ".root" extension
181  if ( !filename_.empty() ) {
182  if(std::ifstream(ss.str().c_str(),std::ifstream::in).fail()) { //save only once. Skip if the file already exist
183  dqm()->save( ss.str() );
184  LogTrace("DQMsource")
185  << "[SiStripCommissioningSource::" << __func__ << "]"
186  << " Saved all histograms to file \""
187  << ss.str() << "\"";
188  } else {
189  LogTrace("DQMsource")
190  << "[SiStripCommissioningSource::" << __func__ << "]"
191  << " Skipping creation of file \""
192  << ss.str() << "\" that already exists" ;
193  }
194  } else {
195  edm::LogWarning("DQMsource")
196  << "[SiStripCommissioningSource::" << __func__ << "]"
197  << " NULL value for filename! No root file saved!";
198  }
199 
200  // reset
201  for(std::vector<HistoSet>::iterator it=calib_.begin();it<calib_.end();++it) {
202  it->vNumOfEntries_.clear();
203  it->vNumOfEntries_.resize(nBins_,0);
204  }
205  // set new parameter values
206  lastCalChan_ = calchan;
207  calchanElement_->Fill(lastCalChan_);
208  }
209 }
210 
211 // -----------------------------------------------------------------------------
212 //
213 void CalibrationTask::directory( std::stringstream& dir,
214  uint32_t run_number ) {
215 
216  // Get details about host
217  char hn[256];
218  gethostname( hn, sizeof(hn) );
219  struct hostent* he;
220  he = gethostbyname(hn);
221 
222  // Extract host name and ip
223  std::string host_name;
224  std::string host_ip;
225  if ( he ) {
226  host_name = std::string(he->h_name);
227  host_ip = std::string( inet_ntoa( *(struct in_addr*)(he->h_addr) ) );
228  } else {
229  host_name = "unknown.cern.ch";
230  host_ip = "255.255.255.255";
231  }
232 
233  // Reformat IP address
234  std::string::size_type pos = 0;
235  std::stringstream ip;
236  //for ( uint16_t ii = 0; ii < 4; ++ii ) {
237  while ( pos != std::string::npos ) {
238  std::string::size_type tmp = host_ip.find(".",pos);
239  if ( tmp != std::string::npos ) {
240  ip << std::setw(3)
241  << std::setfill('0')
242  << host_ip.substr( pos, tmp-pos )
243  << ".";
244  pos = tmp+1; // skip the delimiter "."
245  } else {
246  ip << std::setw(3)
247  << std::setfill('0')
248  << host_ip.substr( pos );
249  pos = std::string::npos;
250  }
251  }
252 
253  // Get pid
254  pid_t pid = getpid();
255 
256  // Construct string
257  dir << std::setw(8)
258  << std::setfill('0')
259  << run_number
260  << "_"
261  << ip.str()
262  << "_"
263  << std::setw(5)
264  << std::setfill('0')
265  << pid;
266 }
#define LogDebug(id)
virtual void update()
int i
Definition: DBlmapReader.cc:9
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
uint16_t apvPairNumber() const
#define NULL
Definition: scimark2.h:8
std::pair< ContainerIterator, ContainerIterator > Range
CalibrationTask(DQMStore *, const FedChannelConnection &, const sistrip::RunType &, const char *filename, uint32_t run, const edm::EventSetup &setup)
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
void directory(std::stringstream &, uint32_t run_number=0)
void Fill(long long x)
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...
uint16_t lastCalChan_
sistrip::RunType runType_
virtual void fill(const SiStripEventSummary &, const edm::DetSet< SiStripRawDigi > &)
MonitorElement * get(const std::string &path) const
get ME from full pathname (e.g. &quot;my/long/dir/my_histo&quot;)
Definition: DQMStore.cc:1708
#define LogTrace(id)
std::string filename_
int k[5][pyjets_maxn]
virtual void book()
const uint32_t & calSel() const
std::vector< uint16_t > ped
DQMStore *const dqm() const
const T & get() const
Definition: EventSetup.h:55
tuple pid
Definition: sysUtil.py:22
std::vector< std::string > getMEs(void) const
get list of (non-dir) MEs of current directory
Definition: DQMStore.cc:1682
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
collection_type data
Definition: DetSet.h:78
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:2540
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:861
virtual ~CalibrationTask()
MonitorElement * calchanElement_
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
std::vector< HistoSet > calib_
tuple conn
Definition: results_mgr.py:53
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:667
void checkAndSave(const uint16_t &)
const std::string & pwd(void) const
Definition: DQMStore.cc:639