Ejemplo n.º 1
0
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
    case WM_COMMAND:
            {
                switch(LOWORD(wParam))
                    {
                    case BUTTON1:{ // OPEN A PE FILE  
                                    OPENFILENAME ofn;
                                    char szFileName[MAX_PATH] = "";
                                    SendDlgItemMessage(hwnd,BUTTON5, BN_CLICKED, (WPARAM)hBtn5,(LPARAM) hwnd);
                                    ZeroMemory(&ofn, sizeof(ofn));
                                    ofn.lStructSize = sizeof(ofn);
                                    ofn.hwndOwner = hwnd;
                                    ofn.lpstrFilter = "Executables (*.exe)\0*.exe\0Dll (*.dll)\0*.dll\0";
                                    ofn.lpstrFile = szFileName;
                                    ofn.nMaxFile = MAX_PATH;
                                    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
                                    ofn.lpstrDefExt = "";
                                    if(GetOpenFileName(&ofn)){
                                          SetDlgItemText(hwnd,EDIT1,szFileName);
                                    }
                    }break;
                    case BUTTON2:{ // READS INFORMATIONF FROM PE HEADER
                                    remove("import.txt");
                                    remove("export.txt");
                                    char txt[5024], txt1[2056], txt2[2056], txt3[2056], txt4[2056], txt5[2056]; 
                                    txt[0] = txt1[0] = txt2[0] = txt3[0] = txt4[0] = txt5[0] = '\0';
 
                                    int len = GetWindowTextLength (GetDlgItem (hwnd, EDIT1));
                                    GetDlgItemText(hwnd,EDIT1,FileName, len + 1);                  
                                    hFile = CreateFile(FileName, GENERIC_READ, FILE_SHARE_READ, 0,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
                                    if (hFile == INVALID_HANDLE_VALUE){
                                          MessageBoxA(NULL,"Cannot Open the File","Error",MB_OK);break;
                                    }
                                    
                                    FileSize = GetFileSize(hFile, NULL);
                                    BaseAddress = (BYTE *) malloc(FileSize);
                                    if (!ReadFile(hFile, BaseAddress, FileSize, &BR, NULL)){
                                          free(BaseAddress);
                                          CloseHandle(hFile);
                                          break;
                                    }
 
                                    ImageDosHeader = (IMAGE_DOS_HEADER *) BaseAddress;
                                    ImageOptionalHeader = (_IMAGE_OPTIONAL_HEADER *) BaseAddress;
                                    
                                    if (ImageDosHeader->e_magic != IMAGE_DOS_SIGNATURE) // checks the DOS SIGNATURE, if not equals to (MZ) then stops
                                    {
                                          MessageBoxA(NULL,"Invalid Dos Header","Error",MB_OK);
                                          free(BaseAddress);
                                          CloseHandle(hFile);
                                          break;
                                    }
 
                                    ImageNtHeaders = (IMAGE_NT_HEADERS *) (ImageDosHeader->e_lfanew + (DWORD) ImageDosHeader);
 
                                    if (ImageNtHeaders->Signature != IMAGE_NT_SIGNATURE) // checks the PE header, if not equals to NT signature stops
                                    {
                                          MessageBoxA(NULL,"Invalid PE Signature","Error",MB_OK);
                                          free(BaseAddress);
                                          CloseHandle(hFile);
                                          break;
                                    }
 
                                    // takes the address of first section
                                    ImageSectionHeader = IMAGE_FIRST_SECTION(ImageNtHeaders);
                                    // shows the section table
                                    for (x = 0; x < ImageNtHeaders->FileHeader.NumberOfSections; x++)
                                    {
                                          memcpy(SectionName, ImageSectionHeader[x].Name,IMAGE_SIZEOF_SHORT_NAME);
                                          long lSize;
                                          char * buffer;
                                          size_t result;  
                                          _ultoa(ImageSectionHeader[x].VirtualAddress,txt1,16);
                                          _ultoa(ImageSectionHeader[x].Misc.VirtualSize,txt2,16);
                                          _ultoa(ImageSectionHeader[x].PointerToRawData,txt3,16);
                                          _ultoa(ImageSectionHeader[x].SizeOfRawData,txt4,16);
                                          _ultoa(ImageSectionHeader[x].Characteristics,txt5,16);
                                          
                                          strcat(txt, "Section Name: ");
                                          strcat(txt, SectionName);
                                          strcat(txt , "\r\n");
                                          strcat(txt, "Virtual Address: ");
                                          strcat(txt, txt1);
                                          strcat(txt , "\r\n");
                                          strcat(txt, "Virtual Size: ");
                                          strcat(txt, txt2);
                                          strcat(txt , "\r\n"); 
                                          strcat(txt, "Raw Address: ");
                                          strcat(txt, txt3);
                                          strcat(txt , "\r\n"); 
                                          strcat(txt, "Raw Size: ");
                                          strcat(txt, txt4);
                                          strcat(txt , "\r\n");
                                          strcat(txt, "Characteristics: ");
                                          strcat(txt, txt5);
                                          strcat(txt , "\r\n\r\n");
                                          
                                          SetDlgItemText(hwnd,EDIT2,txt);  
                                 }
                                 
                                 /* VARIABLES USED TO STORE AND SHOW INFORMATIONS */
                                 char Img[LENGTH],Img1[LENGTH];
                                 char AeP[LENGTH],AeP1[LENGTH];
                                 char BoC[LENGTH],BoC1[LENGTH];
                                 char BoD[LENGTH],BoD1[LENGTH];
                                 char Sa[LENGTH] ,Sa1[LENGTH];
                                 char Fa[LENGTH] ,Fa1[LENGTH];
                                 char Ss[LENGTH] ,Ss1[LENGTH];
                                 char SoC[LENGTH],SoC1[LENGTH];
                                 char SiD[LENGTH],SiD1[LENGTH];
                                 char SuD[LENGTH],SuD1[LENGTH];
                                 char Em[LENGTH] ,Em1[LENGTH];
                                 char Sgn[LENGTH],Sgn1[LENGTH];
                                 
                                 Img[0], Img1[0], AeP[0], AeP1[0], BoC[0], BoC1[0], BoD[0], BoD1[0], Sa[0] , Sa1[0] , Fa[0],  Fa1[0] = '\0';
                                 Ss[0] , Ss1[0] , SoC[0], SoC1[0], SiD[0], SiD1[0], SuD[0], SuD1[0], Em[0] , Em1[0] , Sgn[0], Sgn1[0] = '\0';
                                 
                                 for (int i = 0; i < LENGTH; i++){
                                       Img[i] = '\0';
                                       AeP[i] = '\0';
                                       BoC[i] = '\0';
                                       BoD[i] = '\0';
                                       Sa[i] =  '\0';
                                       Fa[i] =  '\0';
                                       Ss[i] =  '\0';
                                       SoC[i] = '\0';
                                       SiD[i] = '\0';
                                       SuD[i] = '\0';
                                       Em[i] =  '\0';
                                       Sgn[i] = '\0';
                                 }
                                 //STORES VALUES
                                 DWORD ImageBase = ImageNtHeaders->OptionalHeader.ImageBase;
                                 DWORD AddressOfEntryPoint = ImageNtHeaders->OptionalHeader.AddressOfEntryPoint;
                                 DWORD BaseOfCode = ImageNtHeaders->OptionalHeader.BaseOfCode;
                                 DWORD BaseOfData = ImageNtHeaders->OptionalHeader.BaseOfData;
                                 DWORD SectionAlignment = ImageNtHeaders->OptionalHeader.SectionAlignment;
                                 DWORD FileAlignment = ImageNtHeaders->OptionalHeader.FileAlignment;
                                 DWORD Subsystem = ImageNtHeaders->OptionalHeader.Subsystem;
                                 DWORD SizeOfCode = ImageNtHeaders->OptionalHeader.SizeOfCode;
                                 DWORD SizeOfInitializedData = ImageNtHeaders->OptionalHeader.SizeOfInitializedData;
                                 DWORD SizeOfUnitializedData = ImageNtHeaders->OptionalHeader.SizeOfUninitializedData;
                                 DWORD EMagic = ImageDosHeader->e_magic;
                                 DWORD Signature = ImageNtHeaders->Signature;
                                 
                                 /* CONVERT DWORD TO HEX ==> I REALLY LOVE THIS FUNCTION :P*/
                                 _ultoa(ImageBase,Img1,16);
                                 _ultoa(AddressOfEntryPoint,AeP1,16);
                                 _ultoa(BaseOfCode,BoC1,16);
                                 _ultoa(BaseOfData,BoD1,16);
                                 _ultoa(SectionAlignment,Sa1,16);
                                 _ultoa(FileAlignment,Fa1,16);
                                 _ultoa(Subsystem,Ss1,16);
                                 _ultoa(SizeOfCode,SoC1,16);
                                 _ultoa(SizeOfInitializedData,SiD1,16);
                                 _ultoa(SizeOfUnitializedData,SuD1,16);
                                 _ultoa(EMagic,Em1,16);
                                 _ultoa(Signature,Sgn1,16);
                                 
                                 strcat(Img,"ImageBase: 0x");
                                 strcat(Img, Img1);
                                 strcat(AeP, "AddressOfEntryPoint: 0x");
                                 strcat(AeP, AeP1);
                                 strcat(BoC, "BaseOfCode: 0x");
                                 strcat(BoC, BoC1);
                                 strcat(BoD, "BaseOfData: 0x");
                                 strcat(BoD, BoD1);
                                 strcat(Sa, "SectionAlignment: 0x");
                                 strcat(Sa, Sa1);
                                 strcat(Fa, "FileAlignment: 0x");
                                 strcat(Fa, Fa1);      
                                 strcat(Ss, "Subsystem: 0x");
                                 strcat(Ss, Ss1);
                                 strcat(SoC, "SizeOfCode: 0x");
                                 strcat(SoC, SoC1);
                                 strcat(SiD, "SizeOfInitializedData: 0x");
                                 strcat(SiD, SiD1);
                                 strcat(SuD, "SizeOfUnitializedData: 0x");
                                 strcat(SuD, SuD1);
                                 strcat(Em, "EMagic Signature: 0x");
                                 strcat(Em, Em1);
                                 strcat(Sgn, "PE Signature: 0x");
                                 strcat(Sgn, Sgn1);
                                 
                                 SetDlgItemText(hwnd,LABEL5, Img);
                                 SetDlgItemText(hwnd,LABEL6, AeP);
                                 SetDlgItemText(hwnd,LABEL7, BoC);
                                 SetDlgItemText(hwnd,LABEL8, BoD);
                                 SetDlgItemText(hwnd,LABEL9, Sa);
                                 SetDlgItemText(hwnd,LABEL10,Fa);
                                 SetDlgItemText(hwnd,LABEL11,Ss);
                                 SetDlgItemText(hwnd,LABEL12,SoC);
                                 SetDlgItemText(hwnd,LABEL13,SiD);
                                 SetDlgItemText(hwnd,LABEL14,SuD);
                                 SetDlgItemText(hwnd,LABEL15,Em);
                                 SetDlgItemText(hwnd,LABEL16,Sgn);    
                                 /* INFORMATION */
   
                                 /* LOGS FROM IMPORT TABLE */
 
                                 f = fopen("import.txt","wa");
                                 IT_Offset = RvaToOffset(ImageNtHeaders,ImageNtHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
                                 ImageImportDescr = (IMAGE_IMPORT_DESCRIPTOR *) (IT_Offset + (DWORD) BaseAddress);
                                 x = 0; // counter used under
                                                
                                 while (ImageImportDescr[x].FirstThunk != 0) // analyzes all  descriptors
                                 {
                                       Name = (char *) (RvaToOffset(ImageNtHeaders, ImageImportDescr[x].Name) + (DWORD) BaseAddress);
                                       fprintf(f,"\nModule Name: %s\r\nFunctions:\r\n", Name);
 
                                       // selects the array to analyze
                                       Thunks = (DWORD *) (RvaToOffset(ImageNtHeaders, ImageImportDescr[x].OriginalFirstThunk != 0 ? ImageImportDescr[x].OriginalFirstThunk : ImageImportDescr[x].FirstThunk) + (DWORD) BaseAddress);
                                       y = 0; // another counter
 
                                       // browse into internal functions of the analyzed file
                                       while (Thunks[y] != 0)
                                       {
                                             //imports
                                             if (Thunks[y] & IMAGE_ORDINAL_FLAG)
                                             {
                                                   fprintf(f,"Ordinal: %08X\r\n", (Thunks[y] - IMAGE_ORDINAL_FLAG));
                                                   y++;
                                                   continue;
                                             }
 
                                       ImgName = (IMAGE_IMPORT_BY_NAME *) (RvaToOffset(ImageNtHeaders, Thunks[y]) + (DWORD) BaseAddress);
                                       fprintf(f,"Name: %s\r\n", &ImgName->Name);
                                       y++;
                                       }
 
                                       x++;
                                 } 
                                 fflush(f);
                                 fclose(f);
                                 std::ifstream of("import.txt");
                                 char *s;
                                 char sf[LENGTH*50],sf1[LENGTH*50];
                                 sf[0], sf1[0] = '\0';

                                 while (of)
                                 {
                                       of.getline(sf,1000);
                                       strcat(sf1,sf);
                                       strcat(sf1,"\r\n");
                                 }
                                 SetDlgItemText(hwnd,EDIT3,sf1);
                                 of.close();
                                 remove("import.txt");
                                 /* IMPORT TABLE */   
 
                                 /* LOGS FROM EXPORT TABLE */
                                 ET_Offset = RvaToOffset(ImageNtHeaders,ImageNtHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
                                 ImageExportDir = (IMAGE_EXPORT_DIRECTORY *) (ET_Offset +(DWORD) BaseAddress);
                                 Name = (char *) (RvaToOffset(ImageNtHeaders,ImageExportDir->Name) + (DWORD) BaseAddress);
                                 Functions = (DWORD *) (RvaToOffset(ImageNtHeaders, ImageExportDir->AddressOfFunctions) + (DWORD) BaseAddress);
                                 Names = (DWORD *) (RvaToOffset(ImageNtHeaders, ImageExportDir->AddressOfNames) + (DWORD) BaseAddress);
                                 NameOrds = (WORD *) (RvaToOffset(ImageNtHeaders, ImageExportDir->AddressOfNameOrdinals) + (DWORD) BaseAddress);
 
                                 // enums and shows functions
 
                                 for (x = 0; x < ImageExportDir->NumberOfFunctions; x++)
                                 {
                                       // controllo se l'EP e' 0
                                       // se si' allora passa alla prossima funzione
                                       if (Functions[x] == 0)continue;
 
                                       printf("\nOrd: %04X\nEP: %08X\n", (x + ImageExportDir->Base), Functions[x]);
                                       f=fopen("export.txt","a");
                                       
                                       if (f != NULL) {
                                             fprintf(f,"\nOrd: %04X\nEP: %08X\n", (x + ImageExportDir->Base), Functions[x] );
                                             fflush(f);
                                       }
                                       
                                       // if the function has also a name I show it
                                       for (y = 0; y < ImageExportDir->NumberOfNames; y++)
                                       {
                                             if (NameOrds[y] == x)
                                             {
                                                   FName = (char *) (RvaToOffset(ImageNtHeaders, Names[y]) + (DWORD) BaseAddress);
                                                   printf("Name: %s\n", FName);
                                                   if (f != NULL) 
                                                   {
                                                         fprintf(f,"Name: %s\n", FName);
                                                         fflush(f);
                                                         fclose(f);
                                                   }
                                             break;
                                             }
                                       }
                                 }
                                 
                                 std::ifstream of1("export.txt");
                                 char sf2[LENGTH*80],sf3[LENGTH*80];
                                 sf2[0], sf3[0] = '\0';

                                 while (of1) 
                                 {
                                       of1.getline(sf2,1000);
                                       strcat(sf3,sf2);
                                       strcat(sf3,"\r\n");
                                 }
                                 SetDlgItemText(hwnd,EDIT4,sf3);
                                 of1.close();
                                 remove("export.txt");
                                 /* EXPORT TABLE */
   
                                 free(BaseAddress);
                                 CloseHandle(hFile);
                         }break; // end case BUTTON2   
                         
                         case BUTTON3:{PostQuitMessage (0);break;}
                         
                         case BUTTON4:{
 
                                 HANDLE hFile;
                                 BYTE *BaseAddress;
                                 WORD nSection;
                                 DWORD FileSize, BRW, NameSize, Size;
                                 char Dim[MAX_PATH], Sect[MAX_PATH];
                                 int len, lent;
                                  
                                 IMAGE_DOS_HEADER *ImageDosHeader;
                                 IMAGE_NT_HEADERS *ImageNtHeaders;
                                 IMAGE_SECTION_HEADER *ImageSectionHeader;
 
                                 len = GetWindowTextLength (GetDlgItem (hwnd, EDIT6));
                                 GetDlgItemText(hwnd,EDIT6,Dim, len + 1);  
                                 lent = GetWindowTextLength (GetDlgItem (hwnd, EDIT5));
                                 GetDlgItemText(hwnd,EDIT5,Sect, lent + 1);   
                                 Size = atoi(Dim);
 
                                 if (Size > 0 && lent > 0) 
                                 {
 
                                       hFile = CreateFile(FileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
 
                                       if (hFile == INVALID_HANDLE_VALUE)
                                       {
                                             printf("Cannot Open the File\n");
                                             return -1;
                                       }
 
                                       FileSize = GetFileSize(hFile, NULL);
                                       BaseAddress = (BYTE *) malloc(FileSize);
 
                                       if (!ReadFile(hFile, BaseAddress, FileSize, &BRW, NULL))
                                       {
                                             free(BaseAddress);
                                             CloseHandle(hFile);
                                             return -1;
                                       }
 
                                       ImageDosHeader = (IMAGE_DOS_HEADER *) BaseAddress;
                                       ImageOptionalHeader = (_IMAGE_OPTIONAL_HEADER *) BaseAddress;
                                    
                                       if (ImageDosHeader->e_magic != IMAGE_DOS_SIGNATURE) // checks the DOS SIGNATURE, if not equals to (MZ) then stops
                                       {
                                             MessageBoxA(NULL,"Invalid Dos Header","Error",MB_OK);
                                             free(BaseAddress);
                                             CloseHandle(hFile);
                                             break;
                                       }
 
                                       ImageNtHeaders = (IMAGE_NT_HEADERS *) (ImageDosHeader->e_lfanew + (DWORD) ImageDosHeader);
 
                                       if (ImageNtHeaders->Signature != IMAGE_NT_SIGNATURE) // checks the PE header, if not equals to NT signature stops
                                       {
                                             MessageBoxA(NULL,"Invalid PE Signature","Error",MB_OK);
                                             free(BaseAddress);
                                             CloseHandle(hFile);
                                             break;
                                       }
 
                                       // prende le dimensioni
                                       printf("Creating New Section...\n");
 
                                       nSection = ImageNtHeaders->FileHeader.NumberOfSections;
                                       ImageNtHeaders->FileHeader.NumberOfSections++;
                                       ImageSectionHeader = IMAGE_FIRST_SECTION(ImageNtHeaders);
                                       ImageNtHeaders->OptionalHeader.SizeOfImage += CalcAlignment(ImageNtHeaders->OptionalHeader.SectionAlignment, Size);
                                       ZeroMemory(&ImageSectionHeader[nSection], IMAGE_SIZEOF_SECTION_HEADER);
 
                                       if (strlen(Sect) <= IMAGE_SIZEOF_SHORT_NAME)
                                       {
                                             NameSize = strlen(Sect);
                                       }
                                       else
                                       {
                                             NameSize = IMAGE_SIZEOF_SHORT_NAME;   
                                       }
      
                                       memcpy(&ImageSectionHeader[nSection].Name, Sect, NameSize);
 
                                       // calcola il Virtual Address della nuova sezione
                                       ImageSectionHeader[nSection].VirtualAddress = CalcAlignment(ImageNtHeaders->OptionalHeader.SectionAlignment, (ImageSectionHeader[nSection - 1].VirtualAddress + ImageSectionHeader[nSection - 1].Misc.VirtualSize));
                                       ImageSectionHeader[nSection].Misc.VirtualSize = Size;
 
                                       if (ImageSectionHeader[nSection - 1].SizeOfRawData % ImageNtHeaders->OptionalHeader.FileAlignment)
                                       {
                                       // se la sezione prima di quella che vogliamo creare noi non � allineata lo faccio
                                             ImageSectionHeader[nSection - 1].SizeOfRawData = CalcAlignment(ImageNtHeaders->OptionalHeader.FileAlignment, ImageSectionHeader[nSection - 1].SizeOfRawData);
                                             SetFilePointer(hFile,(ImageSectionHeader[nSection - 1].PointerToRawData + ImageSectionHeader[nSection - 1].SizeOfRawData), NULL, FILE_BEGIN);
                                             SetEndOfFile(hFile);
                                       }
 
                                       ImageSectionHeader[nSection].PointerToRawData = GetFileSize(hFile, NULL);
                                       ImageSectionHeader[nSection].SizeOfRawData = CalcAlignment(ImageNtHeaders->OptionalHeader.FileAlignment, Size);
                                       ImageSectionHeader[nSection].Characteristics = IMAGE_SCN_MEM_READ;
                                       SetFilePointer(hFile, ImageSectionHeader[nSection].SizeOfRawData,NULL, FILE_END);
                                       SetEndOfFile(hFile);
 
                                       // salvo le modifiche
                                       SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
                                       WriteFile(hFile, BaseAddress, FileSize, &BRW, NULL);
                                       MessageBox(NULL,"The section has been successfully added!","Info",MB_OK); 
                                       free(BaseAddress);
                                       CloseHandle(hFile);
                                       
                                 }
                                 else 
                                 {
                                       MessageBox(NULL,"The section was not added correctly!","Error",MB_OK);                                       
                                 }   
 
 
                  }break; // fine button 4
                  
                  case BUTTON5:
                  {
                        MessageBoxA(NULL,"Program created by Zyrel aka Marco Lagalla.\nVisit us on: <a href="http://www.unfair-gamers.com" target="_blank">http://www.unfair-gamers.com</a>!\nThanks to Ntoskrnl <img src="images/smilies1/wink1.gif" style="vertical-align: middle;" border="0" alt="Wink" title="Wink" />","Credits and Disclaimer",MB_OK);
                  }break;
                  
                  case BUTTON6:
                  {
                        ShellExecute(NULL,"open","http://www.unfair-gamers.com/forum/index.php",NULL,NULL,SW_HIDE);
                  }break;
                  
                  default:{break;} // DEFAUL CONTROLLI
             }break;} // FINE WM_COMMAND
            
            case WM_DESTROY:
            {
                  PostQuitMessage (0);
            
            }break;     /* send a WM_QUIT to the message queue */
            
            default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
            
            case WM_CTLCOLORSTATIC:
            {
               SetTextColor((HDC)hLabel2,RGB(141,149,155)); 
            } 
    }
 
    return 0;
}
Ejemplo n.º 2
0
//===========================================================================
void SplineCurve::appendCurve(ParamCurve* other_curve,
			      int continuity, double& dist, bool repar)
//===========================================================================
{
    SplineCurve* other_cv = dynamic_cast<SplineCurve*>(other_curve);
    ALWAYS_ERROR_IF(other_cv == 0,
		"Given an empty curve or not a SplineCurve.");
    ALWAYS_ERROR_IF(dim_ != other_cv->dimension(),
		    "The curves must lie in the same space.");

    ALWAYS_ERROR_IF(continuity < -1 || continuity + 1 > order(),
		    "Specified continuity not attainable.");

#ifdef DEBUG
    // DEBUG OUTPUT
    std::ofstream of0("basis0.g2");
    writeStandardHeader(of0);
    write(of0);
    other_cv->writeStandardHeader(of0);
    other_cv->write(of0);
#endif

    // Making sure the curves have the same order. Raise if necessary.
    int diff_order = order() - other_cv->order();
    if (diff_order > 0)
      other_cv->raiseOrder(diff_order);
    else if (diff_order < 0)
      raiseOrder(abs(diff_order));

    // Make sure that we have k-regularity at meeting ends.
    makeKnotEndRegular();
    other_cv->makeKnotStartRegular();

    // Ensure that either none of the curves or both are rational 
    if (rational_ && !other_cv->rational())
      other_cv->representAsRational();
    if (!rational_ && other_cv->rational())
      representAsRational();

 #ifdef DEBUG
   // DEBUG OUTPUT
    std::ofstream of1("basis1.g2");
    writeStandardHeader(of1);
    write(of1);
    other_cv->writeStandardHeader(of1);
    other_cv->write(of1);
#endif

    if (rational_)
      {
	// Set end weight to 1
	setBdWeight(1.0, false);
	other_cv->setBdWeight(1.0, true);
      }

    // Reparametrization (translatation and mult.) of other_cv->basis().knots_ .
    if (repar && continuity > 0) {

      if (rational_)
	{
	  // The weights corresponding to the two coefficients close to the
	  // joints should be equal
	  equalBdWeights(false);
	  other_cv->equalBdWeights(true);
	}
    }

 #ifdef DEBUG
   // DEBUG OUTPUT
    std::ofstream of1_2("basis1_2.g2");
    writeStandardHeader(of1_2);
    write(of1_2);
    other_cv->writeStandardHeader(of1_2);
    other_cv->write(of1_2);
#endif

 #ifdef DEBUG
    // DEBUG OUTPUT
    std::ofstream of2("basis2.dat");
    basis_.write(of2);
    of2 << std::endl;
    other_cv->basis_.write(of2);
    of2 << std::endl;
#endif

    if (continuity > -1 && rational_)
      {
	// Make sure that the rational curves have equal weights in the end
	int k2 = (numCoefs() - 1) * (dim_+1);
	double frac = rcoefs_[k2+dim_]/other_cv->rcoefs_[dim_];
	int kn = other_cv->numCoefs();
	for (int j=0; j<kn*(dim_+1); ++j)
	  other_cv->rcoefs_[j] *= frac;
      }

#ifdef DEBUG
    // DEBUG OUTPUT
    std::ofstream of3("basis3.dat");
    basis_.write(of3);
    of3 << std::endl;
    other_cv->basis_.write(of3);
    of3 << std::endl;
#endif

    if (repar && continuity > 0) {

	double sum1 = 0;
	double sum2 = 0;
	if (rational_)
	  {
	    int k2 = (numCoefs() - 1) * (dim_+1);
	    int k1 = (numCoefs() - 2) * (dim_+1);
	    for (int j = 0; j < dim_; ++j)
	      {
		double t0 = (rcoefs_[k2 + j] - rcoefs_[k1 + j])*rcoefs_[k2 + dim_] -
		  rcoefs_[k2 + j]*(rcoefs_[k2 + dim_] - rcoefs_[k1 + dim_]);
		sum1 += t0*t0;
	      }
	    sum1 = sqrt(sum1)/(rcoefs_[k2+dim_]*rcoefs_[k2+dim_]);

	    k2 = dim_+1;
	    k1 = 0;
	    for (int j = 0; j < dim_; ++j)
	      {
		double t0 = (other_cv->rcoefs_[k2 + j] - 
			     other_cv->rcoefs_[k1 + j])*other_cv->rcoefs_[k1 + dim_] -
		  other_cv->rcoefs_[k1 + j]*(other_cv->rcoefs_[k2 + dim_] - 
					     other_cv->rcoefs_[k1 + dim_]);
		sum2 += t0*t0;
	      }
	    sum2 = sqrt(sum2)/(other_cv->rcoefs_[k1+dim_]*other_cv->rcoefs_[k1+dim_]);
	  }
	else
	  {
	    for (int j = 0; j < dim_; ++j) {
	      sum1 += (coefs_[(numCoefs() - 1) * dim_ + j] -
		       coefs_[(numCoefs() - 2) * dim_ + j]) *
		(coefs_[(numCoefs() - 1) * dim_ + j] -
		 coefs_[(numCoefs() - 2) * dim_ + j]);
	      sum2 += (other_cv->coefs_[dim_ + j] - other_cv->coefs_[j]) *
		(other_cv->coefs_[dim_ + j] - other_cv->coefs_[j]);
	    }
	    sum1 = sqrt(sum1);
	    sum2 = sqrt(sum2);
	  }

#ifdef DEBUG
    // DEBUG OUTPUT
    std::ofstream of4("basis4.dat");
    basis_.write(of4);
    of4 << std::endl;
    other_cv->basis_.write(of4);
    of4 << std::endl;
#endif

	if (sum1 > 1.0e-14) { // @@sbr We should have a universal noise-tolerance.
	  double del1 = basis_.begin()[numCoefs()] - basis_.begin()[numCoefs() - 1];
	  double del2 = other_cv->basis_.begin()[order()] -
	    other_cv->basis_.begin()[order() - 1];
	  double k = sum2*del1/(sum1*del2);
	    other_cv->basis_.rescale(endparam(), endparam() +
				     k * (other_cv->basis_.begin()
					  [other_cv->numCoefs() + order() - 1] -
					  other_cv->basis_.startparam()));
	} else {
	    MESSAGE("Curve seems to be degenerated in end pt!");
	}
    } else {
        other_cv->basis_.rescale(endparam(),
				 endparam() +
				 (other_cv->basis_.begin()
				  [other_cv->numCoefs() + order() - 1] -
				  other_cv->basis_.startparam()));
    }

    // Join the curve-segments (i.e. set endpoints equal), given that...
    if (continuity != -1) {
	for (int j = 0; j < dim_; ++j) {
	    other_cv->coefs_[j] = 
		coefs_[(numCoefs() - 1)*dim_ + j] =
		(coefs_[(numCoefs() - 1)*dim_ + j] + other_cv->coefs_[j])/2;
	    }
    }

    double tpar = basis_.endparam();
    int ti = numCoefs() + order() - 1; // Index of last occurence of tpar.

#ifdef DEBUG
    // DEBUG OUTPUT
    std::ofstream of5("basis5.dat");
    basis_.write(of5);
    of5 << std::endl;
    other_cv->basis_.write(of5);
    of5 << std::endl;
#endif

    // Add other_cv's coefs.
    if (rational_)
      {
	// Ensure identity of the weight in the joint
	other_cv->setBdWeight(rcoefs_[rcoefs_.size()-1], true);
      
	rcoefs_.insert(rcoefs_end(), other_cv->rcoefs_begin(), 
		       other_cv->rcoefs_end());
      }
    else
      coefs_.insert(coefs_end(), other_cv->coefs_begin(), other_cv->coefs_end());

#ifdef DEBUG
    // DEBUG OUTPUT
    std::ofstream of("basis.dat");
    basis_.write(of);
    of << std::endl;
    other_cv->basis_.write(of);
    of << std::endl;
#endif

    // Make an updated basis_ .
    std::vector<double> new_knotvector;
    std::copy(basis_.begin(), basis_.end(), std::back_inserter(new_knotvector));
    std::copy(other_cv->basis_.begin() + order(), other_cv->basis_.end(),
	 std::back_inserter(new_knotvector));
    basis_ = BsplineBasis(order(), new_knotvector.begin(), new_knotvector.end());

    if (rational_)
      updateCoefsFromRcoefs();

    SplineCurve orig_curve = *this; // Save curve for later estimates.

    // Obtain wanted smoothness.
    int i;
    try {
    for (i = 0; i < continuity + 1; ++i)
	removeKnot(tpar);
    }
    catch (...)
    {
	// Leave the knots
    }

    // Estimate distance between curve and smoothed curve: 
    // Raise (copy of) smoothed curve to original spline space 
    // and calculate max distance between corresponding spline-coefs.
    SplineCurve raised_smooth_curve = *this;
    std::vector<double> knots;
    for (i = 0; i < continuity + 1; ++i) knots.push_back(tpar);
    raised_smooth_curve.insertKnot(knots);
    double sum, root_sum;
    dist = 0;
    for (i = std::max(0, ti - (continuity + 1) - order()); 
	 i < ti - order() + continuity + 1; ++i) {
	sum = 0;
	for (int j = 0; j < dim_; ++j)
	    sum += (orig_curve.coefs_[i * dim_ + j] - 
		    raised_smooth_curve.coefs_[i * dim_ + j])
		    * (orig_curve.coefs_[i * dim_ + j] - 
		       raised_smooth_curve.coefs_[i * dim_ + j]);
	// to avoid use of the max function, which is likely to cause
	// trouble with the Microsoft Visual C++ Compiler, the following 
	// two lines are added, and the third one is commented out.
	root_sum = sqrt(sum);
	dist = dist > root_sum ? dist : root_sum;
	//dist = std::max(dist, sqrt(sum));
    }
}
Ejemplo n.º 3
0
Archivo: calcv2.C Proyecto: XuQiao/HI
void calcv2(){
    const int ncent = 6;
    const int npt = 25;
    int centbin[ncent+1] = {0,5,10,20,40,60,100};
    gStyle->SetOptFit(kFALSE);
    gStyle->SetOptStat(kFALSE);

    ifstream fcntbbc("c1_c2_central_ptfiner_south.dat");
    ifstream fcntbbcIn("c1_c2_central_ptIn_south.dat");
    ifstream fcntfvtx("c1_c2_central_ptfiner_north.dat");
    ifstream fcntfvtxIn("c1_c2_central_ptIn_north.dat");
    ifstream fbbcfvtx("c1_c2_central_ptIn_sn.dat");

    float c1cntbbc[ncent][npt], c2cntbbc[ncent][npt], c3cntbbc[ncent][npt];
    float c1cntbbcIn[ncent][npt], c2cntbbcIn[ncent][npt], c3cntbbcIn[ncent][npt];
    float c1cntfvtx[ncent][npt], c2cntfvtx[ncent][npt], c3cntfvtx[ncent][npt];
    float c1cntfvtxIn[ncent][npt], c2cntfvtxIn[ncent][npt], c3cntfvtxIn[ncent][npt];
    float c1bbcfvtx[ncent][npt], c2bbcfvtx[ncent][npt], c3bbcfvtx[ncent][npt];
    float c1errcntbbc[ncent][npt], c2errcntbbc[ncent][npt], c3errcntbbc[ncent][npt];
    float c1errcntbbcIn[ncent][npt], c2errcntbbcIn[ncent][npt], c3errcntbbcIn[ncent][npt];
    float c1errcntfvtx[ncent][npt], c2errcntfvtx[ncent][npt], c3errcntfvtx[ncent][npt];
    float c1errcntfvtxIn[ncent][npt], c2errcntfvtxIn[ncent][npt], c3errcntfvtxIn[ncent][npt];
    float c1errbbcfvtx[ncent][npt], c2errbbcfvtx[ncent][npt], c3errbbcfvtx[ncent][npt];
     
    ifstream PPfcntbbc("c1_c2_PP_centIn_south.dat");
    ifstream PPfcntbbcIn("c1_c2_PP_ptIn_south.dat");
    ifstream PPfcntfvtx("c1_c2_PP_centIn_north.dat");
    ifstream PPfcntfvtxIn("c1_c2_PP_ptIn_north.dat");
    ifstream PPfbbcfvtx("c1_c2_bbcfvtx_PP_centIn.dat");
    
    float c1PPcntbbc[ncent][npt], c2PPcntbbc[ncent][npt], c3PPcntbbc[ncent][npt];
    float c1PPcntbbcIn[ncent][npt], c2PPcntbbcIn[ncent][npt], c3PPcntbbcIn[ncent][npt];
    float c1PPcntfvtx[ncent][npt], c2PPcntfvtx[ncent][npt], c3PPcntfvtx[ncent][npt];
    float c1PPcntfvtxIn[ncent][npt], c2PPcntfvtxIn[ncent][npt], c3PPcntfvtxIn[ncent][npt];
    float c1PPbbcfvtx[ncent][npt], c2PPbbcfvtx[ncent][npt], c3PPbbcfvtx[ncent][npt];
    float c1PPerrcntbbc[ncent][npt], c2PPerrcntbbc[ncent][npt], c3PPerrcntbbc[ncent][npt];
    float c1PPerrcntbbcIn[ncent][npt], c2PPerrcntbbcIn[ncent][npt], c3PPerrcntbbcIn[ncent][npt];
    float c1PPerrcntfvtx[ncent][npt], c2PPerrcntfvtx[ncent][npt], c3PPerrcntfvtx[ncent][npt];
    float c1PPerrcntfvtxIn[ncent][npt], c2PPerrcntfvtxIn[ncent][npt], c3PPerrcntfvtxIn[ncent][npt];
    float c1PPerrbbcfvtx[ncent][npt], c2PPerrbbcfvtx[ncent][npt], c3PPerrbbcfvtx[ncent][npt];

    float tmp;
    int scale = 1; //0: use c1 as scale factor 1: use multiplicity as scale factor

//--------------Multiplicity-----------------------
float MPPbbc[ncent] = {3.8,1,1,1,1,1};
float Mbbc[ncent]= {58.9,1,1,1,1,1};
float MPPfvtx[ncent] = {0.57,1,1,1,1,1};
float Mfvtx[ncent]  = {4,1,1,1,1,1};
//MPPNpart[0] = 2;
//MNpart[0] = 11;
float MPPNpart[ncent] = {3.8,1,1,1,1,1}; //use bbc
float MNpart[ncent] = {58.9,1,1,1,1,1}; //use bbc

//--------------read in parameters--------------------------------------------
for(int icent=0;icent<ncent;icent++){
    //output txt
    ofstream of1(Form("v2cntbbcs_cent%d.dat",icent));
    ofstream of2(Form("v2cntfvtxs_cent%d.dat",icent));
    ofstream of(Form("v2_cent%d.dat",icent));
    ofstream ofsub1(Form("v2cntbbcs_cent%d_scale%d.dat",icent,scale));
    ofstream ofsub2(Form("v2cntfvtxs_cent%d_scale%d.dat",icent,scale));
    ofstream ofsub(Form("v2_cent%d_scale%d.dat",icent,scale));

      fcntbbcIn>>c1cntbbcIn[icent][0]>>c1errcntbbcIn[icent][0]>>c2cntbbcIn[icent][0]>>c2errcntbbcIn[icent][0]>>c3cntbbcIn[icent][0]>>c3errcntbbcIn[icent][0];
      fcntfvtxIn>>c1cntfvtxIn[icent][0]>>c1errcntfvtxIn[icent][0]>>c2cntfvtxIn[icent][0]>>c2errcntfvtxIn[icent][0]>>c3cntfvtxIn[icent][0]>>c3errcntfvtxIn[icent][0];
      fbbcfvtx>>c1bbcfvtx[icent][0]>>c1errbbcfvtx[icent][0]>>c2bbcfvtx[icent][0]>>c2errbbcfvtx[icent][0]>>c3bbcfvtx[icent][0]>>c3errbbcfvtx[icent][0];

    PPfcntbbcIn>>c1PPcntbbcIn[icent][0]>>c1PPerrcntbbcIn[icent][0]>>c2PPcntbbcIn[icent][0]>>c2PPerrcntbbcIn[icent][0]>>c3PPcntbbcIn[icent][0]>>c3PPerrcntbbcIn[icent][0];
    PPfcntfvtxIn>>c1PPcntfvtxIn[icent][0]>>c1PPerrcntfvtxIn[icent][0]>>c2PPcntfvtxIn[icent][0]>>c2PPerrcntfvtxIn[icent][0]>>c3PPcntfvtxIn[icent][0]>>c3PPerrcntfvtxIn[icent][0];
    PPfbbcfvtx>>c1PPbbcfvtx[icent][0]>>c1PPerrbbcfvtx[icent][0]>>c2PPbbcfvtx[icent][0]>>c2PPerrbbcfvtx[icent][0]>>c3PPbbcfvtx[icent][0]>>c3PPerrbbcfvtx[icent][0];
    //    fcntbbcIn>>tmp>>tmp>>c1cntbbcIn[icent][0]>>c1errcntbbcIn[icent][0]>>c2cntbbcIn[icent][0]>>c2errcntbbcIn[icent][0];
    //    fcntfvtxIn>>tmp>>tmp>>c1cntfvtxIn[icent][0]>>c1errcntfvtxIn[icent][0]>>c2cntfvtxIn[icent][0]>>c2errcntfvtxIn[icent][0];
    //    fbbcfvtx>>tmp>>tmp>>c1bbcfvtx[icent][0]>>c1errbbcfvtx[icent][0]>>c2bbcfvtx[icent][0]>>c2errbbcfvtx[icent][0];
    
for(int ipt=0;ipt<npt;ipt++){
        fcntbbc>>c1cntbbc[icent][ipt]>>c1errcntbbc[icent][ipt]>>c2cntbbc[icent][ipt]>>c2errcntbbc[icent][ipt]>>c3cntbbc[icent][ipt]>>c3errcntbbc[icent][ipt];
        fcntfvtx>>c1cntfvtx[icent][ipt]>>c1errcntfvtx[icent][ipt]>>c2cntfvtx[icent][ipt]>>c2errcntfvtx[icent][ipt]>>c3cntfvtx[icent][ipt]>>c3errcntfvtx[icent][ipt];
        PPfcntbbc>>c1PPcntbbc[icent][ipt]>>c1PPerrcntbbc[icent][ipt]>>c2PPcntbbc[icent][ipt]>>c2PPerrcntbbc[icent][ipt]>>c3PPcntbbc[icent][ipt]>>c3PPerrcntbbc[icent][ipt];
        PPfcntfvtx>>c1PPcntfvtx[icent][ipt]>>c1PPerrcntfvtx[icent][ipt]>>c2PPcntfvtx[icent][ipt]>>c2PPerrcntfvtx[icent][ipt]>>c3PPcntfvtx[icent][ipt]>>c3PPerrcntfvtx[icent][ipt];
    }
        cout<<"icent: "<<centbin[icent] << "\% to " << centbin[icent+1]<< "\%"<<endl;
        cout<<"cnt - bbc "<<c2cntbbcIn[icent][0]<<" "<<c2errcntbbcIn[icent][0]<<endl;
        cout<<"cnt - fvtx "<<c2cntfvtxIn[icent][0]<<" "<<c2errcntfvtxIn[icent][0]<<endl;
        cout<<"bbc - fvtx "<<c2bbcfvtx[icent][0]<<" "<<c2errbbcfvtx[icent][0]<<endl;

        cout<<"cnt - bbc PP"<<c2PPcntbbcIn[icent][0]<<" "<<c2PPerrcntbbcIn[icent][0]<<endl;
        cout<<"cnt - fvtx PP"<<c2PPcntfvtxIn[icent][0]<<" "<<c2PPerrcntfvtxIn[icent][0]<<endl;
        cout<<"bbc - fvtx PP"<<c2PPbbcfvtx[icent][0]<<" "<<c2PPerrbbcfvtx[icent][0]<<endl;

//---------------calculating cnt/bbc/fvtx inclusive v2------------------------------

        if(c2cntbbcIn[icent][0]*c2cntfvtxIn[icent][0]/c2bbcfvtx[icent][0]<=0) {cout<<"negative?"<<endl; continue;}
        float v2cnt = sqrt(c2cntbbcIn[icent][0]*c2cntfvtxIn[icent][0]/c2bbcfvtx[icent][0]);
        //float v2errcnt = 1/2.*v2cnt*sqrt(TMath::Power(c2errcntbbcIn[icent][0]/c2cntbbcIn[icent][0],2)+TMath::Power(c2errcntfvtxIn[icent][0]/c2cntfvtxIn[icent][0],2)+TMath::Power(c2errbbcfvtx[icent][0]/c2bbcfvtx[icent][0],2));
        float v2errcnt = get3sqerr(c2cntbbcIn[icent][0],c2errcntbbcIn[icent][0],c2cntfvtxIn[icent][0],c2errcntfvtxIn[icent][0],c2bbcfvtx[icent][0],c2errbbcfvtx[icent][0]);

        float v2bbc = c2cntbbcIn[icent][0]/v2cnt;
        //float v2errbbc = v2bbc * sqrt(TMath::Power(c2errcntbbcIn[icent][0]/c2cntbbcIn[icent][0],2)+TMath::Power(v2errcnt/v2cnt,2));
        float v2errbbc = get2derr(c2cntbbcIn[icent][0],c2errcntbbcIn[icent][0],v2cnt,v2errcnt);
        
        float v2fvtx = c2cntfvtxIn[icent][0]/v2cnt;
        //float v2errfvtx = v2fvtx * sqrt(TMath::Power(c2errcntfvtxIn[icent][0]/c2cntfvtxIn[icent][0],2)+TMath::Power(v2errcnt/v2cnt,2));
        float v2errfvtx = get2derr(c2cntfvtxIn[icent][0],c2errcntfvtxIn[icent][0],v2cnt,v2errcnt);

        cout<<"Integral v2 cnt = "<<v2cnt<<" "<<v2errcnt<<endl;
        cout<<"Integral v2 bbc = "<<v2bbc<<" "<<v2errbbc<<endl;
        cout<<"Integral v2 fvtx = "<<v2fvtx<<" "<<v2errfvtx<<endl;

//------------calculating cnt pt dependence v2--------------------method1----------------
        
        float v2cntpt1[ncent][npt], v2errcntpt1[ncent][npt];
        float v2cntpt2[ncent][npt], v2errcntpt2[ncent][npt];
        float ptmean[npt];
    for(int ipt=0;ipt<npt;ipt++){
        v2cntpt1[icent][ipt] = c2cntbbc[icent][ipt]/v2bbc;
        //v2errcntpt1[icent][ipt] = v2cntpt1[icent][ipt]*sqrt(TMath::Power(c2errcntbbc[icent][ipt]/c2cntbbc[icent][ipt],2)+TMath::Power(v2errbbc/v2bbc,2));
        v2errcntpt1[icent][ipt] = get2derr(c2cntbbc[icent][ipt],c2errcntbbc[icent][ipt],v2bbc,v2errbbc);

        v2cntpt2[icent][ipt] = c2cntfvtx[icent][ipt]/v2fvtx;
        //v2errcntpt2[icent][ipt] = v2cntpt2[icent][ipt]*sqrt(TMath::Power(c2errcntfvtx[icent][ipt]/c2cntfvtx[icent][ipt],2)+TMath::Power(v2errfvtx/v2fvtx,2));
        v2errcntpt2[icent][ipt] = get2derr(c2cntfvtx[icent][ipt],c2errcntfvtx[icent][ipt],v2bbc,v2errbbc);
        ptmean[ipt] = 0.1+0.2*ipt;
    }

//------------calculating cnt pt dependence v2--------------------method2----------------
        
        float v2cntpt[ncent][npt];
        float v2errcntpt[ncent][npt];
    for(int ipt=0;ipt<npt;ipt++){
        v2cntpt[icent][ipt] = sqrt(c2cntbbc[icent][ipt]*c2cntfvtx[icent][ipt]/c2bbcfvtx[icent][0]);
        //v2errcntpt[icent][ipt] = v2cntpt[icent][ipt]*sqrt(TMath::Power(c2errcntbbc[icent][ipt]/c2cntbbc[icent][ipt],2)+TMath::Power(c2errcntfvtx[icent][ipt]/c2cntfvtx[icent][ipt],2)+TMath::Power(c2errbbcfvtx[icent][0]/c2bbcfvtx[icent][0],2));
        v2errcntpt[icent][ipt] = get3sqerr(c2cntbbc[icent][ipt],c2errcntbbc[icent][ipt],c2cntfvtx[icent][ipt],c2errcntfvtx[icent][ipt],c2bbcfvtx[icent][0],c2errbbcfvtx[icent][0]);

        of1<<ptmean[ipt]<<" "<<v2cntpt1[icent][ipt]<<" "<<v2errcntpt1[icent][ipt]<<endl;
        of2<<ptmean[ipt]<<" "<<v2cntpt2[icent][ipt]<<" "<<v2errcntpt2[icent][ipt]<<endl;
        of<<ptmean[ipt]<<" "<<v2cntpt[icent][ipt]<<" "<<v2errcntpt[icent][ipt]<<endl;
    }
    of1.close();
    of2.close();
    of.close();
        
//----subtract---------calculating cnt/bbc/fvtx inclusive v2------------------------------

    if(scale==0){
        c2cntbbcIn[icent][0] = c2cntbbcIn[icent][0] - c2PPcntbbcIn[icent][0]*c1cntbbcIn[icent][0]/c1PPcntbbcIn[icent][0];
        c2cntfvtxIn[icent][0] = c2cntfvtxIn[icent][0] - c2PPcntfvtxIn[icent][0]*c1cntfvtxIn[icent][0]/c1PPcntfvtxIn[icent][0];
        c2bbcfvtx[icent][0] = c2bbcfvtx[icent][0] - c2PPbbcfvtx[icent][0]*c1bbcfvtx[icent][0]/c1PPbbcfvtx[icent][0];
        c2errcntbbcIn[icent][0] = get2mierr(c2cntbbcIn[icent][0],c2errcntbbcIn[icent][0],c2PPcntbbcIn[icent][0]*c1cntbbcIn[icent][0]/c1PPcntbbcIn[icent][0], get3err(c2PPcntbbcIn[icent][0],c2PPerrcntbbcIn[icent][0],c1cntbbcIn[icent][0],c1errcntbbcIn[icent][0],c1PPcntbbcIn[icent][0],c1PPerrcntbbcIn[icent][0]));
        c2errcntfvtxIn[icent][0] = get2mierr(c2cntfvtxIn[icent][0],c2errcntfvtxIn[icent][0],c2PPcntfvtxIn[icent][0]*c1cntfvtxIn[icent][0]/c1PPcntfvtxIn[icent][0], get3err(c2PPcntfvtxIn[icent][0],c2PPerrcntfvtxIn[icent][0],c1cntfvtxIn[icent][0],c1errcntfvtxIn[icent][0],c1PPcntfvtxIn[icent][0],c1PPerrcntfvtxIn[icent][0]));
        c2errbbcfvtx[icent][0] = get2mierr(c2bbcfvtx[icent][0],c2errbbcfvtx[icent][0],c2PPbbcfvtx[icent][0]*c1bbcfvtx[icent][0]/c1PPbbcfvtx[icent][0], get3err(c2PPbbcfvtx[icent][0],c2PPerrbbcfvtx[icent][0],c1bbcfvtx[icent][0],c1errbbcfvtx[icent][0],c1PPbbcfvtx[icent][0],c1PPerrbbcfvtx[icent][0]));
    }
    else{
        c2cntbbcIn[icent][0] = c2cntbbcIn[icent][0] - c2PPcntbbcIn[icent][0]*MPPbbc[icent]/Mbbc[icent];
        c2cntfvtxIn[icent][0] = c2cntfvtxIn[icent][0] - c2PPcntfvtxIn[icent][0]*MPPfvtx[icent]/Mfvtx[icent];
        c2bbcfvtx[icent][0] = c2bbcfvtx[icent][0] - c2PPbbcfvtx[icent][0]*(MPPbbc[icent]*MPPfvtx[icent]/MPPNpart[icent])/(Mbbc[icent]*Mfvtx[icent]/MNpart[icent]);
        c2errcntbbcIn[icent][0] = get2mierr(c2cntbbcIn[icent][0],c2errcntbbcIn[icent][0],c2PPcntbbcIn[icent][0]*MPPbbc[icent]/Mbbc[icent],c2PPerrcntbbcIn[icent][0]*MPPbbc[icent]/Mbbc[icent]);
        c2errcntfvtxIn[icent][0] = get2mierr(c2cntfvtxIn[icent][0],c2errcntfvtxIn[icent][0],c2PPcntfvtxIn[icent][0]*MPPfvtx[icent]/Mfvtx[icent],c2PPerrcntfvtxIn[icent][0]*MPPfvtx[icent]/Mfvtx[icent]);
        c2errbbcfvtx[icent][0] = get2mierr(c2bbcfvtx[icent][0],c2errbbcfvtx[icent][0],c2PPbbcfvtx[icent][0]*(MPPbbc[icent]*MPPfvtx[icent]/MPPNpart[icent])/(Mbbc[icent]*Mfvtx[icent]/MNpart[icent]),c2PPerrbbcfvtx[icent][0]*(MPPbbc[icent]*MPPfvtx[icent]/MPPNpart[icent])/(Mbbc[icent]*Mfvtx[icent]/MNpart[icent]));
    }
        cout<<"cnt - bbc subtract ="<<c2cntbbcIn[icent][0]<<" "<<c2errcntbbcIn[icent][0]<<endl;
        cout<<"cnt - fvtx subtract ="<<c2cntfvtxIn[icent][0]<<" "<<c2errcntfvtxIn[icent][0]<<endl;
        cout<<"bbc - fvtx subtract ="<<c2bbcfvtx[icent][0]<<" "<<c2errbbcfvtx[icent][0]<<endl;

        float v2cnt = sqrt(c2cntbbcIn[icent][0]*c2cntfvtxIn[icent][0]/c2bbcfvtx[icent][0]);
      //  float v2errcnt = 1/2.*v2cnt*sqrt(TMath::Power(c2errcntbbcIn[icent][0]/c2cntbbcIn[icent][0],2)+TMath::Power(c2errcntfvtxIn[icent][0]/c2cntfvtxIn[icent][0],2)+TMath::Power(c2errbbcfvtx[icent][0]/c2bbcfvtx[icent][0],2));
        float v2errcnt = get3sqerr(c2cntbbcIn[icent][0],c2errcntbbcIn[icent][0],c2cntfvtxIn[icent][0],c2errcntfvtxIn[icent][0],c2bbcfvtx[icent][0],c2errbbcfvtx[icent][0]);

        float v2bbc = c2cntbbcIn[icent][0]/v2cnt;
        //float v2errbbc = v2bbc * sqrt(TMath::Power(c2errcntbbcIn[icent][0]/c2cntbbcIn[icent][0],2)+TMath::Power(v2errcnt/v2cnt,2));
        float v2errbbc = get2derr(c2cntbbcIn[icent][0],c2errcntbbcIn[icent][0],v2cnt,v2errcnt);
        
        float v2fvtx = c2cntfvtxIn[icent][0]/v2cnt;
        //float v2errfvtx = v2fvtx * sqrt(TMath::Power(c2errcntfvtxIn[icent][0]/c2cntfvtxIn[icent][0],2)+TMath::Power(v2errcnt/v2cnt,2));
        float v2errfvtx = get2derr(c2cntfvtxIn[icent][0],c2errcntfvtxIn[icent][0],v2cnt,v2errcnt);

        cout<<"Integral v2 subtract cnt = "<<v2cnt<<" "<<v2errcnt<<endl;
        cout<<"Integral v2 subtract bbc = "<<v2bbc<<" "<<v2errbbc<<endl;
        cout<<"Integral v2 subtract fvtx = "<<v2fvtx<<" "<<v2errfvtx<<endl;

//----subtract--------calculating cnt pt dependence v2--------------------method1----------------
        
        float v2cntpt1[ncent][npt], v2errcntpt1[ncent][npt];
        float v2cntpt2[ncent][npt], v2errcntpt2[ncent][npt];
        float ptmean[npt];
    for(int ipt=0;ipt<npt;ipt++){
    if(scale==0){
        c2cntbbc[icent][ipt] = c2cntbbc[icent][ipt] - c2PPcntbbc[icent][ipt]*c1cntbbc[icent][ipt]/c1PPcntbbc[icent][ipt];
        c2cntfvtx[icent][ipt] = c2cntfvtx[icent][ipt] - c2PPcntfvtx[icent][ipt]*c1cntfvtx[icent][ipt]/c1PPcntfvtx[icent][ipt];
        c2bbcfvtx[icent][ipt] = c2bbcfvtx[icent][ipt] - c2PPbbcfvtx[icent][ipt]*c1bbcfvtx[icent][ipt]/c1PPbbcfvtx[icent][ipt];
        c2errcntbbc[icent][ipt] = get2mierr(c2cntbbc[icent][ipt],c2errcntbbc[icent][ipt],c2PPcntbbc[icent][ipt]*c1cntbbc[icent][ipt]/c1PPcntbbc[icent][ipt], get3err(c2PPcntbbc[icent][ipt],c2PPerrcntbbc[icent][ipt],c1cntbbc[icent][ipt],c1errcntbbc[icent][ipt],c1PPcntbbc[icent][ipt],c1PPerrcntbbc[icent][ipt]));
        c2errcntfvtx[icent][ipt] = get2mierr(c2cntfvtx[icent][ipt],c2errcntfvtx[icent][ipt],c2PPcntfvtx[icent][ipt]*c1cntfvtx[icent][ipt]/c1PPcntfvtx[icent][ipt], get3err(c2PPcntfvtx[icent][ipt],c2PPerrcntfvtx[icent][ipt],c1cntfvtx[icent][ipt],c1errcntfvtx[icent][ipt],c1PPcntfvtx[icent][ipt],c1PPerrcntfvtx[icent][ipt]));
        c2errbbcfvtx[icent][ipt] = get2mierr(c2bbcfvtx[icent][ipt],c2errbbcfvtx[icent][ipt],c2PPbbcfvtx[icent][ipt]*c1bbcfvtx[icent][ipt]/c1PPbbcfvtx[icent][ipt], get3err(c2PPbbcfvtx[icent][ipt],c2PPerrbbcfvtx[icent][ipt],c1bbcfvtx[icent][ipt],c1errbbcfvtx[icent][ipt],c1PPbbcfvtx[icent][ipt],c1PPerrbbcfvtx[icent][ipt]));
    }
    else{
        c2cntbbc[icent][ipt] = c2cntbbc[icent][ipt] - c2PPcntbbc[icent][ipt]*MPPbbc[icent]/Mbbc[icent];
        c2cntfvtx[icent][ipt] = c2cntfvtx[icent][ipt] - c2PPcntfvtx[icent][ipt]*MPPfvtx[icent]/Mfvtx[icent];
        c2bbcfvtx[icent][ipt] = c2bbcfvtx[icent][ipt] - c2PPbbcfvtx[icent][ipt]*(MPPbbc[icent]*MPPfvtx[icent]/MPPNpart[icent])/(Mbbc[icent]*Mfvtx[icent]/MNpart[icent]);
        c2errcntbbc[icent][ipt] = get2mierr(c2cntbbc[icent][ipt],c2errcntbbc[icent][ipt],c2PPcntbbc[icent][ipt]*MPPbbc[icent]/Mbbc[icent],c2PPerrcntbbc[icent][ipt]*MPPbbc[icent]/Mbbc[icent]);
        c2errcntfvtx[icent][ipt] = get2mierr(c2cntfvtx[icent][ipt],c2errcntfvtx[icent][ipt],c2PPcntfvtx[icent][ipt]*MPPfvtx[icent]/Mfvtx[icent],c2PPerrcntfvtx[icent][ipt]*MPPfvtx[icent]/Mfvtx[icent]);
        c2errbbcfvtx[icent][ipt] = get2mierr(c2bbcfvtx[icent][ipt],c2errbbcfvtx[icent][ipt],c2PPbbcfvtx[icent][ipt]*(MPPbbc[icent]*MPPfvtx[icent]/MPPNpart[icent])/(Mbbc[icent]*Mfvtx[icent]/MNpart[icent]),c2PPerrbbcfvtx[icent][ipt]*(MPPbbc[icent]*MPPfvtx[icent]/MPPNpart[icent])/(Mbbc[icent]*Mfvtx[icent]/MNpart[icent]));
    }
    }
    for(int ipt=0;ipt<npt;ipt++){
        v2cntpt1[icent][ipt] = c2cntbbc[icent][ipt]/v2bbc;
        //v2errcntpt1[icent][ipt] = v2cntpt1[icent][ipt]*sqrt(TMath::Power(c2errcntbbc[icent][ipt]/c2cntbbc[icent][ipt],2)+TMath::Power(v2errbbc/v2bbc,2));
        v2errcntpt1[icent][ipt] = get2derr(c2cntbbc[icent][ipt],c2errcntbbc[icent][ipt],v2bbc,v2errbbc);

        v2cntpt2[icent][ipt] = c2cntfvtx[icent][ipt]/v2fvtx;
        //v2errcntpt2[icent][ipt] = v2cntpt2[icent][ipt]*sqrt(TMath::Power(c2errcntfvtx[icent][ipt]/c2cntfvtx[icent][ipt],2)+TMath::Power(v2errfvtx/v2fvtx,2));
        v2errcntpt2[icent][ipt] = get2derr(c2cntfvtx[icent][ipt],c2errcntfvtx[icent][ipt],v2fvtx,v2errfvtx);
        ptmean[ipt] = 0.1+0.2*ipt;
    }

//----subtract--------calculating cnt pt dependence v2--------------------method2----------------
        
        float v2cntpt[ncent][npt];
        float v2errcntpt[ncent][npt];
    for(int ipt=0;ipt<npt;ipt++){
        v2cntpt[icent][ipt] = sqrt(c2cntbbc[icent][ipt]*c2cntfvtx[icent][ipt]/c2bbcfvtx[icent][0]);
        //v2errcntpt[icent][ipt] = v2cntpt[icent][ipt]*sqrt(TMath::Power(c2errcntbbc[icent][ipt]/c2cntbbc[icent][ipt],2)+TMath::Power(c2errcntfvtx[icent][ipt]/c2cntfvtx[icent][ipt],2)+TMath::Power(c2errbbcfvtx[icent][0]/c2bbcfvtx[icent][0],2));
        v2errcntpt[icent][ipt] = get3sqerr(c2cntbbc[icent][ipt],c2errcntbbc[icent][ipt],c2cntfvtx[icent][ipt],c2errcntfvtx[icent][ipt],c2bbcfvtx[icent][0],c2errbbcfvtx[icent][0]);
        
        ofsub1<<ptmean[ipt]<<" "<<v2cntpt1[icent][ipt]<<" "<<v2errcntpt1[icent][ipt]<<endl;
        ofsub2<<ptmean[ipt]<<" "<<v2cntpt2[icent][ipt]<<" "<<v2errcntpt2[icent][ipt]<<endl;
        ofsub<<ptmean[ipt]<<" "<<v2cntpt[icent][ipt]<<" "<<v2errcntpt[icent][ipt]<<endl;
    }
    ofsub1.close();
    ofsub2.close();
    ofsub.close();


}
}