Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:59:38

0001 #include <iostream>
0002 #include <vector>
0003 #include "TKey.h"
0004 
0005 Double_t mycurvefunc(Double_t *x, Double_t *par){
0006   return par[0]+ x[0]*par[1];  
0007 }
0008 void make_gaincurves(){
0009   
0010   TFile *file = new TFile("Pixel_DQM_Calibration_smallGain.root");
0011   file->cd();
0012   // make a loop over all plots
0013   TList *list = file->GetListOfKeys();
0014   gStyle->SetOptStat(0);
0015   Int_t nkeys = file->GetNkeys();
0016   TDirectory *dir = file->GetDirectory("DQMData");
0017   if(dir==0)
0018     return;
0019 
0020 
0021   TLatex CMSprelim(0.3,0.3,"CMS Preliminary");
0022   CMSprelim.SetTextColor(2);
0023   CMSprelim.SetTextSize(CMSprelim.GetTextSize()*1.2);
0024   
0025   TString comparestring = "Module";
0026   TString curvestring = "GainCurve";
0027   std::vector<TString> keylist;
0028   std::vector<TString> hist1list;
0029   std::vector<TString> hist2list;
0030   std::vector<TString> dirlist;
0031   std::vector<TString> notdonelist;
0032   std::vector<int> nsubdirs;
0033   TDirectory *dirsav = dir;
0034   list = dir->GetListOfKeys();
0035   int ikey=0;
0036   int localkey=0;
0037   int ntimes=0;
0038 
0039   TCanvas *curvecanvas = new TCanvas();
0040   for(ikey=0;ikey<list->GetEntries();  ikey++){
0041     TKey *thekey = (TKey*)list->At(ikey);
0042     if(thekey==0)
0043       continue;
0044     TString keyname=thekey->GetName();
0045     keyname.ReplaceAll(" ","");
0046     TString keytype=thekey->GetClassName();
0047     keytype.ReplaceAll(" ","");
0048     if(keyname=="EventInfo")
0049       continue;
0050     //    std::cout <<  keytype << " " << keyname << std::endl;
0051     if(keytype=="TDirectoryFile"){
0052       TString dirname=dir->GetPath();
0053       dirname+="/";
0054       dirname+=keyname;
0055       //      std::cout << dirname << std::endl;
0056       dir=file->GetDirectory(dirname);
0057   
0058       list=dir->GetListOfKeys();
0059       if(dirname.Contains(comparestring))
0060     dirlist.push_back(dirname);
0061       else{
0062     notdonelist.push_back(dirname);
0063     nsubdirs.push_back(-1);
0064       }
0065     }
0066   }
0067   int nempty=0;
0068   while(nempty!=notdonelist.size()){
0069     for(int idir=0; idir<notdonelist.size(); ++idir){
0070       if(nsubdirs[idir]==0)
0071     continue;
0072       //std::cout << "now examining " << notdonelist[idir]<< " " << nsubdirs[idir] <<  std::endl;
0073       dir = file->GetDirectory(notdonelist[idir]); 
0074       //      std::cout << dir->GetName() << std::endl;
0075       list= dir->GetListOfKeys(); 
0076       //      std::cout << list->GetEntries() << std::endl;
0077       int ndirectories=0;
0078       for(ikey=0;ikey<list->GetEntries();  ikey++){
0079     TKey *thekey = (TKey*)list->At(ikey);
0080     if(thekey==0)
0081       continue;
0082     TString keyname=thekey->GetName();
0083     keyname.ReplaceAll(" ","");
0084     TString keytype=thekey->GetClassName();
0085     keytype.ReplaceAll(" ","");
0086     if(keytype=="TDirectoryFile"){
0087       TString dirname=dir->GetPath();
0088       dirname+="/";
0089       dirname+=keyname;
0090       //      std::cout << dirname << std::endl;
0091       ndirectories++;
0092       if(dirname.Contains(comparestring))
0093         dirlist.push_back(dirname);
0094       else{
0095         notdonelist.push_back(dirname);
0096         nsubdirs.push_back(-1);
0097       }
0098     }
0099       }
0100       nsubdirs[idir]=ndirectories;
0101       // std::cout << "now done examining " << notdonelist[idir]<< " " << nsubdirs[idir] <<  std::endl;
0102     }
0103     // count number of done dirs;
0104     nempty=0;
0105     for(int i=0; i<nsubdirs.size(); i++){
0106       if(nsubdirs[i]!=-1)
0107     nempty++;
0108     }
0109   }
0110   gStyle->SetOptStat(0);
0111   int ncurves=0;
0112   for(int i=0; i<dirlist.size() ; ++i){
0113     //    std::cout << "good dir "  << dirlist[i] << std::endl;
0114     // now count histograms:
0115     dir = file->GetDirectory(dirlist[i]); 
0116     list= dir->GetListOfKeys(); 
0117     //      std::cout << list->GetEntries() << std::endl;
0118     for(ikey=0;ikey<list->GetEntries();  ikey++){
0119       //      std::cout << ikey << std::endl;
0120       TKey *thekey = (TKey*)list->At(ikey);
0121       if(thekey==0)
0122     continue;
0123       TString keyname=thekey->GetName();
0124       keyname.ReplaceAll(" ","");
0125       TString keytype=thekey->GetClassName();
0126       keytype.ReplaceAll(" ","");
0127       std::cout << ikey << " " <<  keyname << std::endl;
0128 
0129       if(keytype=="TH1F"){
0130     std::cout << keyname << std::endl;
0131     dir=file->GetDirectory(dirlist[i]);
0132     TH1F* temp = (TH1F*) dir->Get(keyname);
0133     if(keyname.Contains(curvestring)){
0134       ncurves++;
0135       std::cout << keyname << std::endl;
0136       curvecanvas->cd();
0137       curvecanvas->Clear();
0138       dir=file->GetDirectory(dirlist[i]);
0139       dir->cd();
0140       gStyle->SetOptStat(0);
0141       gStyle->SetOptFit(0);
0142       temp->SetMarkerSize(2.0); 
0143       temp->GetXaxis()->SetTitle("VCAL");
0144       temp->GetYaxis()->SetTitle("Average ADC response");
0145       temp->DrawClone(); 
0146       float gainandped[2];
0147       getgainresult(keyname,file, dirlist[i],gainandped);
0148       std::cout << gainandped[0] << " " << gainandped[1] << std::endl;
0149       if(gainandped[0]>0.){
0150         TString funcname = keyname;
0151         funcname+="func";
0152         TF1 *func = new TF1(funcname,mycurvefunc,0,256,2);
0153         func->SetLineWidth(3);
0154         func->SetParameter(0,gainandped[1]);
0155         func->SetParameter(1,1./gainandped[0]);
0156         func->DrawClone("lsame");
0157         CMSprelim.DrawText(10,10,"CMS Pixel");
0158         TLegend *legend = new TLegend(0.11,0.2,0.7,0.3);
0159         legend->SetFillStyle(0);
0160         legend->SetBorderSize(0);
0161         TString fitstring = " y = #frac{x}{";
0162         fitstring+= gainandped[0];
0163         fitstring+="} + " ;
0164         fitstring+= gainandped[1];
0165         
0166         legend->AddEntry(func,fitstring,"l");
0167         legend->DrawClone("same");
0168       }
0169       curvecanvas->Update();
0170       curvecanvas->Print(keyname+".jpg");
0171       curvecanvas->Print(keyname+".eps");
0172     }
0173       
0174       }
0175     }
0176   }
0177   //  for(int i=0; i<notdonelist.size();++i){
0178   //    std::cout << notdonelist[i] << std::endl;
0179   //  }
0180 
0181   
0182 }
0183 
0184 
0185 float getgainresult(TString keyname, TFile *file, TString dirname, float *gainandped){
0186 
0187   //  std::cout << "examining histo " << keyname << std::endl;
0188   std::string strversion = keyname;
0189   std::string cutstring[20];
0190   int foundunderscore=0;
0191   for(int i=0; i<20 && foundunderscore<strversion.size(); i++){
0192     int found=strversion.find("_",foundunderscore);
0193     std::string thesubstr = strversion.substr(foundunderscore,found-foundunderscore);
0194     //std::cout << thesubstr << " " << found << std::endl;
0195     cutstring[i]=thesubstr;
0196     foundunderscore=found+1;
0197   }
0198 
0199   int row = atoi(cutstring[2].c_str());
0200   int col = atoi(cutstring[4].c_str());
0201   int detid = atoi(cutstring[6].c_str());
0202   //  std::cout << row << " " << col << " " << detid << std::endl;
0203   TString gainhisto = dirname;
0204   gainhisto+="/Gain2d_siPixelCalibDigis_";
0205   gainhisto+=detid;
0206   TString pedhisto =  dirname;
0207   pedhisto+="/Pedestal2d_siPixelCalibDigis_";
0208   pedhisto+=detid;
0209   //  std::cout << gainhisto << " " << pedhisto << std::endl;
0210   file->cd();
0211   TH2F *gain2d = file->Get(gainhisto);
0212   float gainval=0;
0213   float pedval=0;
0214   if(gain2d!=0)
0215     gainval = gain2d->GetBinContent(row,col);
0216   else return 0;
0217   TH2F *ped2d = file->Get(pedhisto);
0218   if(ped2d)
0219     pedval=ped2d->GetBinContent(row,col);
0220   else return 0;
0221   //std::cout << ped2d->GetNbinsX() << " " << ped2d->GetNbinsY() << std::endl;
0222 
0223   //std::cout << pedhisto << " " << gainhisto << " " << col+1 << " " << row+1 << " " <<  ped2d->GetBinContent(col+1,row+1) << " " << gain2d->GetBinContent(col+1,row+1) <<  std::endl;
0224   gainval = gain2d->GetBinContent(col+1,row+1);
0225   pedval = ped2d->GetBinContent(col+1,row+1);
0226   //  std::cout << gainhisto << " " << row << " " << col << " " << gainval << " " << pedval << std::endl;
0227   gainandped[0]=gainval;
0228   gainandped[1]=pedval;
0229   return 1;
0230 }
0231 double getpedresult(TString keyname){
0232   std::cout << "examining histo " << keyname << std::endl;
0233   float result=0;
0234   
0235   
0236   return result;
0237 
0238 }
0239 
0240 int getrow(std::string keyname){
0241   
0242   return 0;
0243 }
0244 
0245 int getcol(std::string keyname){
0246   return 0;
0247 }