Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
#include <iostream>
#include <vector>
#include "TKey.h"
#include "TMath.h"

Double_t mycurvefunc(Double_t *x, Double_t *par){
  double xval = x[0]-par[0];
  xval /=sqrt(2.)*par[1];
  double val=0.5+0.5*TMath::Erf(xval);
  return val;  
}
void make_scurves(){
  
  TFile *file = new TFile("SCurve_DQM_Calibrationsmall.root");
  file->cd();
  // make a loop over all plots
  TList *list = file->GetListOfKeys();
  gStyle->SetOptStat(0);
  Int_t nkeys = file->GetNkeys();
  TDirectory *dir = file->GetDirectory("DQMData");
  if(dir==0)
    return;


  TLatex CMSprelim(0.3,0.3,"CMS Preliminary");
  CMSprelim.SetTextColor(2);
  CMSprelim.SetTextSize(CMSprelim.GetTextSize()*1.2);
  
  TString comparestring = "Module";
  TString curvestring = "row";
  std::vector<TString> keylist;
  std::vector<TString> hist1list;
  std::vector<TString> hist2list;
  std::vector<TString> dirlist;
  std::vector<TString> notdonelist;
  std::vector<int> nsubdirs;
  TDirectory *dirsav = dir;
  list = dir->GetListOfKeys();
  int ikey=0;
  int localkey=0;
  int ntimes=0;

  TCanvas *curvecanvas = new TCanvas();
  for(ikey=0;ikey<list->GetEntries();  ikey++){
    TKey *thekey = (TKey*)list->At(ikey);
    if(thekey==0)
      continue;
    TString keyname=thekey->GetName();
    //    keyname.ReplaceAll(" ","");
    TString keytype=thekey->GetClassName();
    keytype.ReplaceAll(" ","");
    if(keyname=="EventInfo")
      continue;
    //    std::cout <<  keytype << " " << keyname << std::endl;
    if(keytype=="TDirectoryFile"){
      TString dirname=dir->GetPath();
      dirname+="/";
      dirname+=keyname;
      //      std::cout << dirname << std::endl;
      dir=file->GetDirectory(dirname);
  
      list=dir->GetListOfKeys();
      if(dirname.Contains(comparestring))
	dirlist.push_back(dirname);
      else{
	notdonelist.push_back(dirname);
	nsubdirs.push_back(-1);
      }
    }
  }
  int nempty=0;
  while(nempty!=notdonelist.size()){
    for(int idir=0; idir<notdonelist.size(); ++idir){
      if(nsubdirs[idir]==0)
	continue;
      //      std::cout << "now examining " << notdonelist[idir]<< " " << nsubdirs[idir] <<  std::endl;
      dir = file->GetDirectory(notdonelist[idir]); 
      //      std::cout << dir->GetName() << std::endl;
      list= dir->GetListOfKeys(); 
      //      std::cout << list->GetEntries() << std::endl;
      int ndirectories=0;
      for(ikey=0;ikey<list->GetEntries();  ikey++){
	TKey *thekey = (TKey*)list->At(ikey);
	if(thekey==0)
	  continue;
	TString keyname=thekey->GetName();
	//	keyname.ReplaceAll(" ","");
	TString keytype=thekey->GetClassName();
	keytype.ReplaceAll(" ","");
	if(keytype=="TDirectoryFile"){
	  TString dirname=dir->GetPath();
	  dirname+="/";
	  dirname+=keyname;
	  //	  std::cout << dirname << std::endl;
	  ndirectories++;
	  if(dirname.Contains(comparestring))
	    dirlist.push_back(dirname);
	  else{
	    notdonelist.push_back(dirname);
	    nsubdirs.push_back(-1);
	  }
	}
      }
      nsubdirs[idir]=ndirectories;
      // std::cout << "now done examining " << notdonelist[idir]<< " " << nsubdirs[idir] <<  std::endl;
    }
    // count number of done dirs;
    nempty=0;
    for(int i=0; i<nsubdirs.size(); i++){
      if(nsubdirs[i]!=-1)
	nempty++;
    }
  }
  gStyle->SetOptStat(0);
  int ncurves=0;
  for(int i=0; i<dirlist.size() ; ++i){
    //    std::cout << "good dir "  << dirlist[i] << std::endl;
    // now count histograms:
    dir = file->GetDirectory(dirlist[i]); 
    list= dir->GetListOfKeys(); 
    //      std::cout << list->GetEntries() << std::endl;
    for(ikey=0;ikey<list->GetEntries();  ikey++){
      //      std::cout << ikey << std::endl;
      TKey *thekey = (TKey*)list->At(ikey);
      if(thekey==0)
	continue;
      TString keyname=thekey->GetName();
      keyname.ReplaceAll(" ","");
      TString keytype=thekey->GetClassName();
      keytype.ReplaceAll(" ","");
      //      std::cout << ikey << " " <<  keyname << std::endl;

      if(keytype=="TH1F"){
       	std::cout << keyname << " " << keytype << std::endl;
	if(keyname.Contains(curvestring)){
	  std::cout << keyname << " " << keytype << std::endl;
	  dir=file->GetDirectory(dirlist[i]);
	  TH1F* temp = (TH1F*) dir->Get(keyname);
	  ncurves++;
	  std::cout << keyname << std::endl;
	  curvecanvas->cd();
	  curvecanvas->Clear();
	  dir=file->GetDirectory(dirlist[i]);
	  dir->cd();
	  gStyle->SetOptStat(0);
	  gStyle->SetOptFit(0);
	  temp->SetMarkerSize(2.0); 
	  temp->GetXaxis()->SetTitle("VCAL");
	  temp->GetYaxis()->SetTitle("Efficiency");
	  temp->DrawClone(); 
	  float gainandped[2];
	  getgainresult(keyname,file, dirlist[i],gainandped);
	  std::cout << gainandped[0] << " " << gainandped[1] << std::endl;
	  if(gainandped[0]>0. && gainandped[1]>0.){
	    TString funcname = keyname;
	    funcname+="func";
	    TF1 *func = new TF1(funcname,mycurvefunc,0,256,2);
	    func->SetLineWidth(3);
	    func->SetParameter(0,gainandped[1]);
	    func->SetParameter(1,gainandped[0]);
	    func->DrawClone("lsame");
	    CMSprelim.DrawText(gainandped[1]*1.05,temp->GetMinimum()+0.01,"CMS Pixel");
	    TLegend *legend = new TLegend(0.45,0.2,0.89,0.28);
	    //	    legend->SetFillStyle(0);
	    legend->SetFillColor(0);
	    legend->SetBorderSize(0);
 	    TString fitstring = "y=0.5+0.5 Erf(#frac{x-";
	    fitstring+= gainandped[1];
	    fitstring+="}{" ;
	    fitstring+= gainandped[0];
	    fitstring+=" #sqrt{2}})";
 	    legend->AddEntry(func,fitstring,"l");
 	    legend->DrawClone("same");
	    
	  }
	  curvecanvas->Update();
	  curvecanvas->Print(keyname+".jpg");
	  curvecanvas->Print(keyname+".eps");
	}
      }
    }
  }
  //  for(int i=0; i<notdonelist.size();++i){
  //    std::cout << notdonelist[i] << std::endl;
  //  }

  
}


float getgainresult(TString keyname, TFile *file, TString dirname, float *gainandped){

  //  std::cout << "examining histo " << keyname << std::endl;
  std::string strversion = keyname;
  std::string cutstring[20];
  int foundunderscore=0;
  for(int i=0; i<20 && foundunderscore<strversion.size(); i++){
    int found=strversion.find("_",foundunderscore);
    std::string thesubstr = strversion.substr(foundunderscore,found-foundunderscore);
    //std::cout << thesubstr << " " << found << std::endl;
    cutstring[i]=thesubstr;
    foundunderscore=found+1;
  }

  int row = atoi(cutstring[2].c_str());
  int col = atoi(cutstring[4].c_str());
  int detid = atoi(cutstring[6].c_str());
  //  std::cout << row << " " << col << " " << detid << std::endl;
  TString gainhisto = dirname;
  gainhisto+="/ScurveSigmas_siPixelCalibDigis_";
  gainhisto+=detid;
  TString pedhisto =  dirname;
  pedhisto+="/ScurveThresholds_siPixelCalibDigis_";
  pedhisto+=detid;
  //  std::cout << gainhisto << " " << pedhisto << std::endl;
  file->cd();
  TH2F *gain2d = file->Get(gainhisto);
  float gainval=0;
  float pedval=0;
  if(gain2d!=0)
    gainval = gain2d->GetBinContent(col,row);
  else return 0;
  TH2F *ped2d = file->Get(pedhisto);
  if(ped2d)
    pedval=ped2d->GetBinContent(col,row);
  else return 0;
  //std::cout << ped2d->GetNbinsX() << " " << ped2d->GetNbinsY() << std::endl;

  //std::cout << pedhisto << " " << gainhisto << " " << col+1 << " " << row+1 << " " <<  ped2d->GetBinContent(col+1,row+1) << " " << gain2d->GetBinContent(col+1,row+1) <<  std::endl;
  gainval = gain2d->GetBinContent(col,row);
  pedval = ped2d->GetBinContent(col,row);
  //  std::cout << gainhisto << " " << row << " " << col << " " << gainval << " " << pedval << std::endl;
  gainandped[0]=gainval;
  gainandped[1]=pedval;
  return 1;
}