//------------------------------------------------------------------------// // getgainTripT.cxx by R. Sacco // // Improvements by I. Danko // //------------------------------------------------------------------------// #include #include #include //#include #include #include #include #include "TGraph.h" #include "TMultiGraph.h" #include "TROOT.h" #include "TStyle.h" #include "TH1F.h" #include "TH2F.h" #include "TCanvas.h" #include "TChain.h" #include "TFile.h" //#include int main(int argc, char* argv[]){ gROOT->SetStyle("Plain"); gStyle->SetHistLineWidth(1); gStyle->SetPalette(1); gStyle->SetPaperSize(20,26); // Turn off some borders gStyle->SetCanvasBorderMode(0); gStyle->SetFrameBorderMode(0); gStyle->SetPadBorderMode(0); gStyle->SetDrawBorder(0); gStyle->SetCanvasBorderSize(0); gStyle->SetFrameBorderSize(0); gStyle->SetPadBorderSize(1); gStyle->SetTitleBorderSize(0); // Say it in black and white! gStyle->SetAxisColor(1, "xyz"); gStyle->SetCanvasColor(0); gStyle->SetFrameFillColor(0); gStyle->SetFrameLineColor(1); gStyle->SetHistFillColor(0); gStyle->SetHistLineColor(1); //gStyle->SetPadColor(1); gStyle->SetPadColor(kWhite); gStyle->SetStatColor(0); gStyle->SetStatTextColor(1); gStyle->SetTitleColor(1); gStyle->SetTitleTextColor(1); gStyle->SetLabelColor(1,"xyz"); gStyle->SetLabelOffset(0.02); // Show functions in red... gStyle->SetFuncColor(2); std::cout << "Number of command line arguments: " << argc - 1 << std::endl; //for( int i = 1; i < argc; i++ ) // std::cout << " argc[" << i << "] = " << argv[i] << std::endl; if( argc - 1 < 2 ){ std::cout << "Not enough arguments! " << std::endl; std::cout << "Usage: CalibOffline_getgainTripT.exe []" << std::endl; std::cout << " : ECAL/P0D/SMRD" << std::endl; std::cout << " : path to the list of constants files" << std::endl; std::cout << " : path to the reference constants file" << std::endl; std::cout << " (optional, if not specified the first file from the list will be used)" << std::endl; return -1; } // 1st argument: detector name std::string det = (std::string)argv[1]; std::cout << "Detector: " << det << std::endl; // 2nd argument: constants file list std::string ListName = (std::string)argv[2]; std::cout << "Input list: " << ListName << std::endl; // Name of the reference constants file: // 3rd argument if specified at the command line, // otherwise it is the the first file from the file list (see later) char RefName[1000]; if( argc > 3 ) strcpy(RefName, argv[3]); unsigned long firstCh = 0; unsigned short int nRMM = 0; unsigned int NunusedCh = 0; float MinGain = 0.; float MaxGain = 0.; if( det == "P0D" ){ //firstCh = ITFBChannelId(1,0,0,0,0).AsUInt(); firstCh = 2181041152u; nRMM = 6; NunusedCh = 736; MinGain = 700.; MaxGain = 1600.; } else if( det == "ECAL" ){ //firstCh = ITFBChannelId(4,0,0,0,0).AsUInt(); firstCh = 2281704448u; nRMM = 12; NunusedCh = 1107; MinGain = 1000.; MaxGain = 1700.; } else if( det == "SMRD" ){ //firstCh = ITFBChannelId(7,0,0,0,0).AsUInt(); firstCh = 2382367744u; nRMM = 4; NunusedCh = 4654; MinGain = 700.; MaxGain = 1700.; } else { std::cout << "Unknown detector: " << det << std::endl; return -1; } //Run over all files in list to get time steps std::ifstream ListFile( ListName.c_str() ); int countfiles=-1;// count the number of constants files int minTime=2000000000; int maxTime=0; while(true){ countfiles++; //Get a filename from the list; break if we are at EOF char buf[1000]; ListFile>>buf; if(!ListFile.good()) break; // If the reference file is not specified at the command line // use the first file in the list as the reference if( argc == 3 && countfiles == 0 ) strcpy(RefName, buf); std::ifstream dataFile(buf); //Get to validity range string dataFile.getline(buf,1000,' '); dataFile.getline(buf,1000,' '); dataFile.getline(buf,1000,'\''); dataFile.getline(buf,1000,'\''); //Convert to a unixtime (yuck) struct tm timeinfo; char tmBuf[3]="**"; tmBuf[0]=buf[2];tmBuf[1]=buf[3]; timeinfo.tm_year=atoi(tmBuf)+100; tmBuf[0]=buf[5];tmBuf[1]=buf[6]; timeinfo.tm_mon=atoi(tmBuf)-1; tmBuf[0]=buf[8];tmBuf[1]=buf[9]; timeinfo.tm_mday=atoi(tmBuf); tmBuf[0]=buf[11];tmBuf[1]=buf[12]; timeinfo.tm_hour=atoi(tmBuf); tmBuf[0]=buf[14];tmBuf[1]=buf[15]; timeinfo.tm_min=atoi(tmBuf); tmBuf[0]=buf[17];tmBuf[1]=buf[18]; timeinfo.tm_sec=atoi(tmBuf); time_t unixTime=mktime(&timeinfo); if(unixTime>maxTime)maxTime=unixTime; if(unixTime> unusedCh[ch]; // std::cout << ch << " " << unusedCh[ch] << std::endl; } unusedFile.close(); } else { std::cout << "Cannot open file with unused channel list! " << std::endl; return -1; } //Set histogram x axes to comfortably fit all data //with 1 bin per hour double histTimeMax = maxTime+(double(maxTime-minTime))*0.05; double histTimeMin = minTime-(double(maxTime-minTime))*0.05; int histBins = (int)(histTimeMax-histTimeMin)/3600; //Set vectors for TGraphs int subtimes[countfiles]; int deadchsubrun[countfiles]; int badchsubrun[countfiles]; int overchsubrun[countfiles]; int underchsubrun[countfiles]; //Load reference gains for each channel and store them into an array int ref_gain[nRMM][48][4][16]; char buf[1000]; std::cout << "Reference file: " << RefName << std::endl; std::ifstream refFile( RefName ); refFile.getline(buf,1000); while(refFile.good()){ refFile.getline(buf,1000); std::istringstream line(buf); char *token; unsigned long int chan = 0; int gain = 0; int count = 0 ; token = strtok( buf," "); while( token != NULL ) { /* While there are tokens in "string" */ if(count==0) { chan=strtoul(token,NULL,0); } else if (count==1) { gain=strtoul(token,NULL,0); } else if (count==2) { // error=strtoul(token,NULL,0); } else { std::cout << "Unrecognized case..." << std::endl; } /* Get next token: */ token = strtok( NULL, "," ); count++; unsigned long int channel; channel = chan - firstCh; int rmm=-99, tfb=-99, tript=-99, chid=-99; rmm = int(channel/524288); tfb = int((channel-rmm*524288)/4096); tript = int((channel-rmm*524288-tfb*4096)/32); chid = (channel-rmm*524288-tfb*4096)-tript*32; ref_gain[rmm][tfb][tript][chid]=gain; } }// end loop over channels in reference file //Output file for all plots TFile fOut(Form("DQGainPlots%s.root",det.c_str()),"RECREATE"); //One bin per hour and per ADC int Ybins = 500; float Ylow = -250; float Yhigh= 250; //int Ydchbins= 30; //float Ydchhigh= 30.; //float Ydchlow= 0.; //int Ybchbins= 80; //float Ybchhigh= 80.; //float Ybchlow= 0.; int Gbins = (int)(MaxGain - MinGain); TH2F* GainAll = new TH2F("GainAll","Gain All",histBins,histTimeMin,histTimeMax,Gbins,MinGain,MaxGain); GainAll->GetXaxis()->SetTimeDisplay(1); GainAll->GetXaxis()->SetTimeFormat("#splitline{%d/%m}{%H:%M}"); GainAll->GetXaxis()->SetTimeOffset(0,"gmt"); TH2F* GainDriftAll = new TH2F("GainDriftAll","Gain Drift All",histBins,histTimeMin,histTimeMax,Ybins,Ylow,Yhigh); GainDriftAll->GetXaxis()->SetTimeDisplay(1); GainDriftAll->GetXaxis()->SetTimeFormat("#splitline{%d/%m}{%H:%M}"); GainDriftAll->GetXaxis()->SetTimeOffset(0,"gmt"); //char hiNamedch[20] = "DeadChannelHistory"; //char hiNamebch[20] = "BadChannelHistory"; //TH2F* dptDeadchPlot = new TH2F(hiNamedch,hiNamedch,histBins,histTimeMin,histTimeMax,Ydchbins,Ydchlow,Ydchhigh); //TH2F* dptBadchPlot = new TH2F(hiNamebch,hiNamebch,histBins,histTimeMin,histTimeMax,Ybchbins,Ybchlow,Ybchhigh); TH2F* GainRMM[nRMM]; TH2F* GainDriftRMM[nRMM]; for( unsigned short int r = 0; r < nRMM; r++){ std::string driftName = Form("GainDriftRMM%d",r); GainDriftRMM[r] = new TH2F(driftName.c_str(),driftName.c_str(),histBins,histTimeMin,histTimeMax,Ybins,Ylow,Yhigh); GainDriftRMM[r]->GetXaxis()->SetTimeDisplay(1); GainDriftRMM[r]->GetXaxis()->SetTimeFormat("#splitline{%d/%m}{%H:%M}"); GainDriftRMM[r]->GetXaxis()->SetTimeOffset(0,"gmt"); std::string name = Form("GainRMM%d",r); GainRMM[r] = new TH2F(name.c_str(),name.c_str(),histBins,histTimeMin,histTimeMax,2500,0.,2500.); GainRMM[r]->GetXaxis()->SetTimeDisplay(1); GainRMM[r]->GetXaxis()->SetTimeFormat("#splitline{%d/%m}{%H:%M}"); GainRMM[r]->GetXaxis()->SetTimeOffset(0,"gmt"); } std::ifstream newListFile( ListName.c_str() ); int counter=0;// count the number of files in list while(true){ //Get a filename from the list; break if we are at EOF char buf[1000]; newListFile>>buf; if(!newListFile.good()) break; std::ifstream dataFile(buf); dataFile.getline(buf,1000,' '); dataFile.getline(buf,1000,' '); dataFile.getline(buf,1000,'\''); dataFile.getline(buf,1000,'\''); //Convert to a unixtime (yuck) struct tm timeinfo; char tmBuf[3]="**"; tmBuf[0]=buf[2];tmBuf[1]=buf[3]; timeinfo.tm_year=atoi(tmBuf)+100; tmBuf[0]=buf[5];tmBuf[1]=buf[6]; timeinfo.tm_mon=atoi(tmBuf)-1; tmBuf[0]=buf[8];tmBuf[1]=buf[9]; timeinfo.tm_mday=atoi(tmBuf); tmBuf[0]=buf[11];tmBuf[1]=buf[12]; timeinfo.tm_hour=atoi(tmBuf); tmBuf[0]=buf[14];tmBuf[1]=buf[15]; timeinfo.tm_min=atoi(tmBuf); tmBuf[0]=buf[17];tmBuf[1]=buf[18]; timeinfo.tm_sec=atoi(tmBuf); time_t unixTime=mktime(&timeinfo); dataFile.getline(buf,1000); int countchannel=0; int deadchantot=0; int newdeadchantot=0; int badchantot=0; int overflow = 0; int underflow = 0; while(dataFile.good()){ dataFile.getline(buf,1000); //****Break at end of file other wise it loops over one more time if( !dataFile.good() ) break; std::istringstream line(buf); char *token; unsigned long int chan = 0; int gain = 0; int error = 0; int count = 0; token = strtok( buf,","); while( token != NULL ){ /* While there are tokens in "string" */ if(count==0) { chan=strtoul(token,NULL,0); } else if (count==1) { gain=strtoul(token,NULL,0); } else if (count==2) { error=strtoul(token,NULL,0); } else { std::cout << "Unrecognized case..." << std::endl; } /* Get next token: */ token = strtok( NULL, "," ); count++; } //*****Skip empty line at the end when chan remains 0! if( chan == 0 ) continue; //*****Skip channel if it is disconnected/unused bool unused_channel = false; for(unsigned int ch = 0; ch < NunusedCh; ch++){ if( chan == unusedCh[ch] ){ unused_channel = true; break; } } if( unused_channel ) continue; unsigned long int channel = chan - firstCh; int rmm=-99, tfb=-99, tript=-99, chid=-99; rmm = int(channel/524288); tfb = int((channel-rmm*524288)/4096); tript = int((channel-rmm*524288-tfb*4096)/32); chid = (channel-rmm*524288-tfb*4096)-tript*32; if ( gain > 0 && error < 0 ) { std::cout << "Timestamp: " << unixTime << " -- DODGY CHANNEL! rmm = " << rmm << " tfb = " << tfb << " tript = " << tript << " channel = " << chid << " gain = " << gain << " error = " << error <= MaxGain ){ std::cout << "Timestamp: " << unixTime << " -- HIGH GAIN CHANNEL! rmm = " << rmm << " tfb = " << tfb << " tript = " << tript << " channel = " << chid << " gain = " << gain << " error = " << error <SetName("DeadChGraph"); DeadChGraph->SetTitle("Dead Channels"); DeadChGraph->SetLineColor(2); DeadChGraph->Write(); TGraph* BadChGraph = new TGraph(countfiles,subtimes,badchsubrun); BadChGraph->SetName("BadChGraph"); BadChGraph->SetTitle("Bad Channels"); BadChGraph->SetLineColor(4); BadChGraph->Write(); TGraph* OverChGraph = new TGraph(countfiles,subtimes,overchsubrun); OverChGraph->SetName("OverChGraph"); OverChGraph->SetTitle("Overflow Channels"); OverChGraph->SetLineColor(2); OverChGraph->Write(); TGraph* UnderChGraph = new TGraph(countfiles,subtimes,underchsubrun); UnderChGraph->SetName("UnderChGraph"); UnderChGraph->SetTitle("Underflow Channels"); UnderChGraph->SetLineColor(4); UnderChGraph->Write(); fOut.Write(); //Plot and save histograms in ps file std::string psfile = Form("DQGainPlots%s.ps",det.c_str()); std::string psfilefirst = psfile + "("; std::string psfilelast = psfile + ")"; gStyle->SetOptStat(0); gStyle->SetPalette(1,0); // Drift for all RMMs combined TCanvas *c0 = new TCanvas("c0","",50,50,700,1000); c0->Divide(1,2); c0->cd(1); gPad->SetLogz(); GainAll->SetMinimum(1); GainAll->Draw("colz"); c0->cd(2); gPad->SetLogz(); GainDriftAll->SetMinimum(1); GainDriftAll->Draw("colz"); c0->Print(psfilefirst.c_str(),"Portrait"); // Dead/bad and over/underflow channel counts TCanvas *c0b = new TCanvas("c0b","",150,50,700,1000); c0b->Divide(1,2); c0b->cd(1); TMultiGraph *mg0 = new TMultiGraph(); mg0->SetTitle("Num of dead (red) and bad (blue) channels"); mg0->Add(DeadChGraph,"lp"); mg0->Add(BadChGraph,"lp"); mg0->Draw("a"); mg0->GetXaxis()->SetTimeDisplay(1); mg0->GetXaxis()->SetTimeFormat("#splitline{%d/%m}{%H:%M}"); mg0->GetXaxis()->SetTimeOffset(0,"gmt"); c0b->cd(2); TMultiGraph *mg1 = new TMultiGraph(); mg1->SetTitle("Num of overflow (red) and underflow (blue) channels"); mg1->Add(OverChGraph,"lp"); mg1->Add(UnderChGraph,"lp"); mg1->Draw("a"); mg1->GetXaxis()->SetTimeDisplay(1); mg1->GetXaxis()->SetTimeFormat("#splitline{%d/%m}{%H:%M}"); mg1->GetXaxis()->SetTimeOffset(0,"gmt"); c0b->Print(psfile.c_str()); // Drift for individual RMMs: two of them plotted per canvas TCanvas *crmm[nRMM/2]; for( unsigned short int r = 0; r < nRMM; r++){ if( r%2 == 0 ){ std::string cname = Form("crmm%i",r/2); crmm[r/2] = new TCanvas(cname.c_str(),"",150,50,700,1000); crmm[r/2]->Divide(1,2); crmm[r/2]->cd(1); gPad->SetLogz(); GainDriftRMM[r]->SetMinimum(1); GainDriftRMM[r]->Draw("colz"); } else { crmm[r/2]->cd(2); gPad->SetLogz(); GainDriftRMM[r]->SetMinimum(1); GainDriftRMM[r]->Draw("colz"); if( r < nRMM-1 ){ crmm[r/2]->Print(psfile.c_str()); } else {// last canvas crmm[r/2]->Print(psfilelast.c_str()); } } } return(0); }