/* ==================================== */ int32_t solsyst2( mat22 m, vec2 b, vec2 sol) /* ==================================== */ { mat22 m1; double d, d1, d2; int32_t i, j; d = det2(m); if (((d >= 0) && (d < EPSILON)) || ((d <= 0) && (-d < EPSILON))) return 0; for (i = 0; i < 2; i++) m1[i][0] = b[i]; for (i = 0; i < 2; i++) m1[i][1] = m[i][1]; d1 = det2(m1); for (i = 0; i < 2; i++) m1[i][1] = b[i]; for (i = 0; i < 2; i++) m1[i][0] = m[i][0]; d2 = det2(m1); sol[0] = d1 / d; sol[1] = d2 / d; return 1; } // solsyst2()
void check_svd (double *M , double * U, double * S, double *V) { double T1 [4] ; double T2 [4] ; print_matrix("M",M) ; print_matrix("U",U) ; print_matrix("S",S) ; print_matrix("V",V) ; transp2(T1, V) ; prod2(T2, S, T1) ; prod2(T1, U, T2) ; print_matrix("USV'",T1) ; transp2(T1, U) ; prod2(T2, T1, U) ; print_matrix("U'U",T2) ; transp2(T1, V) ; prod2(T2, T1, V) ; print_matrix("V'V",T2) ; printf("det(M) = %f\n", det2(M)) ; printf("det(U) = %f\n", det2(U)) ; printf("det(V) = %f\n", det2(V)) ; printf("det(S) = %f\n", det2(S)) ; printf("\n") ; }
static double det3(double a1, double b1, double c1, double a2, double b2, double c2, double a3, double b3, double c3) { return a1*det2(b2, c2, b3, c3) - b1*det2(a2, c2, a3, c3) + c1*det2(a2, b2, a3, b3); }
double determinant(const Matrix3x3 &m) { double det = 0; det += m[0][0] * det2(m[1][1], m[1][2], m[2][1], m[2][2]); det -= m[0][1] * det2(m[1][0], m[1][2], m[2][0], m[2][2]); det += m[0][2] * det2(m[1][0], m[1][1], m[2][0], m[2][1]); return det; }
void adjoint3( LWFMatrix3 m, LWFMatrix3 adj ) { adj[ 0 ][ 0 ] = det2( m11, m12, m21, m22 ); adj[ 1 ][ 0 ] = -det2( m10, m12, m20, m22 ); adj[ 2 ][ 0 ] = det2( m10, m11, m20, m21 ); adj[ 0 ][ 1 ] = -det2( m01, m02, m21, m22 ); adj[ 1 ][ 1 ] = det2( m00, m02, m20, m22 ); adj[ 2 ][ 1 ] = -det2( m00, m01, m20, m21 ); adj[ 0 ][ 2 ] = det2( m01, m02, m11, m12 ); adj[ 1 ][ 2 ] = -det2( m00, m02, m10, m12 ); adj[ 2 ][ 2 ] = det2( m00, m01, m10, m11 ); }
// adjoint matrix: b = adjoint(a); returns determinant(a) double adjointMatrix(double a[3][3], double b[3][3]) { b[0][0] = det2(a[1][1], a[1][2], a[2][1], a[2][2]); b[1][0] = det2(a[1][2], a[1][0], a[2][2], a[2][0]); b[2][0] = det2(a[1][0], a[1][1], a[2][0], a[2][1]); b[0][1] = det2(a[2][1], a[2][2], a[0][1], a[0][2]); b[1][1] = det2(a[2][2], a[2][0], a[0][2], a[0][0]); b[2][1] = det2(a[2][0], a[2][1], a[0][0], a[0][1]); b[0][2] = det2(a[0][1], a[0][2], a[1][1], a[1][2]); b[1][2] = det2(a[0][2], a[0][0], a[1][2], a[1][0]); b[2][2] = det2(a[0][0], a[0][1], a[1][0], a[1][1]); return a[0][0]*b[0][0] + a[0][1]*b[0][1] + a[0][2]*b[0][2]; }
bool isConvex(vector<vector<int>>& p) { long n = p.size(), prev = 0, cur; for (int i = 0; i < n; ++i) { vector<vector<int>> A; // = {p[(i+1)%n]-p[i], p[(i+2)%n]-p[i]} for (int j = 1; j < 3; ++j) A.push_back({p[(i+j)%n][0]-p[i][0], p[(i+j)%n][1]-p[i][1]}); if (cur = det2(A)) if (cur*prev < 0) return false; else prev = cur; } return true; }
// calculate matrix for unit square to quad mapping void mapSquareToQuad(double quad[4][2], // vertices of quadrilateral double SQ[3][3]) // square->quad transform { double px, py; px = quad[0][0]-quad[1][0]+quad[2][0]-quad[3][0]; py = quad[0][1]-quad[1][1]+quad[2][1]-quad[3][1]; if (MATRIX_ZERO(px) && MATRIX_ZERO(py)) { SQ[0][0] = quad[1][0]-quad[0][0]; SQ[1][0] = quad[2][0]-quad[1][0]; SQ[2][0] = quad[0][0]; SQ[0][1] = quad[1][1]-quad[0][1]; SQ[1][1] = quad[2][1]-quad[1][1]; SQ[2][1] = quad[0][1]; SQ[0][2] = 0.; SQ[1][2] = 0.; SQ[2][2] = 1.; return; } else { double dx1, dx2, dy1, dy2, del; dx1 = quad[1][0]-quad[2][0]; dx2 = quad[3][0]-quad[2][0]; dy1 = quad[1][1]-quad[2][1]; dy2 = quad[3][1]-quad[2][1]; del = det2(dx1,dx2, dy1,dy2); SQ[0][2] = det2(px,dx2, py,dy2)/del; SQ[1][2] = det2(dx1,px, dy1,py)/del; SQ[2][2] = 1.; SQ[0][0] = quad[1][0]-quad[0][0]+SQ[0][2]*quad[1][0]; SQ[1][0] = quad[3][0]-quad[0][0]+SQ[1][2]*quad[3][0]; SQ[2][0] = quad[0][0]; SQ[0][1] = quad[1][1]-quad[0][1]+SQ[0][2]*quad[1][1]; SQ[1][1] = quad[3][1]-quad[0][1]+SQ[1][2]*quad[3][1]; SQ[2][1] = quad[0][1]; } }
Matrix3x3 inverse(const Matrix3x3 &m) { Matrix3x3 ret; double det = determinant(m); ret[0][0] = det2(m[1][1], m[1][2], m[2][1], m[2][2]) / det; ret[0][1] = det2(m[0][2], m[0][1], m[2][2], m[2][1]) / det; ret[0][2] = det2(m[0][1], m[0][2], m[1][1], m[1][2]) / det; ret[1][0] = det2(m[1][2], m[1][0], m[2][2], m[2][0]) / det; ret[1][1] = det2(m[0][0], m[0][2], m[2][0], m[2][2]) / det; ret[1][2] = det2(m[0][2], m[0][0], m[1][2], m[1][0]) / det; ret[2][0] = det2(m[1][0], m[1][1], m[2][0], m[2][1]) / det; ret[2][1] = det2(m[0][1], m[0][0], m[2][1], m[2][0]) / det; ret[2][2] = det2(m[0][0], m[0][1], m[1][0], m[1][1]) / det; return ret; }
//Рассчитать определитель double Matrix::det() const { if (!isSquare()) throw Matrix::NOT_SQUARE; if (width == 2) return det2(); else if (width == 3) return det3(); else return detN(); }
// Return 1 if the segment joining (x1,y1) and (x2,y2) intersects the // segment joining (x3,y3) and (x4,y4), otherwise return 0. int checkSegmentsIntersection(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) { float denom, p, q; denom = det2(x2 - x1, x3 - x4, y2 - y1, y3 - y4); if (denom != 0) // The straight lines through (x1,y1) and (x2,y2) and through (x3,y3) and (x4,y4) // intersect uniquely at (1-p)(x1,y1) + p(x2,y2) = (1-q)(x3,y3) + q(x4,y4), which // is a point of both segments if both p and q are between 0 and 1. { p = det2(x3 - x1, x3 - x4, y3 - y1, y3 - y4) / denom; q = det2(x2 - x1, x3 - x1, y2 - y1, y3 - y1) / denom; if ( (p >= 0) && (p <= 1) && (q >= 0) && (q <= 1) ) return 1; else return 0; } else if ( det2(x3 - x2, x3 - x1, y3 - y2, y3 - y1) != 0 ) // The straight lines through (x1,y1) and (x2,y2) and through (x3,y3) and (x4,y4) // do no intersect uniquely, and (x1,y1), (x2,y2) and (x3,y3) are not collinear, // in which case the segments do not intersect. return 0; else // All four points are collinear, in which case they do not intersect if // (x3,y3) and (x4,y4) both lie on the same side of segment joining (x1,y1) and (x2,y2). { if ( ( (checkPointWRTSegment(x1, y1, x2, y2, x3, y3) == 1) && (checkPointWRTSegment(x1, y1, x2, y2, x4, y4) == 1) ) || ( (checkPointWRTSegment(x1, y1, x2, y2, x3, y3) == -1) && (checkPointWRTSegment(x1, y1, x2, y2, x4, y4) == -1) ) ) return 0; else return 1; } }
/* ==================================== */ int32_t invmat2( mat22 ma, mat22 mr) /* ==================================== */ { double det = det2( ma ); if ( fabs( det ) < EPSILON ) return 0; mr[0][0] = ma[1][1] / det; mr[1][0] = - ma[1][0] / det; mr[0][1] = - ma[0][1] / det; mr[1][1] = ma[0][0] / det; return 1; } // invmat2()
template <> Matrix<3, 3, float> Matrix<3, 3, float>::adjoint() const { return Matrix<3, 3, float>( Vector<3, float>( det2(c[1].y, c[2].y, c[1].z, c[2].z), det2(c[2].x, c[1].x, c[2].z, c[1].z), det2(c[1].x, c[2].x, c[1].y, c[2].y) ), Vector<3, float>( det2(c[2].y, c[0].y, c[2].z, c[0].z), det2(c[0].x, c[2].x, c[0].z, c[2].z), det2(c[2].x, c[0].x, c[2].y, c[0].y) ), Vector<3, float>( det2(c[0].y, c[1].y, c[0].z, c[1].z), det2(c[1].x, c[0].x, c[1].z, c[0].z), det2(c[0].x, c[1].x, c[0].y, c[1].y) ) ); }
Matrix3 Matrix3::inverse() const { Matrix3 m; double determinant = det(); if (determinant == 0.0) throw cvgException("[Matrix3] The matrix inverse does not exist"); double inv_det = 1.0 / determinant; // Calculate determinant matrix already transposed to increase speed // Negative subdeterminants are computed exchanging columns for speed m.value[0][0] = det2(value[1][1], value[1][2], value[2][1], value[2][2]) * inv_det; m.value[0][1] = det2(value[0][2], value[0][1], value[2][2], value[2][1]) * inv_det; m.value[0][2] = det2(value[0][1], value[0][2], value[1][1], value[1][2]) * inv_det; m.value[1][0] = det2(value[1][2], value[1][0], value[2][2], value[2][0]) * inv_det; m.value[1][1] = det2(value[0][0], value[0][2], value[2][0], value[2][2]) * inv_det; m.value[1][2] = det2(value[0][2], value[0][0], value[1][2], value[1][0]) * inv_det; m.value[2][0] = det2(value[1][0], value[1][1], value[2][0], value[2][1]) * inv_det; m.value[2][1] = det2(value[0][1], value[0][0], value[2][1], value[2][0]) * inv_det; m.value[2][2] = det2(value[0][0], value[0][1], value[1][0], value[1][1]) * inv_det; return m; }
Int_t r3bsim_batch(Double_t fEnergyP, Int_t fMult, Int_t nEvents, Int_t fGeoVer, Double_t fNonUni){ cout << "Running r3bsim_batch with arguments:" <<endl; cout << "Primary energy: " << fEnergyP << " MeV" <<endl; cout << "Multiplicity: " << fEnergyP <<endl; cout << "Number of events: " << nEvents <<endl; cout << "CALIFA geo version: " << fGeoVer <<endl; cout << "Non uniformity: " << fNonUni <<endl<<endl; // Load the Main Simulation macro gROOT->LoadMacro("r3ball_batch.C"); //------------------------------------------------- // Monte Carlo type | fMC (TString) //------------------------------------------------- // Geant3: "TGeant3" // Geant4: "TGeant4" // Fluka : "TFluka" TString fMC ="TGeant4"; //------------------------------------------------- // Primaries generation // Event Generator Type | fGene (TString) //------------------------------------------------- // Box generator: "box" // CALIFA generator: "gammas" // R3B spec. generator: "r3b" TString fGene="gammas"; //------------------------------------------------- // Secondaries generation (G4 only) // R3B Spec. PhysicList | fUserPList (Bool_t) // ---------------------------------------------- // VMC Standard kFALSE // R3B Special kTRUE; Bool_t fUserPList= kTRUE; // Target type TString target1="LeadTarget"; TString target2="Para"; TString target3="Para45"; TString target4="LiH"; //------------------------------------------------- //- Geometrical Setup Definition //- Non Sensitiv | fDetName (String) //------------------------------------------------- // Target: TARGET // Magnet: ALADIN // GLAD //------------------------------------------------- //- Sensitiv | fDetName //------------------------------------------------- // Calorimeter: CALIFA // CRYSTALBALL // // Tof: TOF // MTOF // // Tracker: DCH // TRACKER // GFI // // Neutron: LAND TObjString det0("TARGET"); TObjString det1("ALADIN"); TObjString det2("GLAD"); TObjString det3("CALIFA"); TObjString det4("CRYSTALBALL"); TObjString det5("TOF"); TObjString det6("MTOF"); TObjString det7("DCH"); TObjString det8("TRACKER"); TObjString det9("GFI"); TObjString det10("LAND"); TObjArray fDetList; fDetList.Add(&det0); //fDetList.Add(&det2); fDetList.Add(&det3); //fDetList.Add(&det5); //fDetList.Add(&det6); //fDetList.Add(&det7); fDetList.Add(&det8); //fDetList.Add(&det9); //fDetList.Add(&det10); //------------------------------------------------- //- N# of Sim. Events | nEvents (Int_t) //------------------------------------------------- //NOW GIVEN AS AN ARGUMENT! //Int_t nEvents = 100; //------------------------------------------------- //- EventDisplay | fEventDisplay (Bool_t) //------------------------------------------------- // connected: kTRUE // not connected: kFALSE Bool_t fEventDisplay=kFALSE; // Magnet Field definition //Bool_t fR3BMagnet = kTRUE; Bool_t fR3BMagnet = kFALSE; // Main Sim function call r3ball_batch( nEvents, fDetList, target2, fEventDisplay, fMC, fGene, fUserPList, fR3BMagnet, fEnergyP, fMult, fGeoVer, fNonUni ); cout << "... Work done! " <<endl; }
float det3( LWFMatrix3 m ) { return m00 * det2( m11, m21, m12, m22 ) - m01 * det2( m10, m20, m12, m22 ) + m02 * det2( m10, m20, m11, m21 ); }
CByteMatrix CByteMatrix::Inv() const { if (m!=n) { pErrorProc((void*)this,MATERR_DIMENSION,dwData); return *this; } else { switch (m) { case 1: if (GetModulo256Inv(pMatrix[0])==0) { pErrorProc((void*)this,MATERR_NOINVERSE,dwData); return *this; } return CByteMatrix(1,1,GetModulo256Inv(pMatrix[0])); case 2: { BYTE nInvDet=GetModulo256Inv(det2(*this)); if (nInvDet==0) { pErrorProc((void*)this,MATERR_NOINVERSE,dwData); return *this; } return CByteMatrix(2,2,nInvDet*e(2,2),-nInvDet*e(1,2),-nInvDet*e(2,1),nInvDet*e(1,1)); } case 3: { BYTE nInvDet=GetModulo256Inv(det3(*this)); if (nInvDet==0) { pErrorProc((void*)this,MATERR_NOINVERSE,dwData); return *this; } CByteMatrix inv; inv.SetErrorProc(pErrorProc,dwData); inv.m=inv.n=3; inv.pMatrix=new BYTE[9]; for (UINT i=1;i<=3;i++) { for (UINT j=1;j<=3;j++) { me(inv,j,i)=nInvDet*Cof(i,j); } } return inv; } default: { BYTE nInvDet=GetModulo256Inv(det(*this)); if (nInvDet==0) { pErrorProc((void*)this,MATERR_NOINVERSE,dwData); return *this; } CByteMatrix inv; inv.SetErrorProc(pErrorProc,dwData); inv.m=inv.n=m; inv.pMatrix=new BYTE[inv.m*inv.m]; for (UINT i=1;i<=inv.m;i++) { for (UINT j=1;j<=inv.n;j++) { me(inv,j,i)=nInvDet*Cof(i,j); } } return inv; } } } pErrorProc((void*)this,MATERR_NOINVERSE,dwData); return *this; }
Int_t r3b_sim_and_rclass(){ // Load the Main Simulation macro gROOT->LoadMacro("r3ball.C"); //------------------------------------------------- // Monte Carlo type | fMC (TString) //------------------------------------------------- // Geant3: "TGeant3" // Geant4: "TGeant4" // Fluka : "TFluka" TString fMC ="TGeant3"; //------------------------------------------------- // Primaries generation // Event Generator Type | fGene (TString) //------------------------------------------------- // Box generator: "box" // Ascii generator: "ascii" // R3B spec. generator: "r3b" TString fGene="box"; //------------------------------------------------- // Secondaries generation (G4 only) // R3B Spec. PhysicList | fUserPList (Bool_t) // ---------------------------------------------- // VMC Standard kFALSE // R3B Special kTRUE; Bool_t fUserPList= kTRUE; // Target type TString target1="LeadTarget"; TString target2="Para"; TString target3="Para45"; TString target4="LiH"; //------------------------------------------------- //- Geometrical Setup Definition //- Non Sensitiv | fDetName (String) //------------------------------------------------- // Target: TARGET // Magnet: ALADIN // //------------------------------------------------- //- Sensitiv | fDetName //------------------------------------------------- // Calorimeter: CALIFA // CRYSTALBALL // // Tof: TOF // MTOF // // Tracker: DCH // TRACKER // GFI // // Neutron Detector // Plastic LAND // RPC // RPCFLAND // RPCMLAND TObjString det1("TARGET"); TObjString det2("ALADIN"); TObjString det3("CALIFA"); TObjString det4("CRYSTALBALL"); TObjString det5("TOF"); TObjString det6("MTOF"); TObjString det7("DCH"); TObjString det8("TRACKER"); TObjString det9("GFI"); TObjString det10("LAND"); TObjString det11("RPCMLAND"); TObjString det12("RPCFLAND"); TObjString det13("SCINTNEULAND"); TObjArray fDetList; // fDetList.Add(&det1); fDetList.Add(&det2); // fDetList.Add(&det4); // fDetList.Add(&det5); // fDetList.Add(&det6); // fDetList.Add(&det7); // fDetList.Add(&det8); // fDetList.Add(&det9); fDetList.Add(&det10); // fDetList.Add(&det11); //------------------------------------------------- //- N# of Sim. Events | nEvents (Int_t) //------------------------------------------------- Int_t nEvents = 1; //------------------------------------------------- //- EventDisplay | fEventDisplay (Bool_t) //------------------------------------------------- // connected: kTRUE // not connected: kFALSE Bool_t fEventDisplay=kTRUE; // Magnet Field definition Bool_t fR3BMagnet = kTRUE; // Main Sim function call r3ball( nEvents, fDetList, target1, fEventDisplay, fMC, fGene, fUserPList, fR3BMagnet ); //------------------- Read Data from LMD-ROOT file ------------------------------------- // the ROOT files (simulated and experimental) are the same but, for testing purpose, show how to use two quantities for comparison R3BLandData* LandData1 = new R3BLandData("s318_172.root", "h309"); // file input and tree name R3BLandData* LandData2 = new R3BLandData("s318_172.root", "h309"); // file input and tree name // Define diferent options for TCanvas TCanvas* c = new TCanvas("Compare quantities from ROOT files","ROOT Canvas"); c->Divide(2,1); // Divide a canvas in two ...or more int entries1 = LandData1->GetEntries(); // entries number of first TTree int entries2 = LandData2->GetEntries(); // entries number of first TTree TLeaf* leaf1; // TLeaf* leaf11; // define one leaf... TLeaf* leaf2; // TLeaf* leaf22; // define another leaf cout<<"Entries number in first TTree = "<<entries1<<endl; cout<<"Entries number in second TTree = "<<entries2<<endl; // Reprezentation of 1D histogram from ROOT files for (int i=0; i < entries1; i++) { leaf1 = LandData1->GetLeaf("Nte",i); // Accessing TLeaf informations // leaf11 = LandData1->GetLeaf("Nhe",i); // Accessing TLeaf informations LandData1->FillHisto1D(leaf1); // Fill 1D histogram ... automatical bin width // LandData1->FillHisto2D(leaf1,leaf11); // Fill 2D histogram ... automatical bin width } c->cd(1); LandData1->Draw1D(); // Plotting 1D histogram ... automatical bin width // LandData1->Draw2D(); // Plotting 2D histogram ... automatical bin width for (int i=0; i < entries2; i++) { leaf2 = LandData2->GetLeaf("Nhe",i); // Accessing TLeaf informations // leaf22 = LandData2->GetLeaf("Nte",i); // Accessing TLeaf informations LandData2->FillHisto1D(leaf2); // Fill 1D histogram ... automatical bin width // LandData2->FillHisto2D(leaf2,leaf22); // Fill 2D histogram ... automatical bin width } c->cd(2); LandData2->Draw1D(); // Plotting 1D histogram ... automatical bin width // LandData2->Draw2D(); // Plotting 2D histogram ... automatical bin width // ... or you can plot on the same histogram by comment the precedent two line and comment out the next line // LandData2->Draw1Dsame(); // Plotting 1D histogram ... automatical bin width // ... The same things for 2D histograming //####### for diferent TLeaf analysis ############### /* Int_t len = leaf1->GetLen(); for(Int_t j=0; j<len ; j++) { leaf1->GetValue(j); // TLeaf Value for different analysis } */ }