// Include files #include #include #include #include "time.h" #include "boost/lexical_cast.hpp" #include #include #include "dim/dis.hxx" #include "TSystem.h" #include #include #include #include #include #include #include #include #include #include #include #include "TImage.h" #include "TCanvas.h" #include "TArrayD.h" #include "TColor.h" #include "TAttImage.h" #include "TEnv.h" #include "TASImage.h" #include "TSpectrum.h" #include "TString.h" #include "TF1.h" #include "TF2.h" TArrayD xVal; TArrayD yVal; TArrayD Intensity; TString date; ofstream intensity; ofstream writefile; ofstream LogFile; struct LAMSCommand{ int debugmode; char filename[50]; }; struct ImgAnalysisResults{ double refXCoord; double refYCoord; double refLumi; double reflXCoord; double reflYCoord; double reflLumi; // double radSep; double imgLumi; int quality; }; ImgAnalysisResults Results; ImgAnalysisResults compute(struct LAMSCommand command); int main() { gROOT->Reset(); //create DIM command from PVSS DimCommand PVSSCommand("RichLAMS/ImageFilename", "I:1;C"); // create DIM services, one per camera ImgAnalysisResults result= {0,0,0,0,0,0,0,999}; DimService LAMSResults( "RichLAMS/ImgAnalResults", "D:7;I:1", (void*)&result, sizeof(result) ); // start the DIM server DimServer::start("RichLAMS"); void* voidCommandPointer; LAMSCommand command; // enter infinite loop waiting for commands while(1) { while (PVSSCommand.getNext() ) { // command arrived voidCommandPointer = PVSSCommand.getData(); LAMSCommand* comPointer = static_cast(voidCommandPointer); command = *comPointer; // std::cout << "Filename " << command.filename << std::endl; result = compute(command); // ImgAnalysisResults Results; // update the service realted to this camera if(Results.quality<10)LAMSResults.updateService(); } sleep(1); } return 0; } ImgAnalysisResults compute(struct LAMSCommand command){ TString camName=""; Int_t h = 0; Int_t w = 0 ; Int_t bufferSize= 0; bool toDraw = true; // get all the info from the filename TString filename= command.filename; TString copyfilename= filename; TString rich= filename; TString time= filename; TString exp= filename; TString gain= filename; TString date= filename; TString camera= filename; //get rich number rich.Remove(2,40); TString Rich = rich; Rich.Remove(0,1); //rich1 if(Rich == "1"){ camera.Remove(7,27); camera.Remove(0,2); date.Remove(16,27); date.Remove(0,8); time.Remove(23,27); time.Remove(0,17); // copyfilename.Remove(23,4); } //rich2 if(Rich == "2"){ camera.Remove(7,40); camera.Remove(0,2); date.Remove(16,40); date.Remove(0,8); time.Remove(23,40); time.Remove(0,17); exp.Remove(30,40); exp.Remove(0,24); // gain.Remove(36,40); // if(camera.Contains("5")) gain.Remove(36,40); if(!(camera.Contains("5"))) gain.Remove(36,40); gain.Remove(0,30); // if(camera.Contains("5")) copyfilename.Remove(36,40); // else copyfilename.Remove(36,4); } //get camera value without "cam" TString cam = camera; int quality = -1; cam.Remove(0,3); if(cam.Contains("0")) cam.Remove(0,1); TString camID = cam; TString path="/group/rich/LAMS/RICH"; path.Append(Rich); path.Append("/"); path.Append(date); path.Append("/"); //Output data files TString tobewritten= path; tobewritten.Append("data"); if(command.debugmode == 0){tobewritten.Append("-R");} if(command.debugmode == 1){tobewritten.Append("-DebugR");} tobewritten.Append(Rich); tobewritten.Append("-cam"); tobewritten.Append(cam); tobewritten.Append(".txt"); writefile.open(tobewritten, std::ios::app); writefile<RedirectOutput(LogFileName); //convert // TString toConvert ="convert "; // toConvert.Append(path); // toConvert.Append(filename); // toConvert.Append(" "); // toConvert.Append(path); // toConvert.Append(copyfilename); // toConvert.Append(".bmp"); // std::cout<<"the convert command "<< toConvert<RedirectOutput(ConvertOutput); // Int_t execCode = gSystem->Exec(toConvert); // std::cout<<"gSystem->execute returns "<RedirectOutput(LogFileName); // ifstream test(ConvertOutput, std::ios::binary); // test.seekg(0, std::ios::end); // if (int(test.tellg()) !=0) quality=4; //compose .bmp Filename TString Filename = path; Filename.Append(copyfilename); std::cout<<"************ Start of log from this analysis ************"< "<Divide(4,3); TString oneOrtwo = Rich; TString camStr = cam; Int_t xbin=0; Int_t ybin=0; Int_t zbin=0; Int_t bin=0; TArrayD xVal=TArrayD(10); TArrayD yVal=TArrayD(10); TArrayD Intensity=TArrayD(10); Int_t counter=0; Double_t xbuff=0; Double_t ybuff=0; Double_t cbuff=0; Double_t xvalue=0; Double_t yvalue=0; Int_t point1=0; Int_t point2=0; Double_t buffermax=0; Double_t threshold=0.5; Double_t thresholdII= 0.5; int radius1= 0; int radius2= 0; int deltaRad= 0; Double_t overallcourse=0; Double_t overallfine=0; int a=0; Double_t x1coarse; Double_t x1fine; Double_t y1coarse; Double_t y1fine; Double_t intensityC; Double_t intensityF; Int_t myno=0; Int_t max=0; Int_t n=0; Int_t nII=0; Int_t nIII=0; TString toDel="rm "; TCanvas *c1 = new TCanvas("c1","c1",1200, 700); TH2D* total =new TH2D ("total", "total",w-2, 0.5, w-1.5, h-2 , 0.5, h-1.5); TH2D* ImageHistogram =new TH2D("ImageHistogram", "ImageHistogram", w-2, 1-w/2 , w/2-1, h-2, 1-h/2, h/2-1); TH2D* BufferHistogram=new TH2D("BufferHistogram","BufferHistogram",w-2, 1-w/2 , w/2-1, h-2, 1-h/2, h/2-1); TH2D* ImgHistRebinned = new TH2D ("ImgHistRebinned","ImgHistRebinned", w/10 -2, 10-w/2, w/2-10, h/10 -2 , 10- h/2, h/2 -10); TH2D* BuffHist = new TH2D ("BuffHist", "BuffHist",w/10-2, 10-w/2 , w/2-10, h/10-2, 10-h/2, h/2-10); ifstream *myfile = new ifstream (Filename, std::ios::binary); // if(quality==4) goto BadImage; char buff[bufferSize];//1310720-54]; for(Int_t check=0; check<3; check++) { if(!myfile){ std::cout<<"trying to open "<open(Filename,std::ios::binary);} else check=3; if (!myfile && check==2){ std::cout<<"error opening your file"<seekg(54, std::ios::beg); myfile->read(buff, sizeof(buff)); myfile->close(); } if(toDraw) c1->Divide(4,3); for(Int_t g=0; gSetBinContent(g,0); for(Int_t f=1; f<=bufferSize; f++) // if(buff[f] & 0) { myno=0; // cout<<"f is "< 255)if(command.debugmode == 1){std::cout<<"greater "<Fill(myno); if(myno >=50 && myno<240) histII->Fill(myno); if(myno >=240) histIII->Fill(myno); if((f-1)%3==0) { /*hist->Fill(myno);*/ total->AddBinContent(n, myno); /* TwoD->SetBinContent(n,myno);*/ n++;} if(f%3 ==0) {/* histIII->Fill(myno)*/ ; total->AddBinContent(nIII, myno); /*TwoDIII->SetBinContent(nIII,myno);*/ nIII++;} if((f-2)%3==0){/* histII->Fill(myno);*/ total->AddBinContent(nII, myno); /* TwoDII->SetBinContent(nII,myno);*/ nII++;} if(myno>max) max=myno; } if(toDraw){c1->cd(1);total->DrawCopy("colz");} //Draw original image for(Int_t i=0; iSetBinContent(ImageHistogram->GetBin(x, y) , total->GetBinContent(i)); } if(toDraw){c1->cd(2); ImageHistogram->DrawCopy("colz");} for(Int_t i=0; iSetBinContent(i,0); for(Int_t i=0; iGetBinXYZ(i, xbin, ybin, zbin); bin= ImgHistRebinned->GetBin((int)(xbin/10), (int)(ybin/10)); ImgHistRebinned->AddBinContent(bin, ImageHistogram->GetBinContent(i)); //filling ImgHistRebinned } if(toDraw) { c1->cd(3); ImgHistRebinned->DrawCopy("colz"); } if(oneOrtwo == '1') { radius1=60; radius2=40; deltaRad=150; thresholdII=0.8; overallcourse=(ImgHistRebinned->Integral())/10000000; overallfine= (ImageHistogram->Integral())/10000000; writefile<write } if(oneOrtwo == "2") { if(camStr=="14") { radius1=300; radius2=280;} if(camStr=="15") { radius1=320; radius2=300;} if(camStr=="0") { radius1=270; radius2=250;} if(camStr=="2") { radius1=270; radius2=250;} if(camStr=="9") { radius1=300; radius2=280;} if(camStr=="8") { radius1=280; radius2=260;} if(camStr=="3") { radius1=320; radius2=300;} if(camStr=="4") { radius1=260; radius2=240;} if(camStr=="5") { radius1=320; radius2=300;} if(camStr=="6") { radius1=350; radius2=320;} if(camStr=="7") { radius1=300; radius2=280;} thresholdII=0.5; overallcourse=(ImgHistRebinned->Integral())/1000000000; overallfine= (ImageHistogram->Integral())/1000000000; writefile<GetMaximum()>0) { point1= ImgHistRebinned->GetMaximumBin(); //Spot 1 from ImgHistRebinned Double_t content= ImgHistRebinned->GetBinContent(point1); ImgHistRebinned->GetMaximumBin(xbin, ybin, zbin); yvalue=ImgHistRebinned->GetYaxis()->GetBinCenter(ybin); xvalue=ImgHistRebinned->GetXaxis()->GetBinCenter(xbin); // F.Soomro 6 April 09 // if(oneOrtwo=='1' && yvalue>0 && camStr=="4") // xvalue=ImgHistRebinned->GetXaxis()->GetBinCenter(xbin)-(content-39000)/325;// ------> for R1 box 4 // else xvalue=ImgHistRebinned->GetXaxis()->GetBinCenter(xbin); // if(oneOrtwo=='1' && camStr =='4'){ // commented out: F.Soomro 6 April 09 // if(command.debugmode == 1){ // std::cout<<"minus "<GetMaximum()<<", x,y centers of the bin: "<GetXaxis()->GetBinCenter(i); Double_t yy=ImgHistRebinned->GetYaxis()->GetBinCenter(j); Double_t cc=ImgHistRebinned->GetBinContent(i,j); if( sqrt(pow(xvalue-xx,2)+pow(yvalue-yy,2))< radius1 ) { xbuff+=xx*cc; ybuff+=yy*cc; cbuff+=cc; BuffHist->SetBinContent(i, j, cc); ImgHistRebinned->SetBinContent(i,j,0); } } xbuff=xbuff/cbuff; ybuff=ybuff/cbuff; std::cout<<"Preliminary calc of Spot 1 from the rebinned histogram --- ("<GetMaximum()*threshold; for(Int_t i=0; iGetBinContent(i,j); if(cc < buffermax) { BuffHist->SetBinContent(i,j,0);} else { Double_t xx=ImgHistRebinned->GetXaxis()->GetBinCenter(i); Double_t yy=ImgHistRebinned->GetYaxis()->GetBinCenter(j); if( sqrt(pow(xvalue-xx,2)+pow(yvalue-yy,2))GetMaximum(); std::cout<<" (should be equal to the content printed two lines before)!!"<GetMaximum()<<" "; Intensity[counter]=cbuff/a; Intensity[counter+1]=cbuff; xVal[counter]=xbuff; yVal[counter]=ybuff; counter++; if(toDraw) { c1->cd(4); ImgHistRebinned->DrawCopy("colz"); c1->cd(5); BuffHist->DrawCopy("colz"); } //Spot 1 from ImageHistogram if(oneOrtwo=='1' && camStr=="3" && yvalue<0) threshold=0.8; //----------------->box 4 xvalue=xbuff; yvalue=ybuff; xbuff=0; ybuff=0; cbuff=0; a = 0; for(Int_t i=0; iGetXaxis()->GetBinCenter(i); Double_t yy=ImageHistogram->GetYaxis()->GetBinCenter(j); Double_t cc=ImageHistogram->GetBinContent(i,j); if(sqrt(pow(xvalue-xx,2)+pow(yvalue-yy,2))SetBinContent(i,j, cc); ImageHistogram->SetBinContent(i,j,0); } }//int j }//int i buffermax=BufferHistogram->GetMaximum()*threshold; for(Int_t i=0; iGetXaxis()->GetBinCenter(i); Double_t yy=ImageHistogram->GetYaxis()->GetBinCenter(j); Double_t cc=BufferHistogram->GetBinContent(i,j); if( cc < buffermax) { BufferHistogram->SetBinContent(i,j,0); } else if( sqrt(pow(xvalue-xx,2)+pow(yvalue-yy,2))GetMaximum()<<" "; // Donot remove Intensity[counter]=cbuff/a;// BufferHistogram->GetMaximum(); xVal[counter]=xbuff; yVal[counter]=ybuff; counter++; if(toDraw) { c1->cd(6); ImageHistogram->DrawCopy("colz"); c1->cd(7); BufferHistogram->DrawCopy("colz"); } } // matches if max>0 if(oneOrtwo=='1') point1=int(ybuff); if(oneOrtwo=='2') point1=int(xbuff); //-------------------------->addition //Spot2 BuffHist->Reset(); if(ImgHistRebinned->GetMaximum()>0) { if(oneOrtwo=='1' ) { BuffHist->Reset(); Int_t xbinII=0; Int_t ybinII=0; point2= ImgHistRebinned->GetMaximumBin(xbinII, ybinII, zbin); Double_t deltaX=ImgHistRebinned->GetXaxis()->GetBinCenter(xbin)- ImgHistRebinned->GetXaxis()->GetBinCenter(xbinII); Double_t deltaY=ImgHistRebinned->GetYaxis()->GetBinCenter(ybin)- ImgHistRebinned->GetYaxis()->GetBinCenter(ybinII); Double_t deltaR=sqrt(pow(deltaX,2)+pow(deltaY,2)); while (deltaR<=deltaRad) { ImgHistRebinned->SetBinContent(point2,0); point2= ImgHistRebinned->GetMaximumBin(xbinII, ybinII, zbin); deltaX=ImgHistRebinned->GetXaxis()->GetBinCenter(xbin)-ImgHistRebinned->GetXaxis()->GetBinCenter(xbinII); deltaY=ImgHistRebinned->GetYaxis()->GetBinCenter(ybin)-ImgHistRebinned->GetYaxis()->GetBinCenter(ybinII); deltaR=sqrt(pow(deltaX,2)+pow(deltaY,2)); } } //Spot 2 from ImgHistRebinned xbuff=0; ybuff=0; cbuff=0; ImgHistRebinned->GetMaximumBin(xbin, ybin, zbin); xvalue=ImgHistRebinned->GetXaxis()->GetBinCenter(xbin); yvalue=ImgHistRebinned->GetYaxis()->GetBinCenter(ybin); std::cout<<"For Spot 2 from the rebinned histogram --- max bin no: "<GetMaximum()<<", x,y centers of the bin: "<GetXaxis()->GetBinCenter(i); Double_t yy=ImgHistRebinned->GetYaxis()->GetBinCenter(j); Double_t cc=ImgHistRebinned->GetBinContent(i,j); // if(cc>=ImgHistRebinned->GetMaximum()*threshold) if( sqrt(pow(xvalue-xx,2)+pow(yvalue-yy,2))SetBinContent(i,j,cc); ImgHistRebinned->SetBinContent(i,j,0); } }//int j }//int i xbuff=xbuff/cbuff; ybuff=ybuff/cbuff; std::cout<<"Preliminary calc of Spot 2 from the rebinned histogram --- ("<0) thresholdII=0.5; // ----------> R1cam03 - previosuly box 4 xvalue=xbuff; yvalue=ybuff; xbuff=0; ybuff=0; cbuff=0; a = 0; buffermax=BuffHist->GetMaximum()*threshold; for(Int_t i=0; i< w/10; i++){ for(Int_t j=0; jGetXaxis()->GetBinCenter(i); Double_t yy=ImgHistRebinned->GetYaxis()->GetBinCenter(j); Double_t cc=BuffHist->GetBinContent(i,j); if(cc < buffermax) { BuffHist->SetBinContent(i,j,0);} else if( sqrt(pow(xvalue-xx,2)+pow(yvalue-yy,2))< radius2) { xbuff+=xx*cc; ybuff+=yy*cc; cbuff+=cc; a++; } }//int j } //int i xbuff=xbuff/cbuff; ybuff=ybuff/cbuff; std::cout<<"Content of the max bin of BuffHist "<GetMaximum(); std::cout<<" (should be equal to the content printed two lines before)!!"<GetMaximum(); Intensity[counter+1]=cbuff; xVal[counter]=xbuff; yVal[counter]=ybuff; counter++; if(oneOrtwo=='1') point2 = int(ybuff); if(oneOrtwo=='2') point2 = int(xbuff); //-----------------.addition if(toDraw) { c1->cd(8); ImgHistRebinned->DrawCopy("colz"); c1->cd(9); BuffHist->DrawCopy("colz"); } //Spot 2 from ImageHistogram xvalue=xbuff; yvalue=ybuff; xbuff=0; ybuff=0; cbuff=0;a= 0; BufferHistogram->Reset(); for(Int_t i=0; iGetXaxis()->GetBinCenter(i); Double_t yy=ImageHistogram->GetYaxis()->GetBinCenter(j); Double_t cc=ImageHistogram->GetBinContent(i,j); if( sqrt(pow(xvalue-xx,2)+pow(yvalue-yy,2))< radius1) { BufferHistogram->SetBinContent(i,j,cc); ImageHistogram->SetBinContent(i,j,0); } }//int j }//int i buffermax=BufferHistogram->GetMaximum()*thresholdII; for(Int_t i=0; iGetXaxis()->GetBinCenter(i); Double_t yy=ImageHistogram->GetYaxis()->GetBinCenter(j); Double_t cc=BufferHistogram->GetBinContent(i,j); if( cc< buffermax) { BufferHistogram->SetBinContent(i,j,0);} else if( sqrt(pow(xvalue-xx,2)+pow(yvalue-yy,2))< radius2) { xbuff+=xx*cc; ybuff+=yy*cc; cbuff+=cc; a++; } }//int j }//int i xbuff=xbuff/cbuff; ybuff=ybuff/cbuff; std::cout<<"Final calc of Spot 2: from the original histogram --- ("<GetMaximum()<<" "; // Intensity[counter]=BufferHistogram->GetMaximum(); xVal[counter]=xbuff; yVal[counter]=ybuff; counter++; if(toDraw) { c1->cd(10); ImageHistogram->DrawCopy("colz"); c1->cd(11); BufferHistogram->DrawCopy("colz"); } std::cout<<"Thresholds used on spot1 and spot2: "<0 //**************ordering the values*********************// if(counter>4) std::cout<<"too many spots"<point2 && camStr!='3') || (point2>point1 && camStr=='3') ) { std::cout<<"Rich"<point2 ? point1 : point2) <<" is greater, changing the order now"<point2 ? point1 : point2) <<"is greater, and camStr is "<point2) { std::cout<<"camera is "<point2 ? point1 : point2) <<"is greater, and camStr is "<0.5) { quality = -1; std::cout<<"R2 message: the image was probably too bright"<Exec(toDel); ImgAnalysisResults Results; Results.refXCoord = xVal[1]; Results.refYCoord = yVal[1]; Results.refLumi = Intensity[1]; Results.reflXCoord = xVal[3]; Results.reflYCoord = yVal[3]; Results.reflLumi = Intensity[3]; // Results.radSep = sqrt(pow(xVal[3]-xVal[1],2)+pow(yVal[3]-yVal[1],2)); Results.imgLumi = overallfine; Results.quality = quality; std::cout<<"deleting the ifstream "<Exec(toDel); // delete c1; if(quality==4) { // std::cout<<"convert command not happy with the image\n\n"<RedirectOutput(0); delete ImageHistogram; delete ImgHistRebinned; delete BufferHistogram; delete BuffHist; delete total; delete c1; delete myfile; writefile.close(); LogFile.close(); return Results; } }