////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // Questa macro per ROOT (http://root.cern.ch) decide l'accoppiamento // ottimale tra moduli usando il metodo di "simulated annealing". // // Andrea Giammanco, 11/6/2005 // // (Anche il file di input e' online: http://cern.ch/andrea.giammanco/ds/modulevdepl.txt) // (Un esempio di file di output generato da questo programma: http://cern.ch/andrea.giammanco/ds/out.txt) // ////////////////////////////////////////////////////////////////////////////////////////////////////////////// //gROOT->Reset(); const Int_t nSteps=3000; // numero iterazioni per l'annealing const Double_t TI=1000.0; // "temperatura" iniziale const Double_t TF=1.0; // "temperatura" finale const Double_t K=1.0; // analogo della K di Boltzmann (non serve...) //void ds(Int_t strategy=1, Char_t input1[40]="moduli_stereolr_25_6_2005.txt", Char_t input2[40]="moduli_rphi_25_6_2005.txt", Char_t out[40]="out.txt", Double_t cut=0.3, Int_t verbosity=0) void ds(Int_t strategy=1, Char_t out[40]="out.txt", Double_t cut=0.2, Int_t verbosity=0) { /* sceglibassi(180,80.,"moduli_rphi_25_6_2005.txt","moduli_rphi_180.txt",0); sceglibassi(90,80.,"moduli_stereol_25_6_2005.txt","moduli_stereol_90.txt",0); sceglibassi(90,80.,"moduli_stereor_25_6_2005.txt","moduli_stereor_90.txt",0); */ // unisci("moduli_stereol_v230_4_7_2005.txt","moduli_stereor_v230_4_7_2005.txt","moduli_stereolr_v230_4_7_2005.txt",0); // run(strategy,"moduli_rphi_4_7_2005.txt","moduli_stereolr_v230_4_7_2005.txt",out,cut,verbosity); Int_t n1=conta("moduli_stereolr_12_7_2005.txt", verbosity); Int_t n2=conta("moduli_rphi_12_7_2005.txt", verbosity); Double_t media1=media(n1,"moduli_stereolr_12_7_2005.txt", verbosity); Double_t media2=media(n2,"moduli_rphi_12_7_2005.txt", verbosity); Int_t nmax=min(n1,n2); /* if (n1>n2) { // il n.1 dev'essere il piu' piccolo swap( input1, input2 ); swap( media1, media2 ); swap( n1, n2 ); } Char_t middle1[40]; Char_t middle2[40]; Char_t middle2bis[40]; sprintf(middle1,"%s_reordered",input1); riordina(nmax,input1,middle1,verbosity); sprintf(middle2,"%s_closer",input2); */ sceglivicini(nmax,media1,"moduli_rphi_12_7_2005.txt","moduli_rphi_temp.txt"); // sprintf(middle2bis,"%s_reordered",input2); riordina(nmax,"moduli_rphi_temp.txt","moduli_rphi_reord.txt",verbosity); riordina(nmax,"moduli_stereolr_12_7_2005.txt","moduli_stereolr_reord.txt",verbosity); run(strategy,"moduli_rphi_reord.txt","moduli_stereolr_reord.txt",out,cut,verbosity); } Int_t conta(Char_t input[40]="moduli_stereolr_25_6_2005.txt", Int_t verbosity=0, Double_t cut=0.2) { Int_t n=0; TString name[2000]; Char_t temporary[14]; Double_t y[2000]; ifstream in; in.open(input); do { in >> temporary >> y[n]; name[n]=temporary; if (!in.good()) break; if (verbosity==1) cout << "n = " << n << ", x = " << name[n] << ", y = " << y[n] << endl; n++; } while (1); if (verbosity==1) cout << "Numero moduli: " << n << endl; Double_t mean=0; for (Int_t i=0;icut) { if (verbosity==1) cout << name[i] << " V = " << y[i] << endl; } } return n; } Double_t media(Int_t n, Char_t input[40]="moduli_stereolr_25_6_2005.txt", Int_t verbosity=0) { TString name[2000]; Char_t temporary[14]; Double_t y[2000]; ifstream in; in.open(input); for (Int_t i=0;i> temporary >> y[i]; name[i]=temporary; if (verbosity==1) cout << "i = " << i << ", x = " << name[i] << ", y = " << y[i] << endl; } Double_t mean=0; for (Int_t i=0;i> temporary >> y[i]; name[i]=temporary; if (verbosity==1) cout << "i = " << i << ", x = " << name[i] << ", y = " << y[i] << endl; } // uso l'algoritmo "bubblesort" (http://www.aihorizon.com/essays/basiccs/lists/sorting/bubble.htm) per ordinare in Vdepl decrescente for ( int i = 0; i < n-1; ++i ) { for ( int j = 1; j < n-i; ++j ) { if ( y[j-1] < y[j] ) { swap( y[j-1], y[j] ); swap( name[j-1], name[j] ); } } } // output su file: ofstream output(out); if (! output) { cout << "Error opening output file" << endl; } for (Int_t i=0;i> temporary >> y1[n1]; name1[n1]=temporary; if (!in1.good()) break; if (verbosity==1) cout << "n1 = " << n1 << ", x1 = " << name1[n1] << ", y1 = " << y1[n1] << endl; n1++; } while (1); if (verbosity==1) cout << "Numero moduli: " << n1 << endl; ifstream in2; in2.open(input2); Int_t n2=0; do { in2 >> temporary >> y2[n2]; name2[n2]=temporary; if (!in2.good()) break; if (verbosity==1) cout << "n2 = " << n2 << ", x2 = " << name2[n2] << ", y2 = " << y2[n2] << endl; n2++; } while (1); if (verbosity==1) cout << "Numero moduli: " << n2 << endl; // output su file: ofstream output(out); if (! output) { cout << "Error opening output file" << endl; } for (Int_t i=0;i> temporary >> y[n]; name[n]=temporary; if (!in.good()) break; if (verbosity==1) cout << "n = " << n << ", x = " << name[n] << ", y = " << y[n] << endl; n++; } while (1); if (verbosity==1) cout << "Numero moduli: " << n << endl; if (n y[j] ) { swap( y[j-1], y[j] ); swap( name[j-1], name[j] ); } } } // output su file: ofstream output(out); if (! output) { cout << "Error opening output file" << endl; } for (Int_t i=0;isafetycut) { output << name[i] << " " << y[i] << endl; } else { cout << name[i] << " " << y[i] << ": sotto il taglio Vdepl > " << safetycut << endl; nmax++; } } output.close(); cout << "File riordinato: " << out << endl; } void sceglivicini(Int_t n1, Double_t valore, Char_t input[40]="moduli_stereolr_25_6_2005.txt",Char_t out[40]="moduli_stereolr_closer.txt", Int_t verbosity=0) { Int_t n2=0; TString name[2000]; Char_t temporary[14]; Double_t y[2000]; ifstream in; in.open(input); do { in >> temporary >> y[n2]; name[n2]=temporary; if (!in.good()) break; if (verbosity==1) cout << "n = " << n2 << ", x = " << name[n2] << ", y = " << y[n2] << endl; n2++; } while (1); if (verbosity==1) cout << "Numero moduli: " << n2 << endl; // selezione dei piu' vicini al valore scelto: // uso l'algoritmo "bubblesort" (http://www.aihorizon.com/essays/basiccs/lists/sorting/bubble.htm) per ordinare in |Vdepl-valore| for ( int i = 0; i < n2-1; ++i ) { for ( int j = 1; j < n2-i; ++j ) { if ( fabs(y[j-1]-valore) > fabs(y[j]-valore) ) { swap( y[j-1], y[j] ); swap( name[j-1], name[j] ); } } } // output su file: ofstream output(out); if (! output) { cout << "Error opening output file" << endl; } for (Int_t i=0;i> temporary >> x1[n1]; name1[n1]=temporary; if (!in1.good()) break; if (verbosity==1) cout << "n1 = " << n1 << ", x1 = " << name1[n1] << ", V = " << x1[n1] << endl; n1++; } while (1); cout << "Numero moduli file 1: " << n1 << endl; ifstream in2; in2.open(input2); Int_t n2=0; do { in2 >> temporary >> x2[n2]; name2[n2]=temporary; if (!in2.good()) break; if (verbosity==1) cout << "n2 = " << n2 << ", x2 = " << name2[n2] << ", V = " << x2[n2] << endl; n2++; } while (1); cout << "Numero moduli file 2: " << n2 << endl; Int_t nmax=min(n1,n2); // array delle distanze tra coppie: Double_t dpair[nModules][nModules]; for (Int_t i=0;icut) { output << " *"; counter++; } output << endl; } output << "L'asterisco significa accoppiamento troppo distante in V_depl (piu` del " << 100*cut << "%)" << endl; output << "Questo capita il " << 100*ratio(counter,nmax) << " % delle volte." << endl; output.close(); cout << "File di output con gli accoppiamenti: " << out << endl; } Double_t raffredda(Int_t t) { return TI*pow(TF/TI,(double)t / (double)nSteps); } bool tengo(Double_t nuovo, Double_t vecchio, Double_t temp) { Double_t e = exp((nuovo-vecchio)/(K*temp)); Double_t r= gRandom->Rndm(1); return rRndm(1)*nmax; Int_t i=r; // cout << i << endl; return i; } Double_t sommadistanze(Double_t *darray, Int_t n) // la quantita' da minimizzare nell'annealing { Double_t sum=0.; for (Int_t i=0; i