CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/RecoLocalMuon/CSCValidation/src/CSCValHists.cc

Go to the documentation of this file.
00001 #include "RecoLocalMuon/CSCValidation/src/CSCValHists.h"
00002 #include <algorithm>
00003 
00004 using namespace std;
00005 
00006 
00007   CSCValHists::CSCValHists(){
00008 
00009     std::cout << "Initializing Histogram Manager..." << std::endl;
00010 
00011   }  
00012 
00013 
00014   CSCValHists::~CSCValHists(){
00015 
00016   }
00017 
00018 
00019   void CSCValHists::writeHists(TFile* theFile){
00020 
00021     std::vector<std::string> theFolders;
00022     std::vector<std::string>::iterator fit;
00023     theFile->cd();
00024 
00025     std::map<std::string,std::pair<TH1*,string> >::const_iterator mapit;
00026     for (mapit = theMap.begin(); mapit != theMap.end(); mapit++){
00027       std::string folder = (*mapit).second.second.c_str();
00028       fit = find(theFolders.begin(), theFolders.end(), folder);
00029       if (fit == theFolders.end()){
00030         theFolders.push_back(folder);
00031         theFile->mkdir(folder.c_str());
00032       }
00033       theFile->cd((*mapit).second.second.c_str());
00034       (*mapit).second.first->Write();
00035       theFile->cd();
00036     }
00037 
00038   }
00039 
00040   
00041   void CSCValHists::writeTrees(TFile* theFile){
00042 
00043     theFile->cd("recHits");
00044     rHTree->Write();
00045     theFile->cd();
00046 
00047     theFile->cd("Segments");
00048     segTree->Write();
00049     theFile->cd();
00050 
00051 
00052   }
00053 
00054   
00055   void CSCValHists::setupTrees(){
00056 
00057     // Create the root tree to hold position info
00058     rHTree  = new TTree("rHPositions","Local and Global reconstructed positions for recHits");
00059     segTree = new TTree("segPositions","Local and Global reconstructed positions for segments");
00060 
00061     // Create a branch on the tree
00062     rHTree->Branch("rHpos",&rHpos,"endcap/I:station/I:ring/I:chamber/I:layer/I:localx/F:localy/F:globalx/F:globaly/F");
00063     segTree->Branch("segpos",&segpos,"endcap/I:station/I:ring/I:chamber/I:layer/I:localx/F:localy/F:globalx/F:globaly/F");
00064 
00065   }
00066 
00067   
00068   void CSCValHists::fillRechitTree(float x, float y, float gx, float gy, int en, int st, int ri, int ch, int la){
00069 
00070     // Fill the rechit position branch
00071     rHpos.localx  = x;
00072     rHpos.localy  = y;
00073     rHpos.globalx = gx;
00074     rHpos.globaly = gy;
00075     rHpos.endcap  = en;
00076     rHpos.ring    = ri;
00077     rHpos.station = st;
00078     rHpos.chamber = ch;
00079     rHpos.layer   = la;
00080     rHTree->Fill();
00081 
00082   }
00083   
00084   void CSCValHists::fillSegmentTree(float x, float y, float gx, float gy, int en, int st, int ri, int ch){
00085 
00086     // Fill the segment position branch
00087     segpos.localx  = x;
00088     segpos.localy  = y;
00089     segpos.globalx = gx;
00090     segpos.globaly = gy;
00091     segpos.endcap  = en;
00092     segpos.ring    = ri;
00093     segpos.station = st;
00094     segpos.chamber = ch;
00095     segpos.layer   = 0;
00096     segTree->Fill();
00097 
00098   }
00099 
00100   void CSCValHists::insertPlot(TH1* thePlot, std::string name, std::string folder){
00101 
00102     theMap[name] = std::pair<TH1*,string>(thePlot, folder);
00103 
00104   }
00105   
00106 
00107   void CSCValHists::fillCalibHist(float x, std::string name, std::string title, int bins, float xmin, float xmax,
00108                                   int bin, std::string folder){
00109 
00110     std::map<std::string,std::pair<TH1*,string> >::iterator it;
00111     it = theMap.find(name);
00112     if (it == theMap.end()){
00113       theMap[name] = std::pair<TH1*,string>(new TH1I(name.c_str(),title.c_str(),bins,xmin,xmax), folder);
00114     }
00115 
00116     theMap[name].first->SetBinContent(bin,x);
00117 
00118   }
00119 
00120 
00121   void CSCValHists::fill1DHist(float x, std::string name, std::string title,
00122                                int bins, float xmin, float xmax, std::string folder){
00123 
00124     std::map<std::string,std::pair<TH1*,string> >::iterator it;
00125     it = theMap.find(name);
00126     if (it == theMap.end()){
00127       theMap[name] = std::pair<TH1*,string>(new TH1I(name.c_str(),title.c_str(),bins,xmin,xmax), folder);
00128     }
00129 
00130 
00131     theMap[name].first->Fill(x);
00132 
00133   }
00134 
00135 
00136   void CSCValHists::fill2DHist(float x, float y, std::string name, std::string title,
00137                                int binsx, float xmin, float xmax,
00138                                int binsy, float ymin, float ymax, std::string folder){
00139 
00140     std::map<std::string,std::pair<TH1*,string> >::iterator it;
00141     it = theMap.find(name);
00142     if (it == theMap.end()){
00143       theMap[name] = std::pair<TH1*,string>(new TH2F(name.c_str(),title.c_str(),binsx,xmin,xmax,binsy,ymin,ymax),folder);
00144     }
00145 
00146     theMap[name].first->Fill(x,y);
00147 
00148   }
00149 
00150 
00151   void CSCValHists::fill1DHistByType(float x, std::string name, std::string title, CSCDetId id,
00152                                      int bins, float xmin, float xmax, std::string folder){
00153 
00154     std::string endcap;
00155     if (id.endcap() == 1) endcap = "+";
00156     if (id.endcap() == 2) endcap = "-";
00157 
00158     std::map<std::string,std::pair<TH1*,string> >::iterator it;
00159     ostringstream oss1;
00160     ostringstream oss2;
00161     oss1 << name << endcap << id.station() << id.ring();
00162     oss2 << title << "  (ME " << endcap << id.station() << "/" << id.ring() << ")";
00163     name = oss1.str();
00164     title = oss2.str();
00165     it = theMap.find(name);
00166     if (it == theMap.end()){
00167       theMap[name] = std::pair<TH1*,string>(new TH1F(name.c_str(),title.c_str(),bins,xmin,xmax), folder);
00168     }
00169 
00170     theMap[name].first->Fill(x);
00171 
00172   }
00173 
00174   void CSCValHists::fill2DHistByType(float x, float y, std::string name, std::string title, CSCDetId id,
00175                                    int binsx, float xmin, float xmax,
00176                                    int binsy, float ymin, float ymax, std::string folder){
00177 
00178     std::string endcap;
00179     if (id.endcap() == 1) endcap = "+";
00180     if (id.endcap() == 2) endcap = "-";
00181 
00182     std::map<std::string,std::pair<TH1*,string> >::iterator it;
00183     ostringstream oss1;
00184     ostringstream oss2;
00185     oss1 << name << endcap << id.station() << id.ring();
00186     oss2 << title << "  (ME " << endcap << id.station() << "/" << id.ring() << ")";
00187     name = oss1.str();
00188     title = oss2.str();
00189     it = theMap.find(name);
00190     if (it == theMap.end()){
00191       theMap[name] = std::pair<TH1*,string>(new TH2F(name.c_str(),title.c_str(),binsx,xmin,xmax,binsy,ymin,ymax), folder);
00192     }
00193 
00194     theMap[name].first->Fill(x,y);
00195 
00196   }
00197 
00198 
00199   void CSCValHists::fill1DHistByCrate(float x, string name, string title, CSCDetId id,
00200                                      int bins, float xmin, float xmax, string folder){
00201 
00202     int crate = crate_lookup(id); 
00203   
00204     map<string,pair<TH1*,string> >::iterator it;
00205     ostringstream oss1;
00206     ostringstream oss2;
00207     oss1 << name << "_crate_" << crate;
00208     oss2 << title << "  (crate " << crate << ")";
00209     name = oss1.str();
00210     title = oss2.str();
00211     it = theMap.find(name);
00212     if (it == theMap.end()){
00213       theMap[name] = pair<TH1*,string>(new TH1F(name.c_str(),title.c_str(),bins,xmin,xmax), folder);
00214     }
00215   
00216     theMap[name].first->Fill(x);
00217   
00218   }
00219 
00220   void CSCValHists::fill2DHistByCrate(float x, float y, string name, string title, CSCDetId id,
00221                                       int binsx, float xmin, float xmax,
00222                                       int binsy, float ymin, float ymax, string folder){
00223     
00224     int crate = crate_lookup(id); 
00225      
00226     map<string,pair<TH1*,string> >::iterator it;
00227     ostringstream oss1;
00228     ostringstream oss2;
00229     oss1 << name << "_crate_" << crate;
00230     oss2 << title << "  (crate " << crate << ")";   
00231     name = oss1.str();
00232     title = oss2.str();
00233     it = theMap.find(name);
00234     if (it == theMap.end()){
00235       theMap[name] = pair<TH1*,string>(new TH2F(name.c_str(),title.c_str(),binsx,xmin,xmax,binsy,ymin,ymax), folder);
00236     }
00237     
00238     theMap[name].first->Fill(x,y);
00239     
00240   }
00241 
00242 
00243 
00244   void CSCValHists::fill1DHistByStation(float x, string name, string title, CSCDetId id,
00245                                         int bins, float xmin, float xmax, string folder){
00246 
00247     string endcap;
00248     if (id.endcap() == 1) endcap = "+";
00249     if (id.endcap() == 2) endcap = "-";
00250 
00251     map<string,pair<TH1*,string> >::iterator it;
00252     ostringstream oss1;
00253     ostringstream oss2;
00254     oss1 << name << endcap << id.station();
00255     oss2 << title << "  (Station " << endcap << id.station() << ")";
00256     name = oss1.str();
00257     title = oss2.str();
00258     it = theMap.find(name);
00259     if (it == theMap.end()){
00260       theMap[name] = pair<TH1*,string>(new TH1F(name.c_str(),title.c_str(),bins,xmin,xmax), folder);
00261     }
00262 
00263     theMap[name].first->Fill(x);
00264 
00265   }
00266 
00267 
00268   void CSCValHists::fill2DHistByStation(float x, float y, string name, string title, CSCDetId id,
00269                                         int binsx, float xmin, float xmax,
00270                                         int binsy, float ymin, float ymax, std::string folder){
00271 
00272     std::string endcap;
00273     if (id.endcap() == 1) endcap = "+";
00274     if (id.endcap() == 2) endcap = "-";
00275 
00276     std::map<std::string,std::pair<TH1*,string> >::iterator it;
00277     ostringstream oss1;
00278     ostringstream oss2;
00279     oss1 << name << endcap << id.station();
00280     oss2 << title << "  (Station " << endcap << id.station() << ")";
00281     name = oss1.str();
00282     title = oss2.str();
00283     it = theMap.find(name);
00284     if (it == theMap.end()){
00285       theMap[name] = std::pair<TH1*,string>(new TH2F(name.c_str(),title.c_str(),binsx,xmin,xmax,binsy,ymin,ymax), folder);
00286     }
00287 
00288     theMap[name].first->Fill(x,y);
00289 
00290   }
00291 
00292 
00293   void CSCValHists::fill1DHistByChamber(float x, std::string name, std::string title, CSCDetId id,
00294                                        int bins, float xmin, float xmax, std::string folder){
00295 
00296     std::string endcap;
00297     if (id.endcap() == 1) endcap = "+";
00298     if (id.endcap() == 2) endcap = "-";
00299 
00300     std::map<std::string,std::pair<TH1*,string> >::iterator it;
00301     ostringstream oss1;
00302     ostringstream oss2;
00303     oss1 << name << "_" << endcap << id.station() << "_" << id.ring() << "_" << id.chamber();
00304     oss2 << title << "  (ME " << endcap << id.station() << "/" << id.ring() << "/" << id.chamber() << ")";
00305     name = oss1.str();
00306     title = oss2.str();
00307     it = theMap.find(name);
00308     if (it == theMap.end()){
00309       theMap[name] = std::pair<TH1*,string>(new TH1F(name.c_str(),title.c_str(),bins,xmin,xmax),folder);
00310     }
00311 
00312     theMap[name].first->Fill(x);
00313 
00314   }
00315 
00316 
00317   void CSCValHists::fill2DHistByChamber(float x, float y, std::string name, std::string title, CSCDetId id,
00318                                         int binsx, float xmin, float xmax,
00319                                         int binsy, float ymin, float ymax, std::string folder){
00320 
00321     std::string endcap;
00322     if (id.endcap() == 1) endcap = "+";
00323     if (id.endcap() == 2) endcap = "-";
00324 
00325     std::map<std::string,std::pair<TH1*,string> >::iterator it;
00326     ostringstream oss1;
00327     ostringstream oss2;
00328     oss1 << name << "_" << endcap << id.station() << "_" << id.ring() << "_" << id.chamber();
00329     oss2 << title << "  (ME " << endcap << id.station() << "/" << id.ring() << "/" << id.chamber() << ")";
00330     name = oss1.str();
00331     title = oss2.str();
00332     it = theMap.find(name);
00333     if (it == theMap.end()){
00334       theMap[name] = std::pair<TH1*,string>(new TH2F(name.c_str(),title.c_str(),binsx,xmin,xmax,binsy,ymin,ymax),folder);
00335     }
00336 
00337     theMap[name].first->Fill(x,y);
00338 
00339   }
00340 
00341   void CSCValHists::fill2DHistByEvent(int run, int event, float z, string name, string title, CSCDetId id, string folder){
00342 
00343     string endcap;
00344     if (id.endcap() == 1) endcap = "+";
00345     if (id.endcap() == 2) endcap = "-";
00346     
00347     map<string,pair<TH1*,string> >::iterator it;
00348     ostringstream oss1;
00349     ostringstream oss2;
00350     oss1 << name << "_" << run  << "_" << event ;
00351     oss2 << title << "  ( Run: " << run  << " Event: " << event  << " )";
00352     name = oss1.str();
00353     title = oss2.str();
00354     it = theMap.find(name);
00355     if (it == theMap.end()){
00356       theMap[name] = pair<TH1*,string>(new TH2F(name.c_str(),title.c_str(),36,0.5,36.5,18,0.5,18.5),folder);
00357     }
00358 
00359     int x = id.chamber();
00360     int y = id.ring();
00361     if (y==4) y =1; //collapsing ME1/1a into ME1/1
00362     if (id.station() >1)
00363       y = y + 3 + (id.station()-2)*2;
00364     
00365     if (id.endcap()==1)
00366       y = y+9;
00367     else
00368       y = -1*y+10;
00369     
00370     dynamic_cast<TH2F*>(theMap[name].first)->Fill(x,y,z);
00371     
00372   }
00373 
00374   void CSCValHists::fill2DHist(float z, string name, string title, CSCDetId id, string folder){
00375 
00376     map<string,pair<TH1*,string> >::iterator it;
00377     it = theMap.find(name);
00378     if (it == theMap.end()){
00379       theMap[name] = pair<TH1*,string>(new TH2F(name.c_str(),title.c_str(),36,0.5,36.5,18,0.5,18.5),folder);
00380     }
00381 
00382     int x = id.chamber();
00383     int y = id.ring();
00384     if (y==4) y =1; //collapsing ME1/1a into ME1/1
00385     if (id.station() >1)
00386       y = y + 3 + (id.station()-2)*2;
00387 
00388     if (id.endcap()==1)
00389       y = y+9;
00390     else
00391       y = -1*y+10;
00392 
00393     dynamic_cast<TH2F*>(theMap[name].first)->Fill(x,y,z);
00394 
00395   }
00396 
00397   void CSCValHists::fill1DHistByLayer(float x, string name, string title, CSCDetId id,
00398                                       int bins, float xmin, float xmax, string folder){
00399 
00400     std::string endcap;
00401     if (id.endcap() == 1) endcap = "+";
00402     if (id.endcap() == 2) endcap = "-";
00403 
00404     std::map<std::string,std::pair<TH1*,string> >::iterator it;
00405     ostringstream oss1;
00406     ostringstream oss2;
00407     oss1 << name << "_" << endcap << id.station() << "_" << id.ring() << "_" << id.chamber() << "_L" << id.layer();
00408     oss2 << title << "  (ME " << endcap << id.station() << "/" << id.ring() << "/" << id.chamber() << "/L" << id.layer() << ")";
00409     name = oss1.str();
00410     title = oss2.str();
00411     it = theMap.find(name);
00412     if (it == theMap.end()){
00413       theMap[name] = std::pair<TH1*,string>(new TH1F(name.c_str(),title.c_str(),bins,xmin,xmax),folder);
00414     }
00415 
00416     theMap[name].first->Fill(x);
00417 
00418   }
00419 
00420 
00421   void CSCValHists::fill2DHistByLayer(float x, float y, std::string name, std::string title, CSCDetId id,
00422                                       int binsx, float xmin, float xmax,
00423                                       int binsy, float ymin, float ymax, std::string folder){
00424 
00425     std::string endcap;
00426     if (id.endcap() == 1) endcap = "+";
00427     if (id.endcap() == 2) endcap = "-";
00428 
00429     std::map<std::string,std::pair<TH1*,string> >::iterator it;
00430     ostringstream oss1;
00431     ostringstream oss2;
00432     oss1 << name << "_" << endcap << id.station() << "_" << id.ring() << "_" << id.chamber() << "_L" << id.layer();;
00433     oss2 << title << "  (ME " << endcap << id.station() << "/" << id.ring() << "/" << id.chamber() << "/L" << id.layer() << ")";
00434     name = oss1.str();
00435     title = oss2.str();
00436     it = theMap.find(name);
00437     if (it == theMap.end()){
00438       theMap[name] = std::pair<TH1*,string>(new TH2F(name.c_str(),title.c_str(),binsx,xmin,xmax,binsy,ymin,ymax),folder);
00439     }
00440 
00441     theMap[name].first->Fill(x,y);
00442 
00443   }
00444 
00445 
00446   void CSCValHists::fillProfile(float x, float y, std::string name, std::string title,
00447                                 int binsx, float xmin, float xmax,
00448                                 float ymin, float ymax, std::string folder){
00449 
00450     std::map<std::string,std::pair<TH1*,string> >::iterator it;
00451     it = theMap.find(name);
00452     if (it == theMap.end()){
00453       theMap[name] = std::pair<TProfile*,string>(new TProfile(name.c_str(),title.c_str(),binsx,xmin,xmax,ymin,ymax), folder);
00454     }
00455 
00456     theMap[name].first->Fill(x,y);
00457 
00458   }
00459 
00460 
00461   void CSCValHists::fillProfileByType(float x, float y, std::string name, std::string title, CSCDetId id,
00462                                       int binsx, float xmin, float xmax,
00463                                       float ymin, float ymax, std::string folder){
00464 
00465     std::map<std::string,std::pair<TH1*,string> >::iterator it;
00466     std::string endcap;
00467     if (id.endcap() == 1) endcap = "+";
00468     if (id.endcap() == 2) endcap = "-";
00469 
00470     ostringstream oss1;
00471     ostringstream oss2;
00472     oss1 << name << endcap << id.station() << id.ring();
00473     oss2 << title << "  (ME " << endcap << id.station() << "/" << id.ring() << ")";
00474     name = oss1.str();
00475     title = oss2.str();
00476 
00477     it = theMap.find(name);
00478     if (it == theMap.end()){
00479       theMap[name] = std::pair<TProfile*,string>(new TProfile(name.c_str(),title.c_str(),binsx,xmin,xmax,ymin,ymax), folder);
00480     }
00481 
00482     theMap[name].first->Fill(x,y);
00483 
00484   }
00485 
00486 
00487   void CSCValHists::fillProfileByChamber(float x, float y, std::string name, std::string title, CSCDetId id,
00488                                          int binsx, float xmin, float xmax,
00489                                          float ymin, float ymax, std::string folder){
00490 
00491     std::map<std::string,std::pair<TH1*,string> >::iterator it;
00492     std::string endcap;
00493     if (id.endcap() == 1) endcap = "+";
00494     if (id.endcap() == 2) endcap = "-";
00495 
00496     ostringstream oss1;
00497     ostringstream oss2;
00498     oss1 << name << "_" << endcap << id.station() << "_" << id.ring() << "_" << id.chamber();
00499     oss2 << title << "  (ME " << endcap << id.station() << "/" << id.ring() << "/" << id.chamber() << ")";
00500     name = oss1.str();
00501     title = oss2.str();
00502 
00503     it = theMap.find(name);
00504     if (it == theMap.end()){
00505       theMap[name] = std::pair<TProfile*,string>(new TProfile(name.c_str(),title.c_str(),binsx,xmin,xmax,ymin,ymax), folder);
00506     }
00507 
00508     theMap[name].first->Fill(x,y);
00509 
00510   }
00511 
00512 
00513   void CSCValHists::fill2DProfile(float x, float y, float z, std::string name, std::string title,
00514                                   int binsx, float xmin, float xmax,
00515                                   int binsy, float ymin, float ymax,
00516                                   float zmin, float zmax, std::string folder){
00517 
00518     std::map<std::string,std::pair<TH1*,string> >::iterator it;
00519 
00520     it = theMap.find(name);
00521     if (it == theMap.end()){
00522       theMap[name] = std::pair<TProfile2D*,string>(new TProfile2D(name.c_str(),title.c_str(),binsx,xmin,xmax,binsy,ymin,ymax,zmin,zmax), folder);
00523     }
00524 
00525     TProfile2D *tempp = (TProfile2D*)theMap[name].first;
00526     tempp->Fill(x,y,z);
00527 
00528   }
00529 
00530 int CSCValHists::crate_lookup(CSCDetId id){
00531 
00532   int crate = 0;
00533 
00534   if (id.station() == 1){
00535     if (id.chamber() == 36 || id.chamber() == 1  || id.chamber() == 2  ) crate = 1;
00536     if (id.chamber() == 3  || id.chamber() == 4  || id.chamber() == 5  ) crate = 2;
00537     if (id.chamber() == 6  || id.chamber() == 7  || id.chamber() == 8  ) crate = 3;
00538     if (id.chamber() == 9  || id.chamber() == 10 || id.chamber() == 11 ) crate = 4;
00539     if (id.chamber() == 12 || id.chamber() == 13 || id.chamber() == 14 ) crate = 5;
00540     if (id.chamber() == 15 || id.chamber() == 16 || id.chamber() == 17 ) crate = 6;
00541     if (id.chamber() == 18 || id.chamber() == 19 || id.chamber() == 20 ) crate = 7;
00542     if (id.chamber() == 21 || id.chamber() == 22 || id.chamber() == 23 ) crate = 8;
00543     if (id.chamber() == 24 || id.chamber() == 25 || id.chamber() == 26 ) crate = 9;
00544     if (id.chamber() == 27 || id.chamber() == 28 || id.chamber() == 29 ) crate = 10;
00545     if (id.chamber() == 30 || id.chamber() == 31 || id.chamber() == 32 ) crate = 11;
00546     if (id.chamber() == 33 || id.chamber() == 34 || id.chamber() == 35 ) crate = 12;
00547   }
00548   else{
00549     crate = 12 + id.triggerSector() + (id.station()-2)*6;
00550   }
00551 
00552   if (id.endcap() == 2) 
00553     crate = crate+30;
00554 
00555   return crate;
00556 
00557 }