CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
StoreEcalCondition.cc
Go to the documentation of this file.
2 
9 
10 #include <fstream>
11 #include <iostream>
12 #include <string>
13 #include <cstring>
14 #include <time.h>
15 #include <unistd.h>
16 
17 using std::string;
18 using std::cout;
19 using std::endl;
20 
22 
23  prog_name_ = "StoreEcalCondition";
24 
25  logfile_ = iConfig.getParameter< std::string >("logfile");
26  sm_slot_ = iConfig.getUntrackedParameter< unsigned int >("smSlot", 1);
27 
28  typedef std::vector< edm::ParameterSet > Parameters;
29  Parameters toPut=iConfig.getParameter<Parameters>("toPut");
30  for(Parameters::iterator itToPut = toPut.begin(); itToPut != toPut.end(); ++itToPut)
31  {
32  inpFileName_.push_back(itToPut->getUntrackedParameter<std::string>("inputFile"));
33  inpFileNameEE_.push_back(itToPut->getUntrackedParameter<std::string>("inputFileEE"));
34  objectName_.push_back(itToPut->getUntrackedParameter<std::string>("conditionType"));
35  since_.push_back(itToPut->getUntrackedParameter<unsigned int>("since"));
36  }
37 
38  sm_constr_ = -1;
39 }
40 
42 
44  if( !mydbservice.isAvailable() ){
45  edm::LogError("StoreEcalCondition")<<"PoolDBOutputService is unavailable"<<std::endl;
46  return;
47  }
48 
49  bool toAppend=false;
50  // copy a string to the char *
51  std::string message = "finished OK\n";
52  size_t messageSize = message.size() + 1;
53  char * messChar = new char [messageSize];
54  strncpy(messChar, message.c_str(), messageSize);
55 
56  for (unsigned int i=0;i<objectName_.size();i++) {
57  cond::Time_t newTime;
58 
59  if ( mydbservice->isNewTagRequest( objectName_[i]+std::string("Rcd") ) ) {
60  // This is the first object for this tag.
61  // Append mode should be off.
62  // newTime is the end of this new objects IOV.
63  newTime = mydbservice->beginOfTime();
64  } else {
65  // There should already be an object in the DB for this tag.
66  // Append IOV mode should be on.
67  // newTime is the beginning of this new objects IOV.
68  toAppend=true;
69  newTime = (cond::Time_t)since_[i];
70  }
71  edm::LogInfo("StoreEcalCondition") << "Reading " << objectName_[i]
72  << " from file and writing to DB with newTime " << newTime << endl;
73  std::cout << "Reading " << objectName_[i]
74  << " from file and writing to DB with newTime " << newTime << endl;
75 
76  if (objectName_[i] == "EcalWeightXtalGroups") {
78  if(!toAppend){
79  mydbservice->createNewIOV<EcalWeightXtalGroups>(mycali,newTime,mydbservice->endOfTime(),"EcalWeightXtalGroupsRcd");
80  }else{
81  mydbservice->appendSinceTime<EcalWeightXtalGroups>(mycali,newTime,"EcalWeightXtalGroupsRcd");
82  }
83  }else if (objectName_[i] =="EcalTBWeights") {
85  if(!toAppend){
86  mydbservice->createNewIOV<EcalTBWeights>(mycali,newTime,mydbservice->endOfTime(),"EcalTBWeightsRcd");
87  }else{
88  mydbservice->appendSinceTime<EcalTBWeights>(mycali,newTime,"EcalTBWeightsRcd");
89  }
90  } else if (objectName_[i] == "EcalADCToGeVConstant") {
92  if(!toAppend){
93  mydbservice->createNewIOV<EcalADCToGeVConstant>(mycali,newTime,mydbservice->endOfTime(),"EcalADCToGeVConstantRcd");
94  }else{
95  mydbservice->appendSinceTime<EcalADCToGeVConstant>(mycali,newTime,"EcalADCToGeVConstantRcd");
96  }
97  } else if (objectName_[i] == "EcalIntercalibConstants") {
99  if(!toAppend){
100  mydbservice->createNewIOV<EcalIntercalibConstants>(mycali,newTime,mydbservice->endOfTime(),"EcalIntercalibConstantsRcd");
101  }else{
102  mydbservice->appendSinceTime<EcalIntercalibConstants>(mycali,newTime,"EcalIntercalibConstantsRcd");
103  }
104  } else if (objectName_[i] == "EcalIntercalibConstantsMC") {
106  if(!toAppend){
107  mydbservice->createNewIOV<EcalIntercalibConstantsMC>(mycali,newTime,mydbservice->endOfTime(),"EcalIntercalibConstantsMCRcd");
108  }else{
109  mydbservice->appendSinceTime<EcalIntercalibConstantsMC>(mycali,newTime,"EcalIntercalibConstantsMCRcd");
110  }
111  } else if (objectName_[i] == "EcalGainRatios") {
113  if(!toAppend){
114  mydbservice->createNewIOV<EcalGainRatios>(mycali,newTime,mydbservice->endOfTime(),"EcalGainRatiosRcd");
115  }else{
116  mydbservice->appendSinceTime<EcalGainRatios>(mycali,newTime,"EcalGainRatiosRcd");
117  }
118  } else if (objectName_[i] == "EcalChannelStatus")
119  {
121  if(!toAppend){
122  mydbservice->createNewIOV<EcalChannelStatus>(mycali,newTime,mydbservice->endOfTime(),"EcalChannelStatusRcd");
123  }else{
124  mydbservice->appendSinceTime<EcalChannelStatus>(mycali,newTime,"EcalChannelStatusRcd");
125  }
126  } else {
127  edm::LogError("StoreEcalCondition")<< "Object " << objectName_[i] << " is not supported by this program." << endl;
128  }
129 
130 
131  // writeToLogFile(objectName_[i], inpFileName_[i], since_[i]);
132  //writeToLogFileResults("finished OK\n");
133  writeToLogFileResults(messChar);
134 
135  edm::LogInfo("StoreEcalCondition") << "Finished endJob" << endl;
136  }
137 
138  delete [] messChar;
139 }
140 
141 
143 
144 }
145 
146 //-------------------------------------------------------------
147 void StoreEcalCondition::analyze( const edm::Event& evt, const edm::EventSetup& evtSetup) {
148 //-------------------------------------------------------------
149 
150 }
151 
152 
153 //------------------------------------------------------------
154 void StoreEcalCondition::writeToLogFile(string a, string b, unsigned long long since) {
155 //-------------------------------------------------------------
156 
157  FILE *outFile; // output log file for appending
158  outFile = fopen(logfile_.c_str(),"a");
159  if(!outFile) {
160  edm::LogError("StoreEcalCondition") <<"*** Can not open file: " << logfile_;
161  return;
162  }
163  char header[256];
164  fillHeader(header);
165  char appendMode[10];
166  if (since != 0)
167  sprintf(appendMode,"append");
168  else
169  sprintf(appendMode,"create");
170 
171  fprintf(outFile, "%s %s condition from file %s written into DB for SM %d (mapped to SM %d) in %s mode (since run %u)\n",
172  header, a.c_str(),b .c_str(), sm_constr_, sm_slot_, appendMode, (unsigned int)since);
173 
174  fclose(outFile); // close out file
175 
176 }
177 //------------------------------------------------------------
179 //-------------------------------------------------------------
180 
181  FILE *outFile; // output log file for appending
182  outFile = fopen(logfile_.c_str(),"a");
183  if(!outFile) {
184  edm::LogError("StoreEcalCondition")<<"*** Can not open file: " << logfile_;
185  return;
186  }
187  char header[256];
188  fillHeader(header);
189  fprintf(outFile, "%s %s\n", header,arg);
190  fclose(outFile); // close out file
191 }
192 
193 //------------------------------------------------------------
195 //------------------------------------------------------------
196 {
197  time_t rawtime;
198  struct tm * timeinfo;
199  time ( &rawtime );
200  timeinfo = localtime ( &rawtime );
201  char user[50];
202  sprintf(user,"%s",getlogin());
203  sprintf(header,"%s %s:",asctime(timeinfo),user);
204 }
205 
206 /*
207  * Format for ASCII groups & weights file
208  * Updated by Alex Zabi Imperial College
209  * 03/07/06: implementing final weights format
210  * Accepted format:
211  * groupId nSamples nTDCbins (introductory line)
212  * and then nTDCbins x (3 + nSamples) lines of nSamples numbers containing
213  * For TDCbin1
214  * gain 12 weights (before gain switch)
215  * ===========================================
216  * ampWeight[0] ampWeight[1] ............. |
217  * pedWeight[0] pedWeight[1] ............. |
218  * jitWeight[0] jitWeight[1] ............. |
219  * chi2Matri[0][0] chi2Matrix[0][1] .......... |
220  * chi2Matrix[1][0] chi2Matrix[1][1] .......... |
221  * ..... |
222  * chi2Matrix[nsamples-1][0] chi2Matrix[nsamples-1][1] .......... |
223  * gain 6 and 1 weights (after gain switch)
224  * ===========================================
225  * ampWeight[0] ampWeight[1] ............. |
226  * pedWeight[0] pedWeight[1] ............. |
227  * jitWeight[0] jitWeight[1] ............. |
228  * chi2Matri[0][0] chi2Matrix[0][1] .......... |
229  * chi2Matrix[1][0] chi2Matrix[1][1] .......... |
230  * ..... |
231  * chi2Matrix[nsamples-1][0] chi2Matrix[nsamples-1][1] .......... |
232  * ===========================================
233  * For TDCbin nTDCBins
234  * ............
235  */
236 
237 //-------------------------------------------------------------
240 //-------------------------------------------------------------
241 
242 // Code taken from EcalWeightTools/test/MakeOfflineDbFromAscii.cpp
243 
244  EcalWeightXtalGroups* xtalGroups = new EcalWeightXtalGroups();
245  std::ifstream groupid_in(inputFile);
246 
247  if(!groupid_in.is_open()) {
248  edm::LogError("StoreEcalCondition")<< "*** Can not open file: "<< inputFile ;
249  return 0;
250  }
251 
252  int smnumber=-99999;
253 
254  std::ostringstream str;
255  groupid_in >> smnumber;
256  if (smnumber == -99999) {
257  edm::LogError("StoreEcalCondition") << "ERROR: SM number not found in file" << endl;
258  return 0;
259  }
260  str << "sm= " << smnumber << endl;
261  sm_constr_ = smnumber;
262 
263  char temp[256];
264  //Reading the other 5 header lines containing various informations
265  for (int i=0;i<=5;i++) {
266  groupid_in.getline(temp,255);
267  str << temp << endl;
268  }
269 
270  // Skip the nGroup/Mean line
271  groupid_in.getline(temp, 255);
272  str << temp << endl;
273 
274  edm::LogInfo("StoreEcalCondition") << "GROUPID file " << str.str() ;
275 
276  int xtals = 0;
277  int xtal, ietaf, iphif, groupID;
278  while (groupid_in.good()) {
279  groupid_in >> xtal >> ietaf >> iphif >> groupID;
280  if (groupid_in.eof()) { break; }
281 
282  LogDebug("StoreEcalCondition") << "XTAL=" << xtal << " ETA=" << ietaf << " PHI=" << iphif
283  << " GROUP=" << groupID ;
284 
285  //EBDetId ebid(ieta,iphi);
287  // xtalGroups->setValue(ebid.rawId(), EcalXtalGroupId( ebid.hashedIndex()) );
288  xtalGroups->setValue(ebid.rawId(), EcalXtalGroupId( groupID ) );
289  xtals++;
290  }//loop iphi
291 
292  if (xtals != 1700) {
293  edm::LogError("StoreEcalCondition") << "ERROR: GROUPID file did not contain data for 1700 crystals" << endl;
294  return 0;
295  }
296 
297  edm::LogInfo("StoreEcalCondition") << "Groups for " << xtals << " xtals written into DB" ;
298  sm_constr_ = smnumber;
299 
300  return xtalGroups;
301 }
302 
303 //-------------------------------------------------------------
306  //-------------------------------------------------------------
307 
308  // Zabi code to be written here
309 
310  EcalTBWeights* tbwgt = new EcalTBWeights();
311 
312  std::ifstream WeightsFileTB(inputFile);
313  if(!WeightsFileTB.is_open()) {
314  edm::LogError("StoreEcalCondition")<< "*** Can not open file: "<< inputFile ;
315  return 0;
316  }
317 
318  int smnumber=-99999;
319 
320  std::ostringstream str;
321  WeightsFileTB >> smnumber;
322  if (smnumber == -99999)
323  return 0;
324 
325  str << "sm= " << smnumber << endl;
326 
327  char temp[256];
328  //Reading the other 5 header lines containing various informations
329  for (int i=0;i<=5;i++)
330  {
331  WeightsFileTB.getline(temp,255);
332  str << temp << endl;
333  }
334 
335  edm::LogInfo("StoreEcalCondition") << "Weights file " << str.str() ;
336 
337  int ngroups=0;
338  while (WeightsFileTB.good())
339  {
340  int igroup_ID = -99999;
341  int nSamples = -99999;
342  int nTdcBins = -99999;
343 
344  WeightsFileTB >> igroup_ID >> nSamples >> nTdcBins;
345  if (igroup_ID == -99999 || nSamples == -99999 || nTdcBins == -99999)
346  break;
347 
348  std::ostringstream str;
349  str << "Igroup=" << igroup_ID << " Nsamples=" << nSamples << " NTdcBins=" << nTdcBins <<"\n" ;
350 
351  for (int iTdcBin = 0; iTdcBin < nTdcBins; iTdcBin++) {
352 
353  EcalWeightSet wgt; // one set of weights
358 
359 // std::vector<EcalWeight> wamp, wped, wtime; //weights before gain switch
360 // std::vector<EcalWeight> wamp2, wped2, wtime2; //weights after gain switch
361 
362  //WEIGHTS BEFORE GAIN SWITCH
363  //Amplitude weights
364  for(int j = 0; j < nSamples; ++j) {
365  double ww = 0.0; WeightsFileTB >> ww;
366  wgt1(0,j)=ww;
367  str << ww << " ";
368  }// loop Samples
369  str << std::endl;
370 
371  //Pedestal weights
372  for(int j = 0; j < nSamples; ++j) {
373  double ww = 0.0; WeightsFileTB >> ww;
374  wgt1(1,j)=ww;
375  str << ww << " ";
376  }//loop Samples
377  str << std::endl;
378 
379  //Timing weights
380  for(int j = 0; j < nSamples; ++j) {
381  double ww = 0.0; WeightsFileTB >> ww;
382  wgt1(2,j)=ww;
383  str << ww << " ";
384  }//loop Samples
385  str << std::endl;
386 
387  for(int j = 0; j < nSamples; ++j) {
388  // fill chi2 matrix
389  //std::vector<EcalWeight> vChi2; // row of chi2 matrix
390  for(int k = 0; k < nSamples; ++k) {
391  double ww = 0.0; WeightsFileTB >> ww;
392  chisq1(j,k)=ww;
393  str << ww << " ";
394  } //loop samples
395  str << std::endl;
396  }//loop lines
397 
398  //WEIGHTS AFTER GAIN SWITCH
399  for(int j = 0; j < nSamples; ++j) {
400  double ww = 0.0; WeightsFileTB >> ww;
401  wgt2(0,j)=ww;
402  str << ww << " ";
403  }// loop Samples
404  str << std::endl;
405 
406  //Pedestal weights
407  for(int j = 0; j < nSamples; ++j) {
408  double ww = 0.0; WeightsFileTB >> ww;
409  wgt2(1,j)=ww;
410  str << ww << " ";
411  }//loop Samples
412  str << std::endl;
413 
414  //Timing weights
415  for(int j = 0; j < nSamples; ++j) {
416  double ww = 0.0; WeightsFileTB >> ww;
417  wgt2(2,j)=ww;
418  str << ww << " ";
419  }//loop Samples
420  str << std::endl;
421 
422  for(int j = 0; j < nSamples; ++j) {
423  // fill chi2 matrix
424  //std::vector<EcalWeight> vChi2; // row of chi2 matrix
425  for(int k = 0; k < nSamples; ++k) {
426  double ww = 0.0; WeightsFileTB >> ww;
427  chisq2(j,k)=ww;
428  str << ww << " ";
429  } //loop samples
430  str << std::endl;
431  }//loop lines
432 
433  LogDebug("StoreEcalCondition") << str.str();
434 
435  //modif-27-07-06 tdcid should start from 1
436  tbwgt->setValue(std::make_pair( igroup_ID , iTdcBin+1 ), wgt);
437  }//loop Tdc bins
438  ngroups++;
439  }//loop groupID
440 
441  sm_constr_ = smnumber;
442 
443 
444  edm::LogInfo("StoreEcalCondition") << "Weights for " << ngroups << " groups written into DB" ;
445  return tbwgt;
446 }
447 
448 
449 //-------------------------------------------------------------
452 //-------------------------------------------------------------
453 
454 
455  FILE *inpFile; // input file
456  inpFile = fopen(inputFile,"r");
457  if(!inpFile) {
458  edm::LogError("StoreEcalCondition")<<"*** Can not open file: "<<inputFile;
459  return 0;
460  }
461 
462  char line[256];
463 
464  std::ostringstream str;
465 
466  fgets(line,255,inpFile);
467  int sm_number=atoi(line);
468  str << "sm= " << sm_number << endl ;
469 
470  fgets(line,255,inpFile);
471  //int nevents=atoi(line); // not necessary here just for online conddb
472 
473  fgets(line,255,inpFile);
474  string gen_tag=to_string(line);
475  str << "gen tag " << gen_tag << endl ; // should I use this?
476 
477  fgets(line,255,inpFile);
478  string cali_method=to_string(line);
479  str << "cali method " << cali_method << endl ; // not important
480 
481  fgets(line,255,inpFile);
482  string cali_version=to_string(line);
483  str << "cali version " << cali_version << endl ; // not important
484 
485  fgets(line,255,inpFile);
486  string cali_type=to_string(line);
487  str << "cali type " << cali_type << endl ; // not important
488 
489  edm::LogInfo("StoreEcalCondition") << "ADCToGeV file " << str.str() ;
490 
491  fgets(line,255,inpFile);
492  float adc_to_gev=0;
493  sscanf(line, "%f", &adc_to_gev );
494  LogDebug("StoreEcalCondition") <<" calib="<< adc_to_gev ;
495  fgets(line,255,inpFile);
496  float adc_to_gev_ee=0;
497  sscanf(line, "%f", &adc_to_gev_ee );
498  LogDebug("StoreEcalCondition") <<" calib="<< adc_to_gev_ee ;
499 
500  fclose(inpFile); // close inp. file
501 
502  sm_constr_ = sm_number;
503 
504  // barrel and endcaps the same
505  EcalADCToGeVConstant* agc = new EcalADCToGeVConstant(adc_to_gev,adc_to_gev_ee );
506  edm::LogInfo("StoreEcalCondition") << "ADCtoGeV scale written into the DB";
507  return agc;
508 }
509 
510 
511 //-------------------------------------------------------------
514 //-------------------------------------------------------------
515 
517 
518 
519  FILE *inpFile; // input file
520  inpFile = fopen(inputFile,"r");
521  if(!inpFile) {
522  edm::LogError("StoreEcalCondition")<<"*** Can not open file: "<<inputFile;
523  return 0;
524  }
525 
526  char line[256];
527 
528  std::ostringstream str;
529 
530  fgets(line,255,inpFile);
531  string sm_or_all=to_string(line);
532  int sm_number=0;
533  int nchan=1700;
534  sm_number=atoi(line);
535  str << "sm= " << sm_number << endl ;
536  if(sm_number!=-1){
537  nchan=1700;
538  } else {
539  nchan=61200;
540  }
541 
542 
543  fgets(line,255,inpFile);
544  //int nevents=atoi(line); // not necessary here just for online conddb
545 
546  fgets(line,255,inpFile);
547  string gen_tag=to_string(line);
548  str << "gen tag " << gen_tag << endl ; // should I use this?
549 
550  fgets(line,255,inpFile);
551  string cali_method=to_string(line);
552  str << "cali method " << cali_method << endl ; // not important
553 
554  fgets(line,255,inpFile);
555  string cali_version=to_string(line);
556  str << "cali version " << cali_version << endl ; // not important
557 
558  fgets(line,255,inpFile);
559  string cali_type=to_string(line);
560  str << "cali type " << cali_type << endl ; // not important
561 
562  edm::LogInfo("StoreEcalCondition") << "Intercalibration file " << str.str() ;
563 
564  int sm_num[61200]={0};
565  int cry_num[61200]={0};
566  float calib[61200]={0};
567  float calib_rms[61200]={0};
568  int calib_nevents[61200]={0};
569  int calib_status[61200]={0};
570 
571  int ii = 0;
572  if(sm_number!=-1){
573  while(fgets(line,255,inpFile)) {
574  sscanf(line, "%d %f %f %d %d", &cry_num[ii], &calib[ii], &calib_rms[ii], &calib_nevents[ii], &calib_status[ii] );
575 // if(ii<10) { // print out only the first ten channels
576 // cout << "cry="<<cry_num[ii]<<" calib="<<calib[ii]<<endl;
577 // }
578  sm_num[ii]=sm_number;
579  ii++ ;
580  }
581  } else {
582  // this is for the whole Barrel
583  cout<<"mode ALL BARREL" <<endl;
584  while(fgets(line,255,inpFile)) {
585  sscanf(line, "%d %d %f %f %d", &sm_num[ii], &cry_num[ii], &calib[ii], &calib_rms[ii], &calib_status[ii] );
586  if(ii==0) cout<<"crystal "<<cry_num[ii]<<" of sm "<<sm_num[ii]<<" cali= "<< calib[ii]<<endl;
587  ii++ ;
588  }
589  }
590 
591  // inf.close(); // close inp. file
592  fclose(inpFile); // close inp. file
593 
594  edm::LogInfo("StoreEcalCondition") << "Read intercalibrations for " << ii << " xtals " ;
595 
596  cout << " I read the calibrations for "<< ii<< " crystals " << endl;
597  if(ii!=nchan) edm::LogWarning("StoreEcalCondition") << "Some crystals missing. Missing channels will be set to 0" << endl;
598 
599  // Get channel ID
600 
601  sm_constr_ = sm_number;
602 
603 
604  // Set the data
605  for(int i=0; i<nchan; i++){
606 
607  // EBDetId(int index1, int index2, int mode = ETAPHIMODE)
608  // sm and crys index SMCRYSTALMODE index1 is SM index2 is crystal number a la H4
609 
610  int slot_num=convertFromConstructionSMToSlot(sm_num[i],-1);
611  EBDetId ebid(slot_num,cry_num[i],EBDetId::SMCRYSTALMODE);
612 
613  ical->setValue( ebid.rawId(), calib[i] );
614 
615  if(i==0) cout<<"crystal "<<cry_num[i]<<" of sm "<<sm_num[i]<< " in slot " <<slot_num<<" calib= "<< calib[i]<<endl;
616 
617  } // loop over channels
618 
619  cout<<"loop on channels done" << endl;
620 
621  FILE *inpFileEE; // input file
622  inpFileEE = fopen(inputFileEE,"r");
623  if(!inpFileEE) {
624  edm::LogError("StoreEcalCondition")<<"*** Can not open file: "<<inputFile;
625 
626  // dummy endcap data
627 
628  for(int iX=EEDetId::IX_MIN; iX<=EEDetId::IX_MAX ;++iX)
629  {
630  for(int iY=EEDetId::IY_MIN; iY<=EEDetId::IY_MAX; ++iY)
631  {
632  // make an EEDetId since we need EEDetId::rawId() to be used as the key for the pedestals
633  if (EEDetId::validDetId(iX,iY,1))
634  {
635  EEDetId eedetidpos(iX,iY,1);
636  ical->setValue( eedetidpos, 1.0 );
637  }
638  if (EEDetId::validDetId(iX,iY,-1))
639  {
640  EEDetId eedetidneg(iX,iY,-1);
641  ical->setValue( eedetidneg, 1.0 );
642  }
643  }
644  }
645 
646  } else {
647  cout<<"... now reading EE file ..." <<endl;
648 
649  int ii=0;
650  while(fgets(line,255,inpFileEE)) {
651  int iz,ix,iy;
652  float calibee;
653  sscanf(line, "%d %d %d %f", &iz, &ix, &iy, &calibee );
654  if(ii<=0) cout<<"crystal "<<iz<<"/"<<ix<<"/"<<iy<<" cali="<< calibee<<endl;
655 
656  if (EEDetId::validDetId(ix,iy,iz))
657  {
658  EEDetId eedetid(ix,iy,iz);
659  ical->setValue( eedetid, calibee );
660  }
661 
662  ii++ ;
663  }
664 
665 
666  fclose(inpFileEE); // close inp. file
667 
668  }
669 
670  cout<<"loop on EE channels done" << endl;
671 
672 
673 
674  return ical;
675 }
676 
677 //-------------------------------------------------------------
680 //-------------------------------------------------------------
681 
683 
684 
685  FILE *inpFile; // input file
686  inpFile = fopen(inputFile,"r");
687  if(!inpFile) {
688  edm::LogError("StoreEcalCondition")<<"*** Can not open file: "<<inputFile;
689  return 0;
690  }
691 
692  char line[256];
693 
694  std::ostringstream str;
695 
696  fgets(line,255,inpFile);
697  string sm_or_all=to_string(line);
698  int sm_number=0;
699  int nchan=1700;
700  sm_number=atoi(line);
701  str << "sm= " << sm_number << endl ;
702  if(sm_number!=-1){
703  nchan=1700;
704  } else {
705  nchan=61200;
706  }
707 
708 
709  fgets(line,255,inpFile);
710  //int nevents=atoi(line); // not necessary here just for online conddb
711 
712  fgets(line,255,inpFile);
713  string gen_tag=to_string(line);
714  str << "gen tag " << gen_tag << endl ; // should I use this?
715 
716  fgets(line,255,inpFile);
717  string cali_method=to_string(line);
718  str << "cali method " << cali_method << endl ; // not important
719 
720  fgets(line,255,inpFile);
721  string cali_version=to_string(line);
722  str << "cali version " << cali_version << endl ; // not important
723 
724  fgets(line,255,inpFile);
725  string cali_type=to_string(line);
726  str << "cali type " << cali_type << endl ; // not important
727 
728  edm::LogInfo("StoreEcalCondition") << "Intercalibration file " << str.str() ;
729 
730  int sm_num[61200]={0};
731  int cry_num[61200]={0};
732  float calib[61200]={0};
733  float calib_rms[61200]={0};
734  int calib_nevents[61200]={0};
735  int calib_status[61200]={0};
736 
737  int ii = 0;
738  if(sm_number!=-1){
739  while(fgets(line,255,inpFile)) {
740  sscanf(line, "%d %f %f %d %d", &cry_num[ii], &calib[ii], &calib_rms[ii], &calib_nevents[ii], &calib_status[ii] );
741 // if(ii<10) { // print out only the first ten channels
742 // cout << "cry="<<cry_num[ii]<<" calib="<<calib[ii]<<endl;
743 // }
744  sm_num[ii]=sm_number;
745  ii++ ;
746  }
747  } else {
748  // this is for the whole Barrel
749  cout<<"mode ALL BARREL" <<endl;
750  while(fgets(line,255,inpFile)) {
751  sscanf(line, "%d %d %f %f %d", &sm_num[ii], &cry_num[ii], &calib[ii], &calib_rms[ii], &calib_status[ii] );
752  if(ii==0) cout<<"crystal "<<cry_num[ii]<<" of sm "<<sm_num[ii]<<" cali= "<< calib[ii]<<endl;
753  ii++ ;
754  }
755  }
756 
757  // inf.close(); // close inp. file
758  fclose(inpFile); // close inp. file
759 
760  edm::LogInfo("StoreEcalCondition") << "Read intercalibrations for " << ii << " xtals " ;
761 
762  cout << " I read the calibrations for "<< ii<< " crystals " << endl;
763  if(ii!=nchan) edm::LogWarning("StoreEcalCondition") << "Some crystals missing. Missing channels will be set to 0" << endl;
764 
765  // Get channel ID
766 
767  sm_constr_ = sm_number;
768 
769 
770  // Set the data
771  for(int i=0; i<nchan; i++){
772 
773  // EBDetId(int index1, int index2, int mode = ETAPHIMODE)
774  // sm and crys index SMCRYSTALMODE index1 is SM index2 is crystal number a la H4
775 
776  int slot_num=convertFromConstructionSMToSlot(sm_num[i],-1);
777  EBDetId ebid(slot_num,cry_num[i],EBDetId::SMCRYSTALMODE);
778 
779  ical->setValue( ebid.rawId(), calib[i] );
780 
781  if(i==0) cout<<"crystal "<<cry_num[i]<<" of sm "<<sm_num[i]<< " in slot " <<slot_num<<" calib= "<< calib[i]<<endl;
782 
783  } // loop over channels
784 
785  cout<<"loop on channels done" << endl;
786 
787  FILE *inpFileEE; // input file
788  inpFileEE = fopen(inputFileEE,"r");
789  if(!inpFileEE) {
790  edm::LogError("StoreEcalCondition")<<"*** Can not open file: "<<inputFile;
791 
792  // dummy endcap data
793 
794  for(int iX=EEDetId::IX_MIN; iX<=EEDetId::IX_MAX ;++iX)
795  {
796  for(int iY=EEDetId::IY_MIN; iY<=EEDetId::IY_MAX; ++iY)
797  {
798  // make an EEDetId since we need EEDetId::rawId() to be used as the key for the pedestals
799  if (EEDetId::validDetId(iX,iY,1))
800  {
801  EEDetId eedetidpos(iX,iY,1);
802  ical->setValue( eedetidpos, 1.0 );
803  }
804  if (EEDetId::validDetId(iX,iY,-1))
805  {
806  EEDetId eedetidneg(iX,iY,-1);
807  ical->setValue( eedetidneg, 1.0 );
808  }
809  }
810  }
811 
812  } else {
813  cout<<"... now reading EE file ..." <<endl;
814 
815  int ii=0;
816  while(fgets(line,255,inpFileEE)) {
817  int iz,ix,iy;
818  float calibee;
819  sscanf(line, "%d %d %d %f", &iz, &ix, &iy, &calibee );
820  if(ii<=0) cout<<"crystal "<<iz<<"/"<<ix<<"/"<<iy<<" cali="<< calibee<<endl;
821 
822  if (EEDetId::validDetId(ix,iy,iz))
823  {
824  EEDetId eedetid(ix,iy,iz);
825  ical->setValue( eedetid, calibee );
826  }
827 
828  ii++ ;
829  }
830 
831 
832  fclose(inpFileEE); // close inp. file
833 
834  }
835 
836  cout<<"loop on EE channels done" << endl;
837 
838 
839 
840  return ical;
841 }
842 
843 //-------------------------------------------------------------
845  // input either cosntruction number or slot number and returns the other
846  // the slots are numbered first EB+ slot 1 ...18 then EB- 1... 18
847  // the slots start at 1 and the SM start at 0
848 //-------------------------------------------------------------
849  int slot_to_constr[37]={-1,12,17,10,1,8,4,27,20,23,25,6,34,35,15,18,30,21,9
850  ,24,22,13,31,26,16,2,11,5,0,29,28,14,33,32,3,7,19};
851  int constr_to_slot[36]={28,4,25,34,6,27,11,35,5,18,3,26,1,21,31,14,24,2,15,
852  36,8,17,20,9,19,10,23,7,30,29,16,22,33,32,12,13 };
853 
854  int result=0;
855  if(sm_constr!=-1) {
856  result=constr_to_slot[sm_constr];
857  } else if(sm_slot!=-1) {
858  result=slot_to_constr[sm_slot];
859  }
860  return result;
861 }
862 
863 //-------------------------------------------------------------
866 //-------------------------------------------------------------
867 
868  // create gain ratios
869  EcalGainRatios* gratio = new EcalGainRatios;
870 
871 
872  FILE *inpFile; // input file
873  inpFile = fopen(inputFile,"r");
874  if(!inpFile) {
875  edm::LogError("StoreEcalCondition")<<"*** Can not open file: "<<inputFile;
876  return 0;
877  }
878 
879  char line[256];
880  std::ostringstream str;
881 
882  fgets(line,255,inpFile);
883  string sm_or_all=to_string(line);
884  int sm_number=0;
885  sm_number=atoi(line);
886  str << "sm= " << sm_number << endl ;
887 
888  fgets(line,255,inpFile);
889  //int nevents=atoi(line);
890 
891  fgets(line,255,inpFile);
892  string gen_tag=to_string(line);
893  str << "gen tag " << gen_tag << endl ;
894 
895  fgets(line,255,inpFile);
896  string cali_method=to_string(line);
897  str << "cali method " << cali_method << endl ;
898 
899  fgets(line,255,inpFile);
900  string cali_version=to_string(line);
901  str << "cali version " << cali_version << endl ;
902 
903 
904  fgets(line,255,inpFile);
905  string cali_type=to_string(line);
906 
907  str << "cali type " << cali_type << endl ;
908 
909  edm::LogInfo("StoreEcalCondition") << "GainRatio file " << str.str() ;
910 
911 
912 
913  int cry_num[61200]={0};
914  float g1_g12[61200]={0};
915  float g6_g12[61200]={0};
916  int calib_status[61200]={0};
917  int dummy1=0;
918  int dummy2=0;
919  int hash1=0;
920 
921  int ii = 0;
922 
923  if(sm_number!=-1){
924  while(fgets(line,255,inpFile)) {
925  sscanf(line, "%d %d %d %f %f %d", &dummy1, &dummy2, &cry_num[ii], &g1_g12[ii], &g6_g12[ii], &calib_status[ii] );
926  ii++ ;
927  }
928 
929 
930  fclose(inpFile); // close inp. file
931 
932 
933 
934  edm::LogInfo("StoreEcalCondition") << "Read gainRatios for " << ii << " xtals " ;
935  if(ii!=1700) edm::LogWarning("StoreEcalCondition") << " Missing crystals:: missing channels will be set to 0" << endl;
936 
937  // Get channel ID
938  sm_constr_ = sm_number;
939 
940  for(int i=0; i<1700; i++){
941  // EBDetId(int index1, int index2, int mode = ETAPHIMODE)
942  // sm and crys index SMCRYSTALMODE index1 is SM index2 is crystal number a la H4
943  EBDetId ebid(sm_slot_,cry_num[i],EBDetId::SMCRYSTALMODE);
944  // cout << "ebid.rawId()"<< ebid.rawId()<< endl;
946  gr.setGain12Over6( g6_g12[i] );
947  gr.setGain6Over1(g1_g12[i]/g6_g12[i]);
948  gratio->setValue( ebid.rawId(), gr );
949  } // loop over channels
950 
951 
952 
953 
954 
955 
956  } else {
957  // this is for the whole Barrel
958  cout<<"mode ALL BARREL" <<endl;
959  while(fgets(line,255,inpFile)) {
960  int eta=0;
961  int phi=0;
962  sscanf(line, "%d %d %d %f %f",&hash1, &eta, &phi, &g1_g12[ii], &g6_g12[ii]);
963  if(ii<20) cout<<"crystal eta/phi="<<eta<<"/"<<phi<<" g1_12/g6_12= "<< g1_g12[ii]<<"/"<<g6_g12[ii]<<endl;
964 
965  if(g1_g12[ii]<9 || g1_g12[ii]>15 ) g1_g12[ii]=12.0;
966  if(g6_g12[ii]<1 || g6_g12[ii]>3 ) g6_g12[ii]=2.0;
967 
968  if(eta<-85|| eta>85 || eta==0) std::cout<<"error!!!"<<endl;
969  if(phi<1 || phi>360) std::cout<<"error!!!"<<endl;
970 
971  EBDetId ebid(eta, phi,EBDetId::ETAPHIMODE);
973  gr.setGain12Over6( g6_g12[ii] );
974  gr.setGain6Over1(g1_g12[ii]/g6_g12[ii]);
975  gratio->setValue( ebid.rawId(), gr );
976 
977  ii++ ;
978  }
979 
980  fclose(inpFile); // close inp. file
981  if(ii!=61200) edm::LogWarning("StoreEcalCondition") << " Missing crystals !!!!!!!" << endl;
982 
983  std::cout<< "number of crystals read:"<<ii<<endl;
984 
985  }
986 
987 
988 
989  for(int iX=EEDetId::IX_MIN; iX<=EEDetId::IX_MAX ;++iX) {
990  for(int iY=EEDetId::IY_MIN; iY<=EEDetId::IY_MAX; ++iY) {
991  // make an EEDetId since we need EEDetId::rawId() to be used as the key for the pedestals
993  gr.setGain12Over6( 2. );
994  gr.setGain6Over1( 6. );
995 
996 
997  if (EEDetId::validDetId(iX,iY,1))
998  {
999  EEDetId eedetidpos(iX,iY,1);
1000  gratio->setValue( eedetidpos.rawId(), gr );
1001  }
1002  if (EEDetId::validDetId(iX,iY,-1))
1003  {
1004  EEDetId eedetidneg(iX,iY,-1);
1005  gratio->setValue( eedetidneg.rawId(), gr );
1006  }
1007  }
1008  }
1009 
1010 
1011  std::cout <<" gratio pointer="<<gratio<<endl;
1012 
1013 
1014 
1015  std::cout<< "now leaving"<<endl;
1016 
1017 
1018 
1019  return gratio;
1020 
1021 }
1022 
1025 {
1027  // barrel
1028  for(int ieta=-EBDetId::MAX_IETA; ieta<=EBDetId::MAX_IETA; ++ieta)
1029  {
1030  if(ieta==0) continue;
1031  for(int iphi=EBDetId::MIN_IPHI; iphi<=EBDetId::MAX_IPHI; ++iphi) {
1032  if (EBDetId::validDetId(ieta,iphi)) {
1033  EBDetId ebid(ieta,iphi);
1034  status->setValue( ebid, 0 );
1035  }
1036  }
1037  }
1038  // endcap
1039  for(int iX=EEDetId::IX_MIN; iX<=EEDetId::IX_MAX ;++iX)
1040  {
1041  for(int iY=EEDetId::IY_MIN; iY<=EEDetId::IY_MAX; ++iY)
1042  {
1043  // make an EEDetId since we need EEDetId::rawId() to be used as the key for the pedestals
1044  if (EEDetId::validDetId(iX,iY,1))
1045  {
1046  EEDetId eedetidpos(iX,iY,1);
1047  status->setValue( eedetidpos, 0 );
1048  }
1049  if (EEDetId::validDetId(iX,iY,-1))
1050  {
1051  EEDetId eedetidneg(iX,iY,-1);
1052  status->setValue( eedetidneg, 0 );
1053  }
1054  }
1055  }
1056 
1057 
1058  // edm::LogInfo("EcalTrivialConditionRetriever") << "Reading channel status from file " << edm::FileInPath(channelStatusFile_).fullPath().c_str() ;
1059  std::cout << "Reading channel status from file " << inputFile << std::endl;
1060  FILE *ifile = fopen( inputFile ,"r" );
1061  if ( !ifile )
1062  throw cms::Exception ("Cannot open ECAL channel status file") ;
1063 
1064  char line[256];
1065 
1066  fgets(line,255,ifile);
1067  std::string gen_tag= line;
1068  std::cout << "Gen tag " << gen_tag << std::endl ;
1069 
1070  fgets(line,255,ifile);
1072  std::cout << "Gen comment " << comment << std::endl ;
1073 
1074  int iovRunStart(0);
1075  fgets(line,255,ifile);
1076  sscanf (line, "%d", &iovRunStart);
1077  std::cout << "IOV START " << iovRunStart << std::endl;
1078  //if -1 start of time
1079 
1080  int iovRunEnd(0);
1081  fgets(line,255,ifile);
1082  sscanf (line, "%d", &iovRunEnd);
1083  std::cout << "IOV END " << iovRunEnd << std::endl;
1084  //if -1 end of time
1085 
1086  int ii = 0;
1087  while(fgets(line,255,ifile))
1088  {
1089  std::string EBorEE;
1090  int hashedIndex(0);
1091  int chStatus(0);
1092  std::stringstream aStrStream;
1093  aStrStream << line;
1094  aStrStream >> EBorEE >> hashedIndex >> chStatus;
1095  // if(ii==0)
1096  std::cout << EBorEE << " hashedIndex " << hashedIndex << " status " << chStatus << std::endl;
1097 
1098  if (EBorEE == "EB")
1099  {
1100  EBDetId aEBDetId=EBDetId::unhashIndex(hashedIndex);
1101  status->setValue( aEBDetId, chStatus );
1102  }
1103  else if (EBorEE == "EE")
1104  {
1105  // chStatus=1;
1106  EEDetId aEEDetId=EEDetId::unhashIndex(hashedIndex);
1107  status->setValue( aEEDetId, chStatus );
1108  }
1109  else if (EBorEE == "EBTT")
1110  {
1111  int ism=hashedIndex;
1112  int itt=chStatus;
1113 
1114  int ixtt=(itt-1)%4;
1115  int iytt=(itt-1)/4;
1116  int ixmin=ixtt*5;
1117  int iymin=iytt*5;
1118  int ixmax=(ixtt+1)*5-1;
1119  int iymax=(iytt+1)*5-1;
1120  for(int ieta=iymin; ieta<=iymax; ieta++) {
1121  for(int iphi=ixmin; iphi<=ixmax; iphi++) {
1122  int ixt=ieta*20+iphi+1;
1123  std::cout<< "killing crystal "<< ism << "/" << ixt << endl;
1124  EBDetId ebid(ism,ixt,EBDetId::SMCRYSTALMODE);
1125  status->setValue( ebid, 1 );
1126  }
1127  }
1128  }
1129 
1130  ii++ ;
1131 
1132  }
1133 
1134  fclose(ifile);
1135 
1136 
1137  /*
1138  std::cout <<"KILLING CHANNELS FOR CRAFT EB+16 AND EB+7"<<endl;
1139 
1140  int ism=7;
1141  for(int ixt=1; ixt<=500; ixt++) {
1142  EBDetId ebid(ism,ixt,EBDetId::SMCRYSTALMODE);
1143  status->setValue( ebid, 1 );
1144  }
1145  for(int ixt=501; ixt<=900; ixt++) {
1146  EBDetId ebid(ism,ixt,EBDetId::SMCRYSTALMODE);
1147  if( ((ixt)%20==0) || ((ixt)%20>10) ){
1148  status->setValue( ebid, 1 );
1149  }
1150  }
1151  ism=16;
1152  for(int ixt=501; ixt<=900; ixt++) {
1153  EBDetId ebid(ism,ixt,EBDetId::SMCRYSTALMODE);
1154  if( ((ixt)%20==0) || ((ixt)%20>10) ){
1155  status->setValue( ebid, 1 );
1156  }
1157  }
1158 
1159  */
1160 
1161 
1162  return status;
1163 
1164 
1165 }
#define LogDebug(id)
EcalChi2WeightMatrix & getChi2WeightsBeforeGainSwitch()
Definition: EcalWeightSet.h:31
T getParameter(std::string const &) const
EcalIntercalibConstantMCMap EcalIntercalibConstantsMC
EcalIntercalibConstantsMC * readEcalIntercalibConstantsMCFromFile(const char *, const char *)
T getUntrackedParameter(std::string const &, T const &) const
std::vector< std::string > inpFileNameEE_
int i
Definition: DBlmapReader.cc:9
static const int MIN_IPHI
Definition: EBDetId.h:142
std::vector< unsigned long long > since_
EcalChannelStatus * readEcalChannelStatusFromFile(const char *)
void writeToLogFileResults(char *)
EcalIntercalibConstants * readEcalIntercalibConstantsFromFile(const char *, const char *)
StoreEcalCondition(const edm::ParameterSet &iConfig)
EcalCondObjectContainer< EcalXtalGroupId > EcalWeightXtalGroups
virtual void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup)
void setValue(const EcalXtalGroupId &groupId, const EcalTDCId &tdcId, const EcalWeightSet &weight)
static EEDetId unhashIndex(int hi)
Definition: EEDetId.cc:99
int ii
Definition: cuy.py:588
A arg
Definition: Factorize.h:36
static const int IX_MIN
Definition: EEDetId.h:294
static const int IY_MIN
Definition: EEDetId.h:298
static bool validDetId(int i, int j)
check if a valid index combination
Definition: EBDetId.h:124
void setGain12Over6(const float &g)
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
void setValue(const uint32_t id, const Item &item)
int hashedIndex(int ieta, int iphi)
Definition: EcalPyUtils.cc:42
void appendSinceTime(T *payloadObj, cond::Time_t sinceTime, const std::string &recordName, bool withlogging=false)
MVATrainerComputer * calib
Definition: MVATrainer.cc:64
unsigned long long Time_t
Definition: Time.h:16
EcalChannelStatusMap EcalChannelStatus
vector< ParameterSet > Parameters
EcalGainRatioMap EcalGainRatios
int nchan
Definition: TauolaWrapper.h:80
bool isNewTagRequest(const std::string &recordName)
std::vector< std::string > objectName_
EcalWeightXtalGroups * readEcalWeightXtalGroupsFromFile(const char *)
tuple result
Definition: query.py:137
EcalGainRatios * readEcalGainRatiosFromFile(const char *)
bool isAvailable() const
Definition: Service.h:46
int j
Definition: DBlmapReader.cc:9
EcalChi2WeightMatrix & getChi2WeightsAfterGainSwitch()
Definition: EcalWeightSet.h:32
std::vector< std::string > inpFileName_
EcalADCToGeVConstant * readEcalADCToGeVConstantFromFile(const char *)
EcalWeightMatrix & getWeightsAfterGainSwitch()
Definition: EcalWeightSet.h:30
static const int ETAPHIMODE
Definition: EBDetId.h:166
void createNewIOV(T *firstPayloadObj, cond::Time_t firstSinceTime, cond::Time_t firstTillTime, const std::string &recordName, bool withlogging=false)
EcalTBWeights * readEcalTBWeightsFromFile(const char *)
void writeToLogFile(std::string, std::string, unsigned long long)
math::Matrix< 10, 10 >::type EcalChi2WeightMatrix
Definition: EcalWeightSet.h:23
static const int IX_MAX
Definition: EEDetId.h:302
std::string to_string(char value[])
int convertFromConstructionSMToSlot(int, int)
static const int MAX_IPHI
Definition: EBDetId.h:144
EcalWeightMatrix & getWeightsBeforeGainSwitch()
Definition: EcalWeightSet.h:29
static bool validDetId(int crystal_ix, int crystal_iy, int iz)
Definition: EEDetId.h:248
static const int MAX_IETA
Definition: EBDetId.h:143
double b
Definition: hdecay.h:120
static EBDetId unhashIndex(int hi)
get a DetId from a compact index for arrays
Definition: EBDetId.h:114
EcalIntercalibConstantMap EcalIntercalibConstants
double a
Definition: hdecay.h:121
math::Matrix< 3, 10 >::type EcalWeightMatrix
Definition: EcalWeightSet.h:22
tuple cout
Definition: gather_cfg.py:121
static const int IY_MAX
Definition: EEDetId.h:306
tuple status
Definition: ntuplemaker.py:245
static const int SMCRYSTALMODE
Definition: EBDetId.h:167
int ism(int ieta, int iphi)
Definition: EcalPyUtils.cc:56
void setGain6Over1(const float &g)
#define comment(par)
Definition: vmac.h:161