CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HcalConstantsASCIIWriter.cc
Go to the documentation of this file.
1 // system include files
2 #include <memory>
3 #include <string>
4 #include <iostream>
5 
6 // user include files
19 
22 #include "HepMC/GenParticle.h"
23 #include "HepMC/GenVertex.h"
26 #include <fstream>
27 #include <sstream>
28 #include <map>
29 #include <vector>
30 
31 using namespace reco;
32 //
33 // constructors and destructor
34 //
35 namespace cms{
36 HcalConstantsASCIIWriter::HcalConstantsASCIIWriter(const edm::ParameterSet& iConfig)
37 {
38  // get name of output file with histogramms
39  file_input="Calibration/HcalCalibAlgos/data/"+iConfig.getParameter <std::string> ("fileInput")+".txt";
40  file_output="Calibration/HcalCalibAlgos/data/"+iConfig.getParameter <std::string> ("fileOutput")+".txt";
41 }
42 
43 HcalConstantsASCIIWriter::~HcalConstantsASCIIWriter()
44 {
45 
46  // do anything here that needs to be done at desctruction time
47  // (e.g. close files, deallocate resources etc.)
48 
49 }
50 
52 {
53  edm::FileInPath f1(file_output);
54  std::string fDataFile = f1.fullPath();
55 
56  myout_hcal = new std::ofstream(fDataFile.c_str());
57  if(!myout_hcal) std::cout << " Output file not open!!! "<<std::endl;
58 
59 }
60 
61 void HcalConstantsASCIIWriter::endJob()
62 {
63  delete myout_hcal;
64 }
65 
66 
67 //
68 // member functions
69 //
70 
71 // ------------ method called to produce the data ------------
72 void
74 {
75 
76  std::cout<<" Start HcalConstantsASCIIWriter::analyze "<<std::endl;
77 
79  iSetup.get<HcalRespCorrsRcd>().get(r);
80  HcalRespCorrs* oldRespCorrs = new HcalRespCorrs(*r.product());
81 // std::vector<DetId> dd = oldRespCorrs->getAllChannels();
82 
84  iSetup.get<CaloGeometryRecord>().get(pG);
85  const CaloGeometry* geo = pG.product();
86 // iSetup.get<HcalDbRecord>().get(conditions);
87 
88  std::vector<DetId> did = geo->getValidDetIds();
89 
90 
91  std::map<HcalDetId,float> corrold;
92  //map<HcalDetId,float> corrnew;
93 
94  int mysubd,depth,ieta,iphi;
95  float coradd,corerr;
96 
97  std::vector<HcalDetId> theVector;
98  for(std::vector<DetId>::iterator i = did.begin(); i != did.end(); i++)
99  {
100  if( (*i).det() == DetId::Hcal ) {
101  HcalDetId hid = HcalDetId(*i);
102  theVector.push_back(hid);
103  corrold[hid] = (oldRespCorrs->getValues(*i))->getValue();
104  std::cout<<" Old calibration "<<hid.depth()<<" "<<hid.ieta()<<" "<<hid.iphi()<<std::endl;
105  }
106  }
107 
108  std::cout<<" Get old calibration "<<std::endl;
109 // Read new corrections from file
110 
111  edm::FileInPath f1(file_input);
112  std::string fDataFile = f1.fullPath();
113 
114  std::ifstream in( fDataFile.c_str() );
116 
117  double corrnew_p[5][5][45][75];
118  double corrnew_m[5][5][45][75];
119  std::cout<<" Start to read txt file "<<fDataFile.c_str()<<std::endl;
120  while( std::getline( in, line)){
121 
122 // std::cout<<" Line size "<<line.size()<< " "<<line<< std::endl;
123 
124  if(!line.size() || line[0]=='#') continue;
125  std::istringstream linestream(line);
126 
127  linestream>>mysubd>>depth>>ieta>>iphi>>coradd>>corerr;
128 // DetId mydid(DetId::Hcal,HcalSubdetector(mysubd));
129 // HcalDetId hid(HcalSubdetector(mysubd),ieta,iphi,depth);
130 // HcalDetId hid(mydid);
131 // std::cout<<" Check mysubd "<<hid.subdet()<<" depth "<<hid.depth()<<" ieta "<<hid.ieta()<<" iphi "<<hid.iphi()<<" "<<hid.rawId()<< std::endl;
132  int ietak = ieta;
133  if(ieta<0) ietak = -1*ieta;
134  if(ieta>0) corrnew_p[mysubd][depth][ietak][iphi] = coradd;
135  if(ieta<0) corrnew_m[mysubd][depth][ietak][iphi] = coradd;
136  std::cout<<" Try to initialize mysubd "<<mysubd<<" depth "<<depth<<" ieta "<<ieta<<" "<<ietak<<" iphi "<<iphi<<" "<<coradd<<
137  std::endl;
138  }
139 
140  HcalRespCorrs* mycorrections = new HcalRespCorrs(oldRespCorrs->topo());
141 
142  for(std::vector<HcalDetId>::iterator it = theVector.begin(); it != theVector.end(); it++)
143  {
144  float cc1 = (*corrold.find(*it)).second;
145  // float cc2 = (*corrnew.find(*it)).second;
146  float cc2 = 0.;
147  int ietak = (*it).ieta();
148 
149  if((*it).ieta()<0) ietak=-1*(*it).ieta();
150 
151  if((*it).ieta()>0) cc2 = corrnew_p[(*it).subdet()][(*it).depth()][ietak][(*it).iphi()];
152  if((*it).ieta()<0) cc2 = corrnew_m[(*it).subdet()][(*it).depth()][ietak][(*it).iphi()];
153 
154  float cc = cc1*cc2;
155  std::cout<<" Multiply "<<(*it).subdet()<<" "<<(*it).depth()<<" "<<(*it).ieta()<<" "<<ietak<<" "<<(*it).iphi()<<" "<<(*it).rawId()<<" "<<cc1<<" "<<cc2<<std::endl;
156 
157  // now make the basic object for one cell with HcalDetId myDetId containing the value myValue
158  HcalRespCorr item ((*it).rawId(), cc);
159  mycorrections->addValues(item);
160  }
161 
162  HcalRespCorrs mycc = *mycorrections;
163  HcalDbASCIIIO::dumpObject (*myout_hcal, mycc);
164 
165 }
166 }
167 //define this as a plug-in
168 //DEFINE_FWK_MODULE(HcalConstantsASCIIWriter)
169 
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
const Item * getValues(DetId fId, bool throwOnFail=true) const
void beginJob()
Definition: Breakpoints.cc:15
U second(std::pair< T, U > const &p)
int depth() const
get the tower depth
Definition: HcalDetId.h:55
int iEvent
Definition: GenABIO.cc:230
int ieta() const
get the cell ieta
Definition: HcalDetId.h:51
int iphi() const
get the cell iphi
Definition: HcalDetId.h:53
const T & get() const
Definition: EventSetup.h:56
T const * product() const
Definition: ESHandle.h:86
std::vector< DetId > getValidDetIds() const
Get the list of all valid detector ids.
Definition: CaloGeometry.cc:90
bool dumpObject(std::ostream &fOutput, const HcalPedestals &fObject)
const JetExtendedData & getValue(const Container &, const reco::JetBaseRef &)
get value for the association. Throw exception if no association found
tuple cout
Definition: gather_cfg.py:121
std::string fullPath() const
Definition: FileInPath.cc:165
bool addValues(const Item &myItem)
const HcalTopology * topo() const